Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't save multi-page TIFF as PDF #3267

Closed
darkdragon-001 opened this issue Jul 21, 2018 · 5 comments
Closed

Can't save multi-page TIFF as PDF #3267

darkdragon-001 opened this issue Jul 21, 2018 · 5 comments

Comments

@darkdragon-001
Copy link

I have a tiff image with multiple pages. tiffinfo gives the following output

TIFF Directory at offset 0x13384 (78724)
  Image Width: 2479 Image Length: 3504
  Resolution: 300, 300 pixels/inch
  Bits/Sample: 1
  Compression Scheme: CCITT Group 4
  Photometric Interpretation: min-is-white
  FillOrder: msb-to-lsb
  Orientation: row 0 top, col 0 lhs
  Samples/Pixel: 1
  Rows/Strip: 3504
  Planar Configuration: single image plane
  Page Number: 0-0
TIFF Directory at offset 0x2d38c (185228)
  Image Width: 2479 Image Length: 3504
  Resolution: 300, 300 pixels/inch
  Bits/Sample: 1
  Compression Scheme: CCITT Group 4
  Photometric Interpretation: min-is-white
  FillOrder: msb-to-lsb
  Orientation: row 0 top, col 0 lhs
  Samples/Pixel: 1
  Rows/Strip: 3504
  Planar Configuration: single image plane
  Page Number: 1-0

When I save it again as multi-page tiff, I can also view it without problems:

from PIL import Image
im = Image.open('input.tiff')
im.save('output.tiff', save_all=True)

When I save it as multi-page pdf, it has the correct number of pages, but a page size of 383201 x 0 mm.

from PIL import Image
path = 'input.tiff'
im = Image.open(path)
im.save('output.pdf', save_all=True)

When I save the pages separately as png, everything works without problems.

from PIL import Image, ImageSequence
im = Image.open('input.tiff')
for i, page in enumerate(ImageSequence.Iterator(im)):
    page.save("page%d.png" % i)

When I save the pages separately as pdf

from PIL import Image, ImageSequence
im = Image.open('input.tiff')
for i, page in enumerate(ImageSequence.Iterator(im)):
    page.save("page%d.pdf" % i)

an error is thrown

Traceback (most recent call last):
  File "./convert.py", line 31, in <module>
    page.save("page%d.pdf" % i)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1935, in save
    save_handler(self, fp, filename)
  File "/usr/lib/python3/dist-packages/PIL/PdfImagePlugin.py", line 177, in _save
    existing_pdf.write_obj(image_refs[pageNumber], stream=op.getvalue(),
IndexError: list index out of range

For copyright reasons, I can't include my test image. I verified that the code works in general, by using a multi-page tiff created via #2804. The format from that image is:

TIFF Directory at offset 0x8 (8)
  Image Width: 100 Image Length: 100
  Bits/Sample: 8
  Compression Scheme: None
  Photometric Interpretation: RGB color
  Samples/Pixel: 3
  Rows/Strip: 100
  Planar Configuration: single image plane
TIFF Directory at offset 0x75c8 (30152)
  Image Width: 100 Image Length: 100
  Bits/Sample: 8
  Compression Scheme: None
  Photometric Interpretation: RGB color
  Samples/Pixel: 3
  Rows/Strip: 100
  Planar Configuration: single image plane

So it uses 3 channels with 8 bits each whereas my sample usees a black/white 1 bit image and specifies additional attributes like resolution, compression, fill-order, orientation, page number.

I am using Pillow 5.1.0 with Python 3.6.5 on Ubuntu 18.04.

@radarhere
Copy link
Member

radarhere commented Jul 22, 2018

To provide a partial answer, the 'IndexError: list index out of range' issue was fixed in #3137, so if you update to Pillow 5.2.0, that should be fixed.

Are you able to provide the tiffinfo from the generated 383201 x 0 image?

@darkdragon-001
Copy link
Author

darkdragon-001 commented Jul 22, 2018

I can confirm that the IndexError is solved in 5.2.0. Although the dimensions of the single pdf pages are not the same as for the multi-page pdf (383201 x 0 mm). I just checked that the same problem exists for single-page tiffs of the same kind.

Are you able to provide the tiffinfo from the generated 383201 x 0 image?

Only the pdf export does not work. The other formats (bmp, jpg, png, gif) do work with the same code. tiffinfo does not work for pdf. Tell me another tool to produce similar output for pdf files.

@radarhere: I can also offer you to privately send you test files when you promise to not share them and destroy them afterwards.

@radarhere
Copy link
Member

radarhere commented May 4, 2019

Are you able to check if PR #3827 helped? It has been merged to master. It could be that this issue is really just the same as #1775

@darkdragon-001
Copy link
Author

@radarhere Indeed the issue is fixed in master now!

@hugovk
Copy link
Member

hugovk commented May 5, 2019

Thanks both!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants