将numpy.array对象转换为PIL图像对象 [英] Convert numpy.array object to PIL image object

查看:243
本文介绍了将numpy.array对象转换为PIL图像对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Image.fromarray将numpy数组转换为PIL图像,但是它显示以下错误.

I have been trying to convert a numpy array to PIL image using Image.fromarray but it shows the following error.

回溯(最近一次通话最近):文件"C:\ Users \ Shri1008 Saurav Das \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ PIL \ Image.py", 第2428行,在fromarray中 模式,rawmode = _fromarray_typemap [typekey] KeyError:((1、1、3062),'| u1')

Traceback (most recent call last): File "C:\Users\Shri1008 Saurav Das\AppData\Local\Programs\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2428, in fromarray mode, rawmode = _fromarray_typemap[typekey] KeyError: ((1, 1, 3062), '|u1')

在处理上述异常期间,发生了另一个异常:

During handling of the above exception, another exception occurred:

回溯(最近通话最近一次):文件"C:/Users/Shri1008 Saurav Das/AppData/Local/Programs/Python/Python36-32/projects/try.py,第 13,在 img = Image.fromarray(IMIR)文件"C:\ Users \ Shri1008 Saurav Das \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ PIL \ Image.py", 第2431行,在fromarray中 引发TypeError(无法处理此数据类型")TypeError:无法处理此数据类型

Traceback (most recent call last): File "C:/Users/Shri1008 Saurav Das/AppData/Local/Programs/Python/Python36-32/projects/try.py", line 13, in img = Image.fromarray(IMIR) File "C:\Users\Shri1008 Saurav Das\AppData\Local\Programs\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2431, in fromarray raise TypeError("Cannot handle this data type") TypeError: Cannot handle this data type

我从hdf5文件中提取了矩阵并将其转换为numpy数组.然后,我做了一些基本的转换以增强对比度(最可能的错误原因).这是代码.

I extracted the matrix from an hdf5 file and converted it to a numpy array. I then did some basic transformations to enhance contrast(most probable reason for error). Here is the code.

import tkinter as tk
import h5py as hp
import numpy as np
from PIL import Image, ImageTk

hf = hp.File('3RIMG_13JUL2018_0015_L1C_SGP.h5', 'r')
IMIR = hf.get('IMG_MIR')
IMIR = np.uint8(np.power(np.double(np.array(IMIR)),4)/5000000000)
IMIR = np.array(IMIR)

root = tk.Tk()
img = Image.fromarray(IMIR)
photo = ImageTk.PhotoImage(file = img)
cv = tk.Canvas(root, width=photo.width(), height=photo.height())
cv.create_image(1,1,anchor="nw",image=photo)

我正在Windows 10上运行Python 3.6.请帮助.

I am running Python 3.6 on Windows 10. Please help.

推荐答案

问题是数据的形状.枕头的fromarray函数只能执行MxNx3阵列(RGB图像)或MxN阵列(灰度).要使灰度图像起作用,您必须将MxNx1阵列转换为MxN阵列.您可以使用np.reshape()函数来完成此操作.这样会将数据弄平,然后将其放入其他数组形状中.

The problem is the shape of your data. Pillow's fromarray function can only do a MxNx3 array (RGB image), or an MxN array (grayscale). To make the grayscale image work, you have to turn you MxNx1 array into a MxN array. You can do this by using the np.reshape() function. This will flatten out the data and then put it into a different array shape.

IMIR = IMIR.reshape(M, N) #let M and N be the dimensions of your image

(在img = Image.fromarray(IMIR)之前添加)

这篇关于将numpy.array对象转换为PIL图像对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆