首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用numpy扩展维度后无法返回到PIL图像

使用numpy扩展维度后无法返回到PIL图像
EN

Stack Overflow用户
提问于 2019-02-07 04:23:07
回答 2查看 924关注 0票数 0

我需要向PIL图像添加一个维度,以便它被我正在使用的库接受为输入,但它给了我一个错误。代码如下:

代码语言:javascript
复制
import numpy as np
from PIL import Image

printscreen = Image.open('datasets/custom/spaceship.jpg').convert('RGB')
printscreen = np.uint8(np.expand_dims(np.array(printscreen), axis=0))
printscreen = Image.fromarray(printscreen)

它在最后一行写道:'TypeError: Cannot handle this data type‘。

EN

回答 2

Stack Overflow用户

发布于 2019-02-07 05:15:17

2解决方案:

编号1:(你只需要alpha通道)只需将其转换为(你会得到第四个通道)

代码语言:javascript
复制
printscreen = Image.open('some_image.jpg').convert('RGBA')

2:对扩展维度采取更加灵活和普遍的态度

2)创建用零填充的2D“灰度”图像

3)将原始3D图像与“灰度”图像连接起来

4)结果是4个通道的图像,其中一个通道设置为0

代码语言:javascript
复制
import numpy as np
from PIL import Image

printscreen = Image.open('some_image.jpg').convert('RGB')
temp = np.asarray(printscreen)
shp = temp.shape
printscreen = np.concatenate((temp,np.zeros(shape=[shp[0],shp[1],1])),axis=2)

printscreen = Image.fromarray(printscreen.astype(np.uint8))

代码语言:javascript
复制
>>> printscreen.shape # before transformed with Image.fromarray
(397, 397, 4)

编辑-将矩阵放在自身上的一般态度:

如果你想要更多的图片在它们上面:

(与解决方案2相同,但更具表现力)

代码语言:javascript
复制
img1 = np.zeros(shape = [10,10,3])
img2 = np.zeros(shape = [10,10,3])
img_1_2 = np.concatenate((img1,img2),axis = 2)

>>> img_1_2.shape
(10, 10, 6)

编辑2:

仅供您参考,您得到错误的原因是因为您试图将numpy数组转换为非图像格式的PIL Image。形状是问题所在-你有类似于(x,y,3,1)的东西。函数np.expand_dims做了它所说的-它增加了新的维度,这意味着,你实际上是在从图像中做一部电影/视频。灰度图像是2维的,RGB或RGBA是3维的,电影是4维的,其中4维是时间(或者更准确地说是图像序列)。从RGB到RGBA的转换并没有扩展维度,而是扩展了体积,或者更清楚地说,它将颜色维度从长度3扩展到长度4

EDIT 3:你真的想扩展到第四个维度,在那里额外的维度将被新的图像填充

此解决方案不计入PIL,因为PIL与此无关

代码语言:javascript
复制
import numpy as np


class ImageContainer(object):
    def __init__(self,first_image):
        self.container =  np.uint8(np.expand_dims(np.array(first_image), axis=0))

    def add_image(self,image):
        print(image.shape)
        temp = np.uint8(np.expand_dims(np.array(image), axis=0))
        print(temp.shape)
        self.container  = np.concatenate((self.container,temp),axis = 0)
        print(self.container.shape)

    def save_all(self,name):
        np.save(name,self.container)


img1 = np.zeros(shape = [10,10,3]) # RGB
img2 = np.zeros(shape = [10,10,3]) # RGB

cont = ImageContainer(img1)
cont.add_image(img2)

>>
(1, 10, 10, 3) # 1 image
(2, 10, 10, 3) # 2 images
票数 1
EN

Stack Overflow用户

发布于 2019-02-07 04:39:33

下面是我之前使用过的一些图像处理代码的改编。我不确定这是不是最好的方法,也不知道您正在寻找什么,但它在我将PIL图像转换为数组和数组的过程中很有效

代码语言:javascript
复制
import numpy as np
from PIL import Image

print_screen = Image.open('datasets/custom/spaceship.jpg').convert('RGB')
x, y = print_screen.size
print('Converting from Image to Array')
color = np.zeros((x, y, 3), dtype=np.uint8) # initialize zeros array
for i in range(0, x): # fill the array
    for j in range(0, y):
        color[i][j] = print_screen.getpixel((i, j)) #input RGB to array
print_screen = Image.fromarray(color)

希望这篇文章能帮到你!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54561953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档