8

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,

Community
  • 1
  • 1
FurryAlchemist
  • 103
  • 1
  • 6
  • You're getting the error because `ImageFile.save()` is returning `None`, and you're trying to access the `0` and `1`st elements of `None`. Have you tried removing everything after `save()` from the last two lines? – sundance Jan 24 '14 at 19:30
  • I did, and it will save the First Page, or Invoice # 1, but because there are multiple invoices (Page 1 is Invoice 1, Page 2 is Invoice 2 for example) I am attempting to save Page 1 as one filename and Page 2 as a different filename – FurryAlchemist Jan 24 '14 at 19:36
  • But why are you trying to access the value returned by ImageFile.save()? – sundance Jan 24 '14 at 19:37
  • Honestly, just trying anything that I can, I had Tried ImageFile[0].save(), ImageFile.save[0]() and ImageFile.save()[0] to try it every way that I could, this just happened to be the most recent version. – FurryAlchemist Jan 24 '14 at 19:38
  • It's probably going to be difficult to split a tif into "pages," since the page height is really set by the printer/printer handler. You're probably better off cropping the image into multiple sections by a number of pixels (whatever the height of a "page" is), and then save those cropped versions. – sundance Jan 24 '14 at 19:42
  • See, that's the weird thing, The file is already two separate images, I can flip back and forth between the pages in Windows Photo Viewer – FurryAlchemist Jan 24 '14 at 19:45
  • Can you supply a link to a copy of the multipage .TIF? – martineau Jan 25 '14 at 19:58
  • ImageFile.size[0] is width and ImageFile.size[1] is height for the first page. You are not accessing different pages here. – MatthieuW Jan 27 '14 at 15:12

1 Answers1

9

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
MatthieuW
  • 2,292
  • 15
  • 25
  • How would you find the number of frames in a Tiff stack ? `img.info` doesn't give anything. – MostafaMV May 29 '14 at 19:34
  • 2
    For number of frames, use `n_frames` as described here: https://stackoverflow.com/questions/26660415/pillow-and-multi-page-tiffs – David C Oct 26 '18 at 19:00