Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

python - How can I construct a PIL Image from an array of RGBA pixels?

My goal is to use PIL to extract some details from an image, effectively cropping it down.
For this, I use Image.getdata() to get a list of the pixels in the image, since checking and modifying this is easier for me.

After all the changes I made, I am left with an array of pixels represented in tuples. For simplicity, an array like that could look like this:

new_pixels = [
    (255, 0, 0, 255),
    (0, 255, 0, 255),
    (0, 0, 255, 255),
    (0, 0, 0, 255)
]

I've seen something interesting in the PIL documentation, namely the fromarray classmethod, however passing the array to this function gives an error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:Python39libsite-packagesPILImage.py", line 2741, in fromarray
    arr = obj.__array_interface__
AttributeError: 'list' object has no attribute '__array_interface__'

Trying the same with a two dimensional list gives the same result.

The question finally is, how would I go about turning this array into a PIL Image object that I can later save?

question from:https://stackoverflow.com/questions/65870933/how-can-i-construct-a-pil-image-from-an-array-of-rgba-pixels

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

not sure if thats what you are looking for:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb  4 17:10:02 2021

@author: Pietro
"""

from PIL import Image

import numpy as np


new_pixels = np.array([
    (255, 0, 0, 255),
    (0, 255, 0, 255),
    (0, 0, 255, 255),
    (0, 0, 0, 255)
]).astype('uint8')


new_pixelsRGBA = np.array([[
    [255, 0, 0, 255],
    [0, 255, 0, 255],
    [0, 0, 255, 255],
    [0, 0, 0, 255]]
]).astype('uint8')


new_pixelsRGBA2 = np.array([[
    [255, 0, 0, 255],
    [0, 255, 0, 255]],
    [[0, 0, 255, 255],
    [0, 0, 0, 255]]
]).astype('uint8')


pippo = Image.fromarray(new_pixels)

pippoRGBA = Image.fromarray(new_pixelsRGBA, mode='RGBA')
# pippoRGBA = Image.fromarray(new_pixelsRGBA)


print('pippo image size : ', pippo.size)
print('pippo image mode : ', pippo.mode)
pippo.show()

print('pippoRGBA image size : ', pippoRGBA.size)
print('pippoRGBA image mode : ', pippoRGBA.mode)
pippoRGBA.show()

pippoRGBA2 = Image.fromarray(new_pixelsRGBA2)

print('pippoRGBA2 image size : ', pippoRGBA2.size)
print('pippoRGBA2 image mode : ', pippoRGBA2.mode)
pippoRGBA2.show()

the image I got is:

pippo image size : (4, 4)

pippo image mode : L : (8-bit pixels, black and white)

Apparently your array is not a RGBA pixel array ?! Or not ?

using my new_imageRGBA or new_imageRGBA2 array see above I got:

pippoRGBA image size : (4, 1)

pippoRGBA image mode : RGBA

or (pippoRGBA2 image):

pippoRGBA2 image size : (2, 2)

pippoRGBA2 image mode : RGBA

note that :

pippoRGBA = Image.fromarray(new_pixelsRGBA)

works as well; PIL knows we are talking about RGBA array


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...