Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split .TIF file using PIL

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,

like image 633
FurryAlchemist Avatar asked Jan 24 '14 19:01

FurryAlchemist


People also ask

How do I split a TIF file?

❓ How can I split TIFF document? First, you need to add a file for split: drag & drop your TIFF file or click inside the white area for choose a file. Then click the 'Split' button. When split TIFF document is completed, you can download your result files.

Is .TIF an editable file?

tiff. A TIFF is an image file — so it stores photographic and graphical data. You won't be able to edit the original file information without editing software, like Adobe Photoshop or Illustrator.

Can TIFF files have multiple pages?

Multi-Page Files. Some image file types can only hold a single image - for example BMP and JPEG files. Other image file types can hold multiple images - for example TIFF and DICOM files.


1 Answers

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
like image 186
MatthieuW Avatar answered Oct 01 '22 07:10

MatthieuW