Skip to content

Instantly share code, notes, and snippets.

@aryamansharda
Last active January 9, 2021 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aryamansharda/f9ba77d41735932c6303c85abdccaff4 to your computer and use it in GitHub Desktop.
Save aryamansharda/f9ba77d41735932c6303c85abdccaff4 to your computer and use it in GitHub Desktop.
HistogramBrightness
from PIL import Image
def boundedPixelValue(color, brightnessFactor):
scaledValue = float(color * (1 + brightnessFactor))
if scaledValue < 0:
return 0
elif scaledValue > 255:
return 255
return int(scaledValue)
im = Image.open("source.jpg")
out = Image.new('RGB', im.size, 0xffffff)
brightnessFactor = float(input("Enter brightness amount (-1.0 to 1.0: "))
width, height = im.size
for x in range(width):
for y in range(height):
r,g,b = im.getpixel((x,y))
updatedR = boundedPixelValue(r, brightnessFactor)
updatedG = boundedPixelValue(g, brightnessFactor)
updatedB = boundedPixelValue(b, brightnessFactor)
out.putpixel((x,y), (updatedR, updatedG, updatedB))
out.save('brightnessScaled.jpg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment