Reputation: 13025
I am trying to transform images from gif format to png format. This is how I did
def _gif_to_png(gif_dir, png_dir):
img_names = tf.gfile.ListDirectory(gif_dir)
for f in img_names:
# get the filename without the extension
basename = os.path.basename(f).split(".")[0]
im = Image.open(os.path.join(gif_dir,f))
transparency = im.info['transparency']
png_file_name = os.path.join(png_dir, basename+'.png')
im.save(png_file_name, transparency=transparency)
png_names = tf.gfile.ListDirectory(png_dir)
However, I got the following error message
transparency = im.info['transparency']
KeyError: 'transparency'
What might be the problem and how to fix it?
Upvotes: 2
Views: 78
Reputation: 22457
That happens when your GIF does not contain transparency.
transparency
Transparency color index. This key is omitted if the image is not transparent.
(http://pillow.readthedocs.io/en/5.1.x/handbook/image-file-formats.html#gif)
How do you know if a GIF contains transparent pixels? Apparently, the way you did – by testing if that key exists. You can directly test it, surrounded by a try..except
, or with
if 'transparency' in im.info.dict():
.. do stuff ..
Upvotes: 2