使用PIL分割.TIF文件 [英] Split .TIF file using PIL

查看:430
本文介绍了使用PIL分割.TIF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了使用python拆分多页tiff 拆分.TIFF文件的文件,但是,老实说,我还没有完全理解答案,我希望得到一些澄清.

I took a look at the Split multi-page tiff with python file for Splitting a .TIFF File, however to be honest, I didn't fully understand the answers, and I'm hoping for a little clarification.

我正在尝试获取其中包含多个发票的.Tif文件,并将其拆分为每个页面,然后将其压缩并上传到数据库中. PIL已安装在将要运行该程序的计算机上,因此我想坚持使用PIL库.我知道我可以在打开PIL后使用PIL查看诸如每个图像的大小之类的信息,但是当我尝试保存每个图像时,它变得很简单. (下面的示例代码)

I am attempting to take a .Tif file with multiple Invoices in it and Split it into each page which will then be Zipped Up and uploaded into a database. PIL is installed on the computers that will be running this program, as such I'd like to stick with the PIL Library. I know that I can view information such as the Size of each Image using PIL after it's open, however when I attempt to Save each it gets dicey. (Example Code Below)

def Split_Images(img,numFiles):
    ImageFile = Image.open(img)
    print ImageFile.size[0]
    print ImageFile.size[1]
    ImageFile.save('InvoiceTest1.tif')[0]
    ImageFile.save('InvoiceTest2.tif')[1]

但是,当我运行此代码时,出现以下错误:

However when I run this code I get the following Error:

TypeError: 'NoneType' object has no attribute '__getitem__'

有什么建议吗?

先谢谢您

推荐答案

您需要PIL图像搜索"方法来访问不同的页面.

You need the PIL Image "seek" method to access the different pages.

from PIL import Image

img = Image.open('multipage.tif')

for i in range(4):
    try:
        img.seek(i)
        img.save('page_%s.tif'%(i,))
    except EOFError:
        break

这篇关于使用PIL分割.TIF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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