AttributeError: 'tuple' object has no attribute 'ljust' when trying to save image #2278

Open
dtenenba opened this Issue Dec 6, 2016 · 1 comment

Projects

None yet

2 participants

@dtenenba
dtenenba commented Dec 6, 2016

What did you do?

Ran a snippet of code, see below.

What did you expect to happen?

The script to complete without error.

What actually happened?

Stack trace, see below.

What versions of Pillow and Python are you using?

Pillow==3.4.2
(also fails with Pillow==3.5.0.dev0)
Python 3.5.2

Please include code that reproduces the issue and whenever possible, an image that demonstrates the issue. The best reproductions are self-contained scripts with minimal dependencies. If you are using a framework such as plone, django, or buildout, try to replicate the issue just using Pillow.

from io import BytesIO
from contextlib import closing
from PIL import Image

image = Image.open('phototest.tif')
print(image)

with closing(BytesIO()) as f:
    image.save(f, image.format or 'JPEG')
    value = f.getvalue()
    

Where the image can be found here, or download it with this command:

curl  "https://github.com/tesseract-ocr/tesseract/blob/master/testing/phototest.tif?raw=true" > phototest.tif

The output is:

<PIL.TiffImagePlugin.TiffImageFile image mode=1 size=640x480 at 0x7F37D8546E10>
Traceback (most recent call last):
File "try.py", line 9, in
image.save(f, image.format or 'JPEG')
File "/usr/local/lib/python3.5/dist-packages/PIL/Image.py", line 1698, in save
save_handler(self, fp, filename)
File "/usr/local/lib/python3.5/dist-packages/PIL/TiffImagePlugin.py", line 1487, in _save
offset = ifd.save(fp)
File "/usr/local/lib/python3.5/dist-packages/PIL/TiffImagePlugin.py", line 758, in save
entries.append((tag, typ, count, data.ljust(4, b"\0"), b""))
AttributeError: 'tuple' object has no attribute 'ljust'

@wiredfool wiredfool added the Tiff label Dec 6, 2016
@wiredfool
Member

Minimal repro is:

from PIL import Image
im = Image.open('phototest.tif')
im.save('tmp.tif')

It's 3.x only, this works in 2.7. It succeeded before the great metadata rewrite of Pillow 3.0.
Error is in a metadata entry:

Tag 34377, Type: 1, Value: ("8BIM\x03\xed\x00\x00\x00\x00\x00\x10\x00\xc8\x00\x00\x00\x01\x00\x01\x00\xc8\x00\x00\x00\x01\x00\x018BIM\x03\xf3\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x008BIM'\x10\x00\x00\x00\x00\x00\n\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02",)
save: PhotoshopInfo (34377) - type: byte (1) - value: <table: 70 bytes>

and in 3.5:

Tag 34377, Type: 1, Value: ((b"8BIM\x03\xed\x00\x00\x00\x00\x00\x10\x00\xc8\x00\x00\x00\x01\x00\x01\x00\xc8\x00\x00\x00\x01\x00\x018BIM\x03\xf3\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x008BIM'\x10\x00\x00\x00\x00\x00\n\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02",),)
save: PhotoshopInfo (34377) - type: byte (1) - value: ((b"8BIM\x03\xed\x00\x00\x00\x00\x00\x10\x00\xc8\x00\x00\x00\x01\x00\x01\x00\xc8\x00\x00\x00\x01\x00\x018BIM\x03\xf3\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x008BIM'\x10\x00\x00\x00\x00\x00\n\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02",),)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment