0

Splitting of TIF file from and to specific pages in python

I am able to split multipage TIF file into pages sequential and create TIF file for each page using following python code suggested at Split .TIF file using PIL 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

My requirement is to split multipage.tif based on start and end page numbers and save it as different tif file. Eg. Create a file from page #3 to page #5 of multipage.tif. Can anyone please suggest a way to do this?

yatu
  • 86,083
  • 12
  • 84
  • 139

1 Answers1

2
from PIL import Image
img = Image.open('multipage.tif')
frames = []
for i in range(2, 5):
    img.seek(i)
    frames.append(img.copy())
frames[0].save('output.tif', save_all=True, append_images=frames[1:])
radarhere
  • 929
  • 8
  • 23