简体   繁体   English

如何从 RGBA 像素数组构造 PIL 图像?

[英]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.我的目标是使用 PIL 从图像中提取一些细节,有效地裁剪它。
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.为此,我使用Image.getdata()来获取图像中的像素列表,因为检查和修改这对我来说更容易。

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:我在 PIL 文档中看到了一些有趣的东西,即fromarray类方法,但是将数组传递给这个 function 会给出错误消息:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python39\lib\site-packages\PIL\Image.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?最后的问题是,我将如何 go 将该数组转换为 PIL 图像 object 以便以后保存?

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 图像大小:(4、4)

pippo image mode: L: (8-bit pixels, black and white) pippo图像模式:L:(8位像素,黑白)

Apparently your array is not a RGBA pixel array??显然您的阵列不是 RGBA 像素阵列? Or not ?或不 ?

using my new_imageRGBA or new_imageRGBA2 array see above I got:使用我的 new_imageRGBA 或 new_imageRGBA2 数组见上面我得到:

pippoRGBA image size: (4, 1) pippoRGBA 图像大小:(4, 1)

pippoRGBA image mode: RGBA pippoRGBA 图像模式:RGBA

or (pippoRGBA2 image):或(pippoRGBA2 图像):

pippoRGBA2 image size: (2, 2) pippoRGBA2 图像大小:(2, 2)

pippoRGBA2 image mode: RGBA pippoRGBA2 图像模式:RGBA

note that:注意:

pippoRGBA = Image.fromarray(new_pixelsRGBA)

works as well;也可以; PIL knows we are talking about RGBA array PIL 知道我们在谈论 RGBA 数组

u can use matplotlib here你可以在这里使用 matplotlib

import matplotlib.pyplot as plt
plt.imshow(img_pixels)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM