Monday, March 16, 2015

Studying for Python Mid-Term

So I will have my mid-term for my Python class soon and I wanted to share a script that will most likely be on the test.

This semester we have been working a little with Pygame, and this script uses Pygame to load an image and essentially change the color of the image depending on the color temperature. Here is what I have:

import pygame, sys
from pygame.locals import *
pygame.init()
grey = (150, 150, 150)
lite_grey = (220, 220, 220)
blue = (80, 144, 154)
dark_blue = (0,50,77)
lite_blue = (143,220,236)
display_surface = pygame.display.set_mode((900,948))
display_surface.fill(grey)
image = pygame.image.load("image_before.png").convert()
for x in range(900):
for y in range(948):
color_temp = image.get_at((x,y))
temp_int = color_temp[0] + color_temp[1] + color_temp[2]
integer = (max(color_temp[0], color_temp[1], color_temp[2]) + temp_int//2//3)
if integer < 70:
image.set_at((x,y),dark_blue)
elif integer < 100:
image.set_at((x,y),blue)
elif integer < 130:
image.set_at((x,y),lite_blue)
else:
image.set_at((x,y),lite_grey)
pygame.image.save(image, 'image_after.png')
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
display_surface.blit(image, (0,0))
pygame.display.update()

Here is what I got from this script:

Before and After
Pygame is still really new to me, but its really cool to be able to do this. Anyways, that's all for now, thanks for reading!

No comments:

Post a Comment