Split .TIF file using PIL

懵懂的女人 提交于 2019-12-09 10:17:08

问题


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.

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__'

Any Suggestions?

Thank you in advance,


回答1:


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


来源:https://stackoverflow.com/questions/21340740/split-tif-file-using-pil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!