GIF image loses transparency

Bug #381379 reported by Stani
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Phatch
Fix Released
High
Nadia Alramli

Bug Description

When a transparent gif is saved, the transparency is lost. The solution is described here:
http://nadiana.com/pil-tips-converting-png-gif

For the user interface we should decide if we provide a check box or a drop down list with <transparency>, yes or no.

Stani (stani)
summary: - GIF image looses transparency
+ GIF image loses transparency
Changed in phatch:
assignee: nobody → Nadia Alramli (nadiana)
importance: Undecided → High
status: New → Confirmed
Revision history for this message
Erich (sophacles-gmail) wrote :

I think it should always default to preserving transparency. If there is a filetype conversion in the process, and the final filetype supports transparency (eg png->gif or gif -> png) it should be maintained. Further this transparency should be maintained from end to end, expecting each action to handle transparency correctly is an issue. Fortunately this seems easy enough.

Revision history for this message
Stani (stani) wrote :

Maybe we can add instead of "GIF Transparency" checkbox, a more general "Transparency" checkbox which is True by default. Phatch would behave like this:
1) Transparency = True
- gif: transparent =1 is passed to the save function
- png: normal pil save
2) Transparency = False:
- gif: normal pil save
- png: if image is rgba, it is converted first to rgb and then saved

Phatch is aimed also at not very graphic experienced people. They know what transparency is, but not what RGBA means.

Revision history for this message
Stani (stani) wrote :

Nadia, I looked at this bug myself as I realized that this would require a patch in the core as well. The info['transparency'] was not exposed yet, so I did that. Also as transparency is an index of a color (not a boolean), it doesn't make sense to add it to the UI. Especially because the save action has already an impressive list of parameters. This could be discussed again when Phatch can optionally show and hide parameters. I think the default behavior is fine for now.

$ bzr diff
=== modified file 'phatch/actions/save.py'
--- phatch/actions/save.py 2009-05-28 01:23:53 +0000
+++ phatch/actions/save.py 2009-05-29 13:12:07 +0000
@@ -28,7 +28,7 @@
 if DESKTOP_FOLDER == USER_FOLDER:
     DESKTOP_FOLDER = os.path.expanduser('~/phatch')

-
+GIF = ['gif']
 JPG = ['jpg','jpeg','jpe']
 PNG = ['png']
 TIF = ['tif','tiff']
@@ -114,6 +114,11 @@
             elif self.is_type(typ,TIF):
                 compression = self.get_field('TIFF Compression',info)
                 options['compression.tif'] = compression
+ elif self.is_type(typ,GIF):
+ try:
+ options['transparency'] = info['Pil.transparency']
+ except KeyError:
+ pass
             #save
             photo.save(filename, **options)
         return photo

=== modified file 'phatch/core/pil.py'
--- phatch/core/pil.py 2009-05-28 02:07:47 +0000
+++ phatch/core/pil.py 2009-05-29 12:56:34 +0000
@@ -219,6 +219,8 @@
         'Pil'+DOT+'Format' : image.format,
         'Pil'+DOT+'FormatDescription' : image.format_description,
     })
+ for key, value in image.info.items():
+ info['Pil'+DOT+key] = value
     return info

 def extract_info_pil(image):

$ bzr commit -m "fix gif transparency"
Committing to: /home/stani/sync/python/phatch/trunk/
modified phatch/actions/save.py
modified phatch/core/pil.py
Committed revision 616.

Changed in phatch:
assignee: Nadia Alramli (nadiana) → stani (stani)
status: Confirmed → Fix Committed
Revision history for this message
Nadia Alramli (nadiana) wrote :

Hi Stani,

Your fix will work fine when saving GIF images as GIF. But will not work if the user tries to save a non-palette based PNG image with transparency as GIF (which is a very likely scenario)

There is also another issue I was intending to fix. Currently if you save any type other than GIF as GIF the resulting colors will be dithered like this: http://nadiana.com/sites/default/files/pil_tips/mouse-bad.gif the fix is really easy just use the adaptive palette when converting to 'P'.

Nadia

Revision history for this message
Stani (stani) wrote :

Nadia, it is back in your hands. You asked for it ;-)

Changed in phatch:
assignee: stani (stani) → Nadia Alramli (nadiana)
status: Fix Committed → In Progress
Revision history for this message
Stani (stani) wrote :

$ bzr diff
=== modified file 'phatch/actions/save.py'
--- phatch/actions/save.py 2009-05-29 13:40:01 +0000
+++ phatch/actions/save.py 2009-06-03 03:02:58 +0000
@@ -114,8 +114,11 @@
             elif self.is_type(typ,TIF):
                 compression = self.get_field('TIFF Compression',info)
                 options['compression.tif'] = compression
- elif self.is_type(typ,GIF) and not(info['transparency'] is None):
- options['transparency'] = info['transparency']
+ elif self.is_type(typ,GIF):
+ photo.convert('P', palette=Image.ADAPTIVE)
+ info = photo.get_info()
+ if not info['transparency'] is None:
+ options['transparency'] = info['transparency']

             #save
             photo.save(filename, **options)

=== modified file 'phatch/core/pil.py'
--- phatch/core/pil.py 2009-05-29 16:56:48 +0000
+++ phatch/core/pil.py 2009-06-03 02:56:02 +0000
@@ -411,7 +411,17 @@
     def convert(self,mode,*args,**keyw):
         """Converts all layers to a different mode."""
         for layer in self.layers.values():
- layer.image = layer.image.convert(mode,*args,**keyw)
+ if mode == 'P' and layer.image.mode == 'RGBA':
+ alpha = layer.image.split()[3]
+ layer.image = layer.image.convert('RGB').convert(
+ mode, colors=255, *args, **keyw
+ )
+ layer.image.paste(
+ 255, Image.eval(alpha, lambda a: 255 if a <=128 else 0)
+ )
+ self.info['transparency'] = 255
+ else:
+ layer.image = layer.image.convert(mode,*args,**keyw)
         self.set_attr(_t('mode'),mode)

     def resize(self,size,method):

$ bzr commit -m "fix gif transparency"
Committing to: /home/stani/sync/python/phatch/trunk/
modified phatch/actions/save.py
modified phatch/core/pil.py
Committed revision 626.

Changed in phatch:
status: In Progress → Fix Committed
Stani (stani)
Changed in phatch:
milestone: none → 0.2.1
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.