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

Exception when converting GIF to RGB and saving #2803

Closed
tisdall opened this issue Oct 17, 2017 · 8 comments · Fixed by #5552
Closed

Exception when converting GIF to RGB and saving #2803

tisdall opened this issue Oct 17, 2017 · 8 comments · Fixed by #5552
Labels
Bug Any unexpected behavior, until confirmed feature. GIF Palette
Projects
Milestone

Comments

@tisdall
Copy link

tisdall commented Oct 17, 2017

I've reduced the issue down to 2 lines of code:

from PIL import Image
Image.open('plus.gif').convert('RGB').save('plus_thumb.gif', format='GIF')

The image with the issue:
plus

The exception I get:

# python test5.py 
/usr/local/lib/python2.7/site-packages/PIL/Image.py:967: UserWarning: Couldn't allocate palette entry for transparency
  "for transparency")
Traceback (most recent call last):
  File "test5.py", line 8, in <module>
    Image.open('plus.gif').convert('RGB').save('plus_thumb.gif', format='GIF')
  File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 1928, in save
    save_handler(self, fp, filename)
  File "/usr/local/lib/python2.7/site-packages/PIL/GifImagePlugin.py", line 465, in _save
    _write_single_frame(im, fp, palette)
  File "/usr/local/lib/python2.7/site-packages/PIL/GifImagePlugin.py", line 383, in _write_single_frame
    _write_local_header(fp, im, (0, 0), flags)
  File "/usr/local/lib/python2.7/site-packages/PIL/GifImagePlugin.py", line 490, in _write_local_header
    transparency = int(transparency)
TypeError: int() argument must be a string or a number, not 'tuple'

This is with Pillow 4.3.0 and Python 2.7.7

@hugovk
Copy link
Member

hugovk commented Oct 17, 2017

Running with Python 3 it prints a warning:

/Users/hugo/github/Pillow/PIL/Image.py:967: UserWarning: Couldn't allocate palette entry for transparency
  "for transparency")
Traceback (most recent call last):
  File "2803.py", line 2, in <module>
    Image.open('plus.gif').convert('RGB').save('plus_thumb.gif', format='GIF')
  File "/Users/hugo/github/Pillow/PIL/Image.py", line 1928, in save
    save_handler(self, fp, filename)
  File "/Users/hugo/github/Pillow/PIL/GifImagePlugin.py", line 465, in _save
    _write_single_frame(im, fp, palette)
  File "/Users/hugo/github/Pillow/PIL/GifImagePlugin.py", line 383, in _write_single_frame
    _write_local_header(fp, im, (0, 0), flags)
  File "/Users/hugo/github/Pillow/PIL/GifImagePlugin.py", line 490, in _write_local_header
    transparency = int(transparency)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

Adding some prints:

from PIL import Image
im = Image.open('plus.gif')

print()
print(im)
print(im.info)
print()

im = im.convert('RGB')

print()
print(im)
print(im.info)
print()

im.save('plus_thumb.gif', format='GIF')

Shows the transparency tuple:

<PIL.GifImagePlugin.GifImageFile image mode=P size=559x600 at 0x10EAFD358>
{'version': b'GIF89a', 'background': 0, 'comment': b'G44', 'transparency': 255, 'duration': 0}


<PIL.Image.Image image mode=RGB size=559x600 at 0x10EAFD320>
{'version': b'GIF89a', 'background': 0, 'comment': b'G44', 'transparency': (85, 0, 0), 'duration': 0}

Putting in a del im.info['transparency'] before the save gives this:
plus_thumb

@tisdall
Copy link
Author

tisdall commented Oct 17, 2017

Right above the warning code is the following: del(new.info['transparency'])

Why is that not fixing the issue? Does the warning cause the image with the deleted transparency to not be returned and the used later?

@radarhere radarhere changed the title exception when converting GIF to RGB and saving Exception when converting GIF to RGB and saving Dec 24, 2017
@aclark4life
Copy link
Member

@hugovk Is this a bug?

@aclark4life aclark4life added the Bug Any unexpected behavior, until confirmed feature. label Apr 1, 2018
@aclark4life aclark4life modified the milestones: Future, 5.0.1 Apr 1, 2018
@kkopachev
Copy link
Contributor

is this the same? #2704

@radarhere
Copy link
Member

I have created PR #3187 to address this, which has the effect of correctly applying del(new.info['transparency']).

@radarhere radarhere added the GIF label Sep 30, 2018
@radarhere
Copy link
Member

With #3187 merged, the TypeError is now fixed. The image is now output as in the earlier comment by @hugovk. If the UserWarning 'Couldn't allocate palette entry for transparency' is accepted behaviour, then this issue is resolved.

@aclark4life aclark4life added this to Backlog in Pillow May 11, 2019
@aclark4life aclark4life moved this from Backlog to Icebox in Pillow May 11, 2019
@radarhere
Copy link
Member

There's a fundamental problem with this situation. Outside of the plus symbol, color index is 255, the transparency. Inside of the plus symbol, the color index is 0.

Both of those entries are (0, 0, 0) in the palette though. So once you convert to RGB, they become the same, and we're not able to get the original image back again. This is a limitation of converting P to RGB.

So I think the correct output of this code should an empty plus symbol.

@radarhere
Copy link
Member

#5552 resolves this.
plus_thumb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Any unexpected behavior, until confirmed feature. GIF Palette
Projects
Pillow
  
Closed
Development

Successfully merging a pull request may close this issue.

5 participants