from PIL import Image
def get_image(image_path):
image = Image.open(image_path).convert("L")
pixel_values = list(image.getdata())
return pixel_values
# Third party modules
import numpy
from PIL import Image
def get_image(image_path):
"""Get a numpy array of an image so that one can access values[x][y]."""
image = Image.open(image_path, "r")
width, height = image.size
pixel_values = list(image.getdata())
if image.mode == "RGB":
channels = 3
elif image.mode == "L":
channels = 1
else:
print("Unknown mode: %s" % image.mode)
return None
pixel_values = numpy.array(pixel_values).reshape((width, height, channels))
return pixel_values
image = get_image("gradient.png")
print(image[0])
print(image.shape)
from PIL import Image
def change_image_pixel_values(imaged_path,by_value):
# Import an image from directory:
input_image = Image.open(imaged_path)
# Extracting pixel map:
pixel_map = input_image.load()
# Extracting the width and height
# of the image:
width, height = input_image.size
# taking half of the width:
for i in range(width):
for j in range(height):
# getting the RGB pixel value.
red, green, blue, p = input_image.getpixel((i, j))
red -= by_value
green -= by_value
blue -= by_value
# setting the pixel value.
pixel_map[i, j] = (red,green,blue)
# Saving the final output
# as "grayscale.png":
input_image.save(imaged_path, format="png")