You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Exception when converting GIF to RGB and saving #2803
# 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'
/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'
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.
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.
Activity
hugovk commentedon Oct 17, 2017
Running with Python 3 it prints a warning:
Adding some prints:
Shows the transparency tuple:
Putting in a

del im.info['transparency']before the save gives this:tisdall commentedon 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?
[-]exception when converting GIF to RGB and saving[/-][+]Exception when converting GIF to RGB and saving[/+]aclark4life commentedon Apr 1, 2018
@hugovk Is this a bug?
kkopachev commentedon Apr 3, 2018
is this the same? #2704
radarhere commentedon Sep 7, 2018
I have created PR #3187 to address this, which has the effect of correctly applying
del(new.info['transparency']).radarhere commentedon Dec 26, 2018
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.
radarhere commentedon Jun 27, 2021
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 commentedon Jun 28, 2021
#5552 resolves this.
