Last active
December 5, 2021 19:38
-
-
Save SeyedAbdollahi/19f669077dd904dd32da32bc016602e0 to your computer and use it in GitHub Desktop.
Example of adjusting brightness of an image with Python and Pygame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
img = pygame.image.load('F:\input.jpg') | |
W, H = img.get_width(), img.get_height() | |
brightness = int(input()) | |
def fix_color(color): | |
if color > 255: | |
return 255 | |
elif color < 0: | |
return 0 | |
else: | |
return color | |
for x in range(W): | |
for y in range(H): | |
color = pygame.Surface.get_at(img, (x,y)) | |
R , G , B = color.r + brightness, color.g + brightness, color.b + brightness | |
R , G , B = fix_color(R), fix_color(G), fix_color(B) | |
color.update(R,G,B) | |
pygame.Surface.set_at(img, (x,y), color) | |
pygame.image.save(img, 'F:\output.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment