我正在尝试在 Python 中读取二进制文件(8 位 RGB 元组),对其进行一些转换,然后将其写入为 png 图像。我正在执行以下操作:
typeinfo = np.dtype('>i1' ) #read single bytes
data=np.fromfile(("f%05d.txt" %(files[ctr])),dtype=typeinfo)
data=np.reshape(data,[linesperfile,resX,3]) #reshape to size/channels
如果我显示的类型信息data
它说:
<type 'numpy.ndarray'>
(512L, 7456L, 3L)
然后我对图像进行一些操作(就地),然后我想将图像写入文件。目前我使用:
import PIL.Image as im
svimg=im.fromarray(data)
svimg.save(("im%05d"%(fileno)),"png")
但它一直给我以下错误:
line 2026, in fromarray
raise TypeError("Cannot handle this data type")
TypeError: Cannot handle this data type
有什么想法如何做到这一点?
Image
needs unsigned bytes, i1
means signed字节。如果符号不相关(所有值都在 0 到 127 之间),那么这将起作用:
svimg=im.fromarray(data.astype('uint8'))
如果您需要 0-255 的全范围,您应该使用'uint8'
自始至终。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)