Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

GIF avec PIL

5 réponses
Avatar
François Couloigner
J'enregistre l'image
http://www.bcgsc.ca/about/resources/python-logo.gif, j'obtiens un
fichier de 2549 octets.


Je l'ouvre et le réenregistre avec le module PIL de python :

Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL.Image as IMG
>>> im = IMG.open("image.gif")
>>> im.save("python.gif")

J'obtiens un fichier 2 fois plus important (5 159 octets) et ce n'est
pas un cas particulier.
Comment l'éviter ?

5 réponses

Avatar
debimax
Le 16/03/2015 18:59, François Couloigner a écrit :
J'enregistre l'image
http://www.bcgsc.ca/about/resources/python-logo.gif, j'obtiens un
fichier de 2549 octets.


Je l'ouvre et le réenregistre avec le module PIL de python :

Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL.Image as IMG
>>> im = IMG.open("image.gif")
>>> im.save("python.gif")

J'obtiens un fichier 2 fois plus important (5 159 octets) et ce n'est
pas un cas particulier.
Comment l'éviter ?



Bonjour

Je viens de faire un test

In [2]: img = Image.open("photo.gif")
In [3]: img.save("photo2.gif")
In [4]: img = Image.open("photo2.gif")
In [5]: img.save("photo3.gif")
In [6]: ll photo.gif
-rw-r--r-- 1 jeanclaude 142661 mars 16 20:07 photo.gif
In [7]: ll photo2.gif
-rw-r--r-- 1 jeanclaude 169581 mars 16 20:18 photo2.gif
In [8]: ll photo3.gif
-rw-r--r-- 1 jeanclaude 169581 mars 16 20:18 photo3.gif


Je suppose donc que c'est l'entete qui change
Avatar
Nicolas
Le 16/03/2015 20:21, debimax a écrit :
Le 16/03/2015 18:59, François Couloigner a écrit :
J'enregistre l'image
http://www.bcgsc.ca/about/resources/python-logo.gif, j'obtiens un
fichier de 2549 octets.


Je l'ouvre et le réenregistre avec le module PIL de python :

Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL.Image as IMG
>>> im = IMG.open("image.gif")
>>> im.save("python.gif")

J'obtiens un fichier 2 fois plus important (5 159 octets) et ce n'est
pas un cas particulier.
Comment l'éviter ?



Bonjour

Je viens de faire un test

In [2]: img = Image.open("photo.gif")
In [3]: img.save("photo2.gif")
In [4]: img = Image.open("photo2.gif")
In [5]: img.save("photo3.gif")
In [6]: ll photo.gif
-rw-r--r-- 1 jeanclaude 142661 mars 16 20:07 photo.gif
In [7]: ll photo2.gif
-rw-r--r-- 1 jeanclaude 169581 mars 16 20:18 photo2.gif
In [8]: ll photo3.gif
-rw-r--r-- 1 jeanclaude 169581 mars 16 20:18 photo3.gif


Je suppose donc que c'est l'entete qui change



Une entête de 26ko, ça fait beaucoup...
Je dirais plutôt que le fichier généré n'est pas dans le même format que
le fichier d'origine. Voir cette page :
https://pillow.readthedocs.org/handbook/image-file-formats.html
Lire la "version" GIF du fichier donnera peut-être une réponse.
A première lecture, il n'y a pas d'option pour la méthode "save" en GIF.

Nicolas
Avatar
Benoit Izac
Bonjour,

le 16/03/2015 à 18:59, François Couloigner a écrit dans le message
<550719ee$0$3051$ :

J'enregistre l'image
http://www.bcgsc.ca/about/resources/python-logo.gif, j'obtiens un
fichier de 2549 octets.

Je l'ouvre et le réenregistre avec le module PIL de python :

Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
import PIL.Image as IMG
im = IMG.open("image.gif")
im.save("python.gif")







J'obtiens un fichier 2 fois plus important (5 159 octets) et ce n'est
pas un cas particulier.
Comment l'éviter ?



% file python-logo.gif python-logo-2.gif
python-logo.gif: GIF image data, version 89a, 211 x 71
python-logo-2.gif: GIF image data, version 87a, 211 x 71
% tail -n5 /usr/lib/python3.4/site-packages/PIL/GifImagePlugin.py
#
# Uncomment the following line if you wish to use NETPBM/PBMPLUS
# instead of the built-in "uncompressed" GIF encoder

# Image.register_save(GifImageFile.format, _save_netpbm)

(Je n'ai pas essayé)

Je suppose que l'on peut dire merci à Unisys et son brevet concernant
LZW sur ce coup là...

--
Benoit Izac
Avatar
François Couloigner
Le 17/03/2015 18:44, Benoit Izac a écrit :
Bonjour,

le 16/03/2015 à 18:59, François Couloigner a écrit dans le message
<550719ee$0$3051$ :

J'enregistre l'image
http://www.bcgsc.ca/about/resources/python-logo.gif, j'obtiens un
fichier de 2549 octets.

Je l'ouvre et le réenregistre avec le module PIL de python :

Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
import PIL.Image as IMG
im = IMG.open("image.gif")
im.save("python.gif")







J'obtiens un fichier 2 fois plus important (5 159 octets) et ce n'est
pas un cas particulier.
Comment l'éviter ?



% file python-logo.gif python-logo-2.gif
python-logo.gif: GIF image data, version 89a, 211 x 71
python-logo-2.gif: GIF image data, version 87a, 211 x 71
% tail -n5 /usr/lib/python3.4/site-packages/PIL/GifImagePlugin.py
#
# Uncomment the following line if you wish to use NETPBM/PBMPLUS
# instead of the built-in "uncompressed" GIF encoder

# Image.register_save(GifImageFile.format, _save_netpbm)

(Je n'ai pas essayé)

Je suppose que l'on peut dire merci à Unisys et son brevet concernant
LZW sur ce coup là...



Après avoir décommenté la ligne citée j'obtiens maintenant :

im = IMG.open("python-logo.gif")
im.save("python.gif")






Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1468, in save
save_handler(self, fp, filename)
File "/usr/lib/python3/dist-packages/PIL/GifImagePlugin.py", line
329, in _save_netpbm
file = im._dump()
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 505, in _dump
self.im.save_ppm(file)
ValueError: image has wrong mode
Avatar
Benoit Izac
Bonjour,

le 17/03/2015 à 22:27, François Couloigner a écrit dans le message
:

# Image.register_save(GifImageFile.format, _save_netpbm)



Après avoir décommenté la ligne citée j'obtiens maintenant :

im = IMG.open("python-logo.gif")
im.save("python.gif")






Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1468, in save
save_handler(self, fp, filename)
File "/usr/lib/python3/dist-packages/PIL/GifImagePlugin.py", line
329, in _save_netpbm
file = im._dump()
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 505, in _dump
self.im.save_ppm(file)
ValueError: image has wrong mode











% cat pil.py
import PIL.Image as IMG
im = IMG.open("python-logo.gif")
im2 = im.convert("RGB")
im2.save("python-logo-2.gif")
% python pil.py
% file python-logo.gif python-logo-2.gif
python-logo.gif: GIF image data, version 89a, 211 x 71
python-logo-2.gif: GIF image data, version 87a, 211 x 71
% wc -c python-logo.gif python-logo-2.gif
2549 python-logo.gif
2539 python-logo-2.gif
5088 total

Tu as gagné 10 octets.

--
Benoit Izac