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

后端 未结 2 1236
感情败类
感情败类 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:28

    You can convert the image data into a Python list (or list-of-lists) like this:

    from PIL import Image
    
    img = Image.open('eggs.png').convert('L')  # convert image to 8-bit grayscale
    WIDTH, HEIGHT = img.size
    
    data = list(img.getdata()) # convert image data to a list of integers
    # convert that to 2D list (list of lists of integers)
    data = [data[offset:offset+WIDTH] for offset in range(0, WIDTH*HEIGHT, WIDTH)]
    
    # At this point the image's pixels are all in memory and can be accessed
    # individually using data[row][col].
    
    # For example:
    for row in data:
        print(' '.join('{:3}'.format(value) for value in row))
    
    # Here's another more compact representation.
    chars = '@%#*+=-:. '  # Change as desired.
    scale = (len(chars)-1)/255.
    print()
    for row in data:
        print(' '.join(chars[int(value*scale)] for value in row))
    

    Here's an enlarged version of a small 24x24 RGB eggs.png image I used for testing:

    Here's the output from the first example of access:

    And here the output from the second example:

    @ @ % * @ @ @ @ % - . * @ @ @ @ @ @ @ @ @ @ @ @
    @ @ .   . + @ # .     = @ @ @ @ @ @ @ @ @ @ @ @
    @ *             . .   * @ @ @ @ @ @ @ @ @ @ @ @
    @ #     . .   . .     + % % @ @ @ @ # = @ @ @ @
    @ %       . : - - - :       % @ % :     # @ @ @
    @ #     . = = - - - = - . . = =         % @ @ @
    @ =     - = : - - : - = . .     . : .   % @ @ @
    %     . = - - - - : - = .   . - = = =   - @ @ @
    =   .   - = - : : = + - : . - = - : - =   : * %
    -   .   . - = + = - .   . - = : - - - = .     -
    =   . : : . - - .       : = - - - - - = .   . %
    %   : : .     . : - - . : = - - - : = :     # @
    @ # :   .   . = = - - = . = + - - = - .   . @ @
    @ @ #     . - = : - : = - . - = = : . .     # @
    @ @ %     : = - - - : = -     : -   . . .   - @
    @ @ *     : = : - - - = .   . - .   .     . + @
    @ #       . = - : - = :     : :   .   - % @ @ @
    *     . . . : = = - : . .   - .     - @ @ @ @ @
    *   . .       . : .   . .   - = . = @ @ @ @ @ @
    @ :     - -       . . . .     # @ @ @ @ @ @ @ @
    @ @ = # @ @ *     . .     . - @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ .   .   . # @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ -     . % @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ # . : % @ @ @ @ @ @ @ @ @ @ @ @ @
    

    Access to the pixel data should now be faster than using the object img.load() returns (and the values will be integers in the range of 0..255).

提交回复
热议问题