Answers for "python how to get pixel values from image"

1

python how to get pixel values from image

from PIL import Image

def get_image(image_path):
    image = Image.open(image_path).convert("L")
    pixel_values = list(image.getdata())

    return pixel_values
Posted by: Guest on April-21-2022
0

python how to get pixel values from image

# 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)
Posted by: Guest on April-21-2022

Code answers related to "python how to get pixel values from image"

Python Answers by Framework

Browse Popular Code Answers by Language