Skip to content

Instantly share code, notes, and snippets.

@SeyedAbdollahi
Last active December 5, 2021 19:38
Show Gist options
  • Save SeyedAbdollahi/dfa78c4e2c406009b549ab127f9ce74b to your computer and use it in GitHub Desktop.
Save SeyedAbdollahi/dfa78c4e2c406009b549ab127f9ce74b to your computer and use it in GitHub Desktop.
Example of adjusting contrast of an image with Python and Pygame
import pygame
img = pygame.image.load('F:\input.jpg')
W, H = img.get_width(), img.get_height()
contrast = int(input())
F = (259 * (contrast + 255)) / (255 * (259 - contrast))
def fix_color(color):
if color > 255:
return 255
elif color < 0:
return 0
else:
return int(color)
for x in range(W):
for y in range(H):
color = pygame.Surface.get_at(img, (x,y))
R , G , B = F * (color.r - 128) + 128, F * (color.g - 128) + 128, F * (color.b - 128) + 128
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