将图像实例(文件)转换为数组(Python) [英] Converting an image instance (file) to an array (python)
问题描述
我有一个图像序列,我正在尝试在特定帧上运行脚本.我需要切换到此框架并将其转换为数组.但是,由于框架是实例<TiffImagePlugin.TiffImageFile image mode=I;16 size=512x512 at 0x104A0C998>
,我无法这样做.如何将该实例转换为数组?我已经使用过numpy.array,它不起作用.
谢谢!
I have an image sequence and I am trying to run a script on a specific frame. I need to switch to this frame and convert it to an array. However, I am unable to as the frame is an instance <TiffImagePlugin.TiffImageFile image mode=I;16 size=512x512 at 0x104A0C998>
. How can I convert this instance to an array? I have already used numpy.array and it does not work.
Thank you!
prot=Image.open("F23BN.tif")
for frame in ImageSequence.Iterator(dna):
if frame==16:
frame.convert('L')
print frame.mode, frame.format #I checked the format and it is still in the Instance format
推荐答案
据我所知,您正在尝试访问tiff图像中包含的二进制数据.正如Dav1d建议的那样,您可以在PIL中进行操作.我在python 2.6.5中测试了以下内容
As I understand your question, you are trying to get access to the binary data contained in a tiff image. As Dav1d suggested you can do it in PIL. I tested the following in python 2.6.5
import Image
import numpy
im = Image.open('Tiff.tif')
imarray = numpy.array(im)
print imarray.shape, im.size #these agree
对于更困难的方法,可以像打开任何其他文件一样打开文件.在代码段中,我假设您的文件不是太大,无法一次全部加载到内存中.
For a more difficult way to do this, you can open the file just as you would any other file. In the code snippet, I assume that your file isn't too big to just load into memory all at once.
im = open('Tiff.tif','r')
image_data = im.read()
im.close()
#lets look at the first few characters
for char in image_data[:64]:
print ord(char), #need to use ord so we can see the number
使用字符串image_data中的数据,您可以将其自由地转换为所需的任何其他数据类型.但是,这可能不会立即有用.首先,数据是二进制的.您将需要使用tiff规范对其进行解密.例如,前8个字节是标头.
With the data in the string image_data, you are free to turn it into any other data type you wish. However, this may not be useful right away. First of all the data is binary. You will need to decipher it using the tiff specification. For example, the first 8 bytes are a header.
更多详细信息: http://partners.adobe.com/public/developer/en/tiff /TIFF6.pdf
这篇关于将图像实例(文件)转换为数组(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!