在Python中使用Pi将数组转换为图像(tif)

2024-09-28 20:45:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将array转换为图像(tif)进行压缩(它将在另一端撤消)。然而,我在第一个栏位就摔倒了。。。在

我有以下内容:

pillow_image = Image.fromarray(image_data)

这给了我一个错误:

  File "/Users/workspace/test-app/env/lib/python2.7/site-packages/PIL/Image.py",

line 2155, in fromarray arr = obj.array_interface AttributeError: 'tuple' object has no attribute 'array_interface'

我做错什么了?在


Tags: test图像imagedata错误arrayusersworkspace
1条回答
网友
1楼 · 发布于 2024-09-28 20:45:09

image_data是由4个numpy数组组成的元组,每个数组(可能)都是形状(H,W)。你呢 需要image_data为单个形状数组(H,W,4)。因此,使用np.dstack组合通道。在

至少有一个数组具有dtype int32。但要将其用作8位彩色通道,数组必须是dtypeuint8(因此最大值为255)。可以使用astype将数组转换为数据类型uint8。希望您的数据不包含大于255的值。如果是这样,astype('uint8')将只保留最低有效位(即返回数字模256)。在

image_data = np.dstack(image_data).astype('uint8')
pillow_image = Image.fromarray(image_data)

相关问题 更多 >