Skip to content

Exception when converting GIF to RGB and saving #2803

Closed

Description

@tisdall

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

Activity

  1. hugovk commented on Oct 17, 2017

    @hugovk
    Member

    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

  2. tisdall commented on Oct 17, 2017

    @tisdall
    Author

    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?

  3. changed the title [-]exception when converting GIF to RGB and saving[/-] [+]Exception when converting GIF to RGB and saving[/+] on Dec 24, 2017
  4. aclark4life commented on Apr 1, 2018

    @aclark4life
    Member

    @hugovk Is this a bug?

  5. added
    BugAny unexpected behavior, until confirmed feature.
    on Apr 1, 2018
  6. modified the milestones: Future, on Apr 1, 2018
  7. kkopachev commented on Apr 3, 2018

    @kkopachev
    Contributor

    is this the same? #2704

  8. radarhere commented on Sep 7, 2018

    @radarhere
    Member

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

  9. radarhere commented on Dec 26, 2018

    @radarhere
    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.

  10. radarhere commented on Jun 27, 2021

    @radarhere
    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.

  11. radarhere commented on Jun 28, 2021

    @radarhere
    Member

    #5552 resolves this.
    plus_thumb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugAny unexpected behavior, until confirmed feature.GIFPalette

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions