python 图片转数组_将图像实例(文件)转换为数组(Python)

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 . 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

解决方案

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值