Created
September 14, 2019 15:24
-
-
Save tjguk/3bcf1f83e2e204c70ef5efbe1715668b to your computer and use it in GitHub Desktop.
Changing the saturation of an image
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
# | |
# You nearly always just need to import Image | |
# from PIL(low) to do what you need | |
# | |
from PIL import Image | |
# | |
# It's convenient to import everything from colorzero | |
# so you don't have to put "colorzero." before everything | |
# | |
from colorzero import * | |
# | |
# Load an image from the disk into the Pillow Image object | |
# | |
image = Image.open("abbey.jpg") | |
# | |
# Go across all the columns and down all the rows | |
# of the image | |
# | |
for x in range(image.width): | |
for y in range(image.height): | |
coord = (x, y) | |
# | |
# For each coordinate, construct a Color object | |
# from the pixel's RGB value | |
# | |
rgb = image.getpixel(coord) | |
pixel = Color(rgb) | |
# | |
# Apply a 75% saturation to the colour and | |
# give the new colour a new name | |
# | |
desat = pixel * Saturation(0.75) | |
# | |
# Apply that new colour to the same pixel | |
# (you need to use .rgb_bytes to get the | |
# 0-255 colours; if you just use .rgb, you'll | |
# get RGB values between 0 and 1 | |
# | |
image.putpixel(coord, desat.rgb_bytes) | |
# | |
# Save the altered image under a new name | |
# | |
image.save("abbey.desat.jpg") |
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
# | |
# You nearly always just need to import Image | |
# from PIL(low) to do what you need | |
# | |
from PIL import Image | |
# | |
# It's convenient to import everything from colorzero | |
# so you don't have to put "colorzero." before everything | |
# | |
from colorzero import * | |
# | |
# Load an image from the disk into the Pillow Image object | |
# | |
image = Image.open("abbey.jpg") | |
# | |
# Go across all the columns and down all the rows | |
# of the image | |
# | |
for x in range(image.width): | |
for y in range(image.height): | |
coord = (x, y) | |
# | |
# For each coordinate, construct a Color object | |
# from the pixel's RGB value | |
# | |
rgb = image.getpixel(coord) | |
pixel = Color(rgb) | |
# | |
# Apply a 75% saturation to the colour and | |
# give the new colour a new name | |
# | |
desat = pixel * Saturation(0.75) | |
# | |
# Apply that new colour to the same pixel | |
# (you need to use .rgb_bytes to get the | |
# 0-255 colours; if you just use .rgb, you'll | |
# get RGB values between 0 and 1 | |
# | |
image.putpixel(coord, desat.rgb_bytes) | |
# | |
# Save the altered image under a new name | |
# | |
image.save("abbey.desat.jpg") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment