How to convert a grayscale image into a list of pixel values?

后端 未结 2 1234
感情败类
感情败类 2020-12-02 21:59

I am trying to create a python program which takes a grayscale, 24*24 pixel image file (I haven\'t decided on the type, so suggestions are welcome) and converts it to a list

2条回答
  •  孤城傲影
    2020-12-02 22:14

    You can access the greyscale value of each individual pixel by accessing the r, g, or b value, which will all be the same for a greyscale image.

    I.e.

    img = Image.open('eggs.png').convert('1')
    rawData = img.load()
    data = []
    for y in range(24):
        for x in range(24):
            data.append(rawData[x,y][0])
    

    This doesn't solve the problem of access speed.

    I'm more familiar with scikit-image than Pillow. It seems to me that if all you are after is listing the greyscale values, you could use scikit-image, which stores images as numpy arrays, and use img_as_ubyte to represent the image as a uint array, containing values between 0 and 255.

    Images are NumPy Arrays provides a good starting point to see what the code looks like.

提交回复
热议问题