-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_blue.py
More file actions
51 lines (41 loc) · 1.74 KB
/
max_blue.py
File metadata and controls
51 lines (41 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Import necessary image processing library
# Documentation: https://pillow.readthedocs.io/en/stable/index.html
from PIL import Image
# WRITE TOGETHER
def max_blue(image):
"""Sets the blue component of the RGB color of every pixel in the picture to the maximum value. An example of manipulating one component of the color of a pixel."""
# load the pixel data into an accessible format
pixels = image.load()
width = image.width # get image width in pixels
#print(width) # 900px
height = image.height # get image height in pixels
#print(height) # 600px
# iterate through (access) each pixel in the image
for x in range(width):
for y in range(height):
# a pixel is represented by a tuple of RGB values
# e.g. (0,255,0) is a fully green pixel
# access each pixel's color tuple
color_tuple = pixels[x, y]
# unpack each color_tuple into its RGB values
r, g, b = color_tuple
if x == 0 and y == 0:
print(r,g,b) # see top-left pixel at 0,0
# Modifies the pixel at the given x,y position, replacing it with a new rgb color tuple
image.putpixel((x, y), (r, g, 255))
# save the new image to the file tree as a PNG file
image.save("max_blue.png", "png")
# WRITE ON YOUR OWN - NO COPY/PASTE!
def max_red(image):
"""Sets the red component of the RGB color of every pixel in the picture to the maximum value."""
pass
def max_green(image):
"""Sets the green component of the RGB color of every pixel in the picture to the maximum value."""
pass
# MAIN CODE
# open source image and convert to RGB format (if it wasn't already)
orig_image = Image.open("huskie.jpg").convert('RGB')
# call a function and pass to it the original image (only call one function at a time)
max_blue(orig_image)
#max_red(orig_image)
#max_green(orig_image)