2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# GIF file handling
|
|
|
|
#
|
|
|
|
# History:
|
|
|
|
# 1995-09-01 fl Created
|
|
|
|
# 1996-12-14 fl Added interlace support
|
|
|
|
# 1996-12-30 fl Added animation support
|
|
|
|
# 1997-01-05 fl Added write support, fixed local colour map bug
|
|
|
|
# 1997-02-23 fl Make sure to load raster data in getdata()
|
|
|
|
# 1997-07-05 fl Support external decoder (0.4)
|
|
|
|
# 1998-07-09 fl Handle all modes when saving (0.5)
|
|
|
|
# 1998-07-15 fl Renamed offset attribute to avoid name clash
|
|
|
|
# 2001-04-16 fl Added rewind support (seek to frame 0) (0.6)
|
|
|
|
# 2001-04-17 fl Added palette optimization (0.7)
|
|
|
|
# 2002-06-06 fl Added transparency support for save (0.8)
|
|
|
|
# 2004-02-24 fl Disable interlacing for small images
|
|
|
|
#
|
|
|
|
# Copyright (c) 1997-2004 by Secret Labs AB
|
|
|
|
# Copyright (c) 1995-2004 by Fredrik Lundh
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
2017-03-07 01:46:55 +03:00
|
|
|
import itertools
|
2020-02-08 00:34:53 +03:00
|
|
|
import math
|
2019-10-07 16:28:36 +03:00
|
|
|
import os
|
|
|
|
import subprocess
|
2017-03-07 01:46:55 +03:00
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from . import Image, ImageChops, ImageFile, ImagePalette, ImageSequence
|
2020-09-01 20:16:46 +03:00
|
|
|
from ._binary import i16le as i16
|
|
|
|
from ._binary import o8
|
|
|
|
from ._binary import o16le as o16
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Identify/read GIF files
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def _accept(prefix):
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
return prefix[:6] in [b"GIF87a", b"GIF89a"]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
##
|
|
|
|
# Image plugin for GIF images. This plugin supports both GIF87 and
|
|
|
|
# GIF89 images.
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
class GifImageFile(ImageFile.ImageFile):
|
|
|
|
|
|
|
|
format = "GIF"
|
|
|
|
format_description = "Compuserve GIF"
|
2017-03-15 02:16:38 +03:00
|
|
|
_close_exclusive_fp_after_loading = False
|
2017-09-30 06:32:43 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
global_palette = None
|
|
|
|
|
|
|
|
def data(self):
|
|
|
|
s = self.fp.read(1)
|
2020-05-08 19:48:02 +03:00
|
|
|
if s and s[0]:
|
|
|
|
return self.fp.read(s[0])
|
2010-07-31 06:52:47 +04:00
|
|
|
return None
|
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
|
|
|
|
# Screen
|
|
|
|
s = self.fp.read(13)
|
2020-06-23 10:41:13 +03:00
|
|
|
if not _accept(s):
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("not a GIF file")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
self.info["version"] = s[:6]
|
2020-05-08 21:11:58 +03:00
|
|
|
self._size = i16(s, 6), i16(s, 8)
|
2010-07-31 06:52:47 +04:00
|
|
|
self.tile = []
|
2020-05-08 19:48:02 +03:00
|
|
|
flags = s[10]
|
2010-07-31 06:52:47 +04:00
|
|
|
bits = (flags & 7) + 1
|
|
|
|
|
|
|
|
if flags & 128:
|
|
|
|
# get global palette
|
2020-05-08 19:48:02 +03:00
|
|
|
self.info["background"] = s[11]
|
2010-07-31 06:52:47 +04:00
|
|
|
# check if palette contains colour indices
|
2014-08-26 17:47:10 +04:00
|
|
|
p = self.fp.read(3 << bits)
|
2010-07-31 06:52:47 +04:00
|
|
|
for i in range(0, len(p), 3):
|
2020-05-08 19:48:02 +03:00
|
|
|
if not (i // 3 == p[i] == p[i + 1] == p[i + 2]):
|
2010-07-31 06:52:47 +04:00
|
|
|
p = ImagePalette.raw("RGB", p)
|
|
|
|
self.global_palette = self.palette = p
|
|
|
|
break
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
self.__fp = self.fp # FIXME: hack
|
2010-07-31 06:52:47 +04:00
|
|
|
self.__rewind = self.fp.tell()
|
2015-04-15 03:43:05 +03:00
|
|
|
self._n_frames = None
|
2015-06-30 06:25:00 +03:00
|
|
|
self._is_animated = None
|
2015-04-15 03:43:05 +03:00
|
|
|
self._seek(0) # get ready to read first frame
|
|
|
|
|
|
|
|
@property
|
|
|
|
def n_frames(self):
|
|
|
|
if self._n_frames is None:
|
|
|
|
current = self.tell()
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
self.seek(self.tell() + 1)
|
|
|
|
except EOFError:
|
|
|
|
self._n_frames = self.tell() + 1
|
|
|
|
self.seek(current)
|
|
|
|
return self._n_frames
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-06-30 06:25:00 +03:00
|
|
|
@property
|
|
|
|
def is_animated(self):
|
|
|
|
if self._is_animated is None:
|
2017-08-18 13:20:27 +03:00
|
|
|
if self._n_frames is not None:
|
|
|
|
self._is_animated = self._n_frames != 1
|
|
|
|
else:
|
|
|
|
current = self.tell()
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.seek(1)
|
|
|
|
self._is_animated = True
|
|
|
|
except EOFError:
|
|
|
|
self._is_animated = False
|
|
|
|
|
|
|
|
self.seek(current)
|
2015-06-30 06:25:00 +03:00
|
|
|
return self._is_animated
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def seek(self, frame):
|
2017-09-30 06:32:43 +03:00
|
|
|
if not self._seek_check(frame):
|
2015-04-15 03:43:05 +03:00
|
|
|
return
|
|
|
|
if frame < self.__frame:
|
2019-03-16 12:02:24 +03:00
|
|
|
if frame != 0:
|
|
|
|
self.im = None
|
2015-04-15 03:43:05 +03:00
|
|
|
self._seek(0)
|
2015-06-18 17:49:18 +03:00
|
|
|
|
|
|
|
last_frame = self.__frame
|
2015-04-15 03:43:05 +03:00
|
|
|
for f in range(self.__frame + 1, frame + 1):
|
2015-06-18 17:49:18 +03:00
|
|
|
try:
|
|
|
|
self._seek(f)
|
2020-06-21 13:13:35 +03:00
|
|
|
except EOFError as e:
|
2015-06-18 17:49:18 +03:00
|
|
|
self.seek(last_frame)
|
2020-06-21 13:13:35 +03:00
|
|
|
raise EOFError("no more images in GIF file") from e
|
2015-04-15 03:43:05 +03:00
|
|
|
|
|
|
|
def _seek(self, frame):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if frame == 0:
|
|
|
|
# rewind
|
|
|
|
self.__offset = 0
|
|
|
|
self.dispose = None
|
2014-08-26 17:47:10 +04:00
|
|
|
self.dispose_extent = [0, 0, 0, 0] # x0, y0, x1, y1
|
2010-07-31 06:52:47 +04:00
|
|
|
self.__frame = -1
|
|
|
|
self.__fp.seek(self.__rewind)
|
2014-07-03 20:48:12 +04:00
|
|
|
self.disposal_method = 0
|
2014-07-07 22:46:54 +04:00
|
|
|
else:
|
|
|
|
# ensure that the previous frame was loaded
|
2021-04-06 12:31:51 +03:00
|
|
|
if self.tile:
|
2014-07-07 22:46:54 +04:00
|
|
|
self.load()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if frame != self.__frame + 1:
|
2020-07-16 12:43:29 +03:00
|
|
|
raise ValueError(f"cannot seek to frame {frame}")
|
2010-07-31 06:52:47 +04:00
|
|
|
self.__frame = frame
|
|
|
|
|
|
|
|
self.tile = []
|
|
|
|
|
|
|
|
self.fp = self.__fp
|
|
|
|
if self.__offset:
|
|
|
|
# backup to last frame
|
|
|
|
self.fp.seek(self.__offset)
|
|
|
|
while self.data():
|
|
|
|
pass
|
|
|
|
self.__offset = 0
|
|
|
|
|
|
|
|
if self.dispose:
|
2014-07-02 23:27:52 +04:00
|
|
|
self.im.paste(self.dispose, self.dispose_extent)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-08-02 18:32:49 +04:00
|
|
|
from copy import copy
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2013-08-02 18:32:49 +04:00
|
|
|
self.palette = copy(self.global_palette)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2018-09-01 02:28:22 +03:00
|
|
|
info = {}
|
2021-03-16 16:24:57 +03:00
|
|
|
frame_transparency = None
|
|
|
|
interlace = None
|
2012-10-17 07:39:56 +04:00
|
|
|
while True:
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
s = self.fp.read(1)
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
if not s or s == b";":
|
2010-07-31 06:52:47 +04:00
|
|
|
break
|
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
elif s == b"!":
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# extensions
|
|
|
|
#
|
|
|
|
s = self.fp.read(1)
|
|
|
|
block = self.data()
|
2020-05-08 19:48:02 +03:00
|
|
|
if s[0] == 249:
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# graphic control extension
|
|
|
|
#
|
2020-05-08 19:48:02 +03:00
|
|
|
flags = block[0]
|
2010-07-31 06:52:47 +04:00
|
|
|
if flags & 1:
|
2021-03-16 16:24:57 +03:00
|
|
|
frame_transparency = block[3]
|
2020-05-08 21:11:58 +03:00
|
|
|
info["duration"] = i16(block, 1) * 10
|
2014-07-02 23:27:52 +04:00
|
|
|
|
|
|
|
# disposal method - find the value of bits 4 - 6
|
2014-07-03 21:01:18 +04:00
|
|
|
dispose_bits = 0b00011100 & flags
|
|
|
|
dispose_bits = dispose_bits >> 2
|
|
|
|
if dispose_bits:
|
|
|
|
# only set the dispose if it is not
|
|
|
|
# unspecified. I'm not sure if this is
|
|
|
|
# correct, but it seems to prevent the last
|
|
|
|
# frame from looking odd for some animations
|
|
|
|
self.disposal_method = dispose_bits
|
2020-05-08 19:48:02 +03:00
|
|
|
elif s[0] == 254:
|
2016-05-07 06:57:40 +03:00
|
|
|
#
|
|
|
|
# comment extension
|
|
|
|
#
|
2018-11-27 13:05:41 +03:00
|
|
|
while block:
|
|
|
|
if "comment" in info:
|
|
|
|
info["comment"] += block
|
|
|
|
else:
|
|
|
|
info["comment"] = block
|
|
|
|
block = self.data()
|
|
|
|
continue
|
2020-05-08 19:48:02 +03:00
|
|
|
elif s[0] == 255:
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# application extension
|
|
|
|
#
|
2018-09-01 02:28:22 +03:00
|
|
|
info["extension"] = block, self.fp.tell()
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
if block[:11] == b"NETSCAPE2.0":
|
2010-07-31 06:52:47 +04:00
|
|
|
block = self.data()
|
2020-05-08 19:48:02 +03:00
|
|
|
if len(block) >= 3 and block[0] == 1:
|
2020-05-08 21:11:58 +03:00
|
|
|
info["loop"] = i16(block, 1)
|
2010-07-31 06:52:47 +04:00
|
|
|
while self.data():
|
|
|
|
pass
|
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
elif s == b",":
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# local image
|
|
|
|
#
|
|
|
|
s = self.fp.read(9)
|
|
|
|
|
|
|
|
# extent
|
2020-05-08 21:11:58 +03:00
|
|
|
x0, y0 = i16(s, 0), i16(s, 2)
|
|
|
|
x1, y1 = x0 + i16(s, 4), y0 + i16(s, 6)
|
2019-05-02 12:46:17 +03:00
|
|
|
if x1 > self.size[0] or y1 > self.size[1]:
|
|
|
|
self._size = max(x1, self.size[0]), max(y1, self.size[1])
|
2014-07-02 23:27:52 +04:00
|
|
|
self.dispose_extent = x0, y0, x1, y1
|
2020-05-08 19:48:02 +03:00
|
|
|
flags = s[8]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
interlace = (flags & 64) != 0
|
|
|
|
|
|
|
|
if flags & 128:
|
|
|
|
bits = (flags & 7) + 1
|
2019-03-21 16:28:20 +03:00
|
|
|
self.palette = ImagePalette.raw("RGB", self.fp.read(3 << bits))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# image data
|
2020-05-08 19:48:02 +03:00
|
|
|
bits = self.fp.read(1)[0]
|
2010-07-31 06:52:47 +04:00
|
|
|
self.__offset = self.fp.tell()
|
|
|
|
break
|
|
|
|
|
|
|
|
else:
|
|
|
|
pass
|
2020-05-08 19:48:02 +03:00
|
|
|
# raise OSError, "illegal GIF tag `%x`" % s[0]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-07-02 23:27:52 +04:00
|
|
|
try:
|
2014-07-03 20:48:12 +04:00
|
|
|
if self.disposal_method < 2:
|
2014-07-02 23:27:52 +04:00
|
|
|
# do not dispose or none specified
|
|
|
|
self.dispose = None
|
2014-07-03 20:48:12 +04:00
|
|
|
elif self.disposal_method == 2:
|
2014-07-02 23:27:52 +04:00
|
|
|
# replace with background colour
|
2021-03-14 05:31:16 +03:00
|
|
|
|
|
|
|
# only dispose the extent in this frame
|
2021-03-14 05:40:55 +03:00
|
|
|
x0, y0, x1, y1 = self.dispose_extent
|
|
|
|
dispose_size = (x1 - x0, y1 - y0)
|
|
|
|
|
|
|
|
Image._decompression_bomb_check(dispose_size)
|
2021-06-25 17:55:31 +03:00
|
|
|
|
|
|
|
# by convention, attempt to use transparency first
|
2021-10-12 01:45:52 +03:00
|
|
|
color = self.info.get("transparency", frame_transparency)
|
|
|
|
if color is None:
|
|
|
|
color = self.info.get("background", 0)
|
2021-06-25 17:55:31 +03:00
|
|
|
self.dispose = Image.core.fill("P", dispose_size, color)
|
2014-07-02 23:27:52 +04:00
|
|
|
else:
|
|
|
|
# replace with previous contents
|
|
|
|
if self.im:
|
2021-03-14 05:31:16 +03:00
|
|
|
# only dispose the extent in this frame
|
|
|
|
self.dispose = self._crop(self.im, self.dispose_extent)
|
2021-04-15 12:01:12 +03:00
|
|
|
elif frame_transparency is not None:
|
|
|
|
x0, y0, x1, y1 = self.dispose_extent
|
|
|
|
dispose_size = (x1 - x0, y1 - y0)
|
|
|
|
|
|
|
|
Image._decompression_bomb_check(dispose_size)
|
|
|
|
self.dispose = Image.core.fill(
|
|
|
|
"P", dispose_size, frame_transparency
|
|
|
|
)
|
2021-04-08 01:04:20 +03:00
|
|
|
except AttributeError:
|
2014-07-02 23:27:52 +04:00
|
|
|
pass
|
|
|
|
|
2021-03-16 16:24:57 +03:00
|
|
|
if interlace is not None:
|
|
|
|
transparency = -1
|
|
|
|
if frame_transparency is not None:
|
|
|
|
if frame == 0:
|
|
|
|
self.info["transparency"] = frame_transparency
|
|
|
|
else:
|
|
|
|
transparency = frame_transparency
|
|
|
|
self.tile = [
|
|
|
|
(
|
|
|
|
"gif",
|
|
|
|
(x0, y0, x1, y1),
|
|
|
|
self.__offset,
|
|
|
|
(bits, interlace, transparency),
|
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
2010-07-31 06:52:47 +04:00
|
|
|
# self.__fp = None
|
2015-06-18 17:49:18 +03:00
|
|
|
raise EOFError
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2021-03-16 16:24:57 +03:00
|
|
|
for k in ["duration", "comment", "extension", "loop"]:
|
2018-09-01 02:28:22 +03:00
|
|
|
if k in info:
|
|
|
|
self.info[k] = info[k]
|
|
|
|
elif k in self.info:
|
|
|
|
del self.info[k]
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
self.mode = "L"
|
|
|
|
if self.palette:
|
|
|
|
self.mode = "P"
|
|
|
|
|
2021-06-25 14:54:21 +03:00
|
|
|
def load_prepare(self):
|
|
|
|
if not self.im and "transparency" in self.info:
|
|
|
|
self.im = Image.core.fill(self.mode, self.size, self.info["transparency"])
|
|
|
|
|
|
|
|
super(GifImageFile, self).load_prepare()
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def tell(self):
|
|
|
|
return self.__frame
|
|
|
|
|
2018-11-17 13:56:06 +03:00
|
|
|
def _close__fp(self):
|
|
|
|
try:
|
2019-01-04 04:29:23 +03:00
|
|
|
if self.__fp != self.fp:
|
|
|
|
self.__fp.close()
|
2018-11-17 13:56:06 +03:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
self.__fp = None
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Write GIF files
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
RAWMODE = {"1": "L", "L": "L", "P": "P"}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-07-01 02:18:05 +03:00
|
|
|
|
2017-02-23 17:58:37 +03:00
|
|
|
def _normalize_mode(im, initial_call=False):
|
2017-03-03 18:51:07 +03:00
|
|
|
"""
|
|
|
|
Takes an image (or frame), returns an image in a mode that is appropriate
|
|
|
|
for saving in a Gif.
|
|
|
|
|
|
|
|
It may return the original image, or it may return an image converted to
|
|
|
|
palette or 'L' mode.
|
|
|
|
|
|
|
|
UNDONE: What is the point of mucking with the initial call palette, for
|
|
|
|
an image that shouldn't have a palette, or it would be a mode 'P' and
|
|
|
|
get returned in the RAWMODE clause.
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2017-03-03 18:51:07 +03:00
|
|
|
:param im: Image object
|
|
|
|
:param initial_call: Default false, set to true for a single frame.
|
|
|
|
:returns: Image object
|
|
|
|
"""
|
2017-02-23 17:58:37 +03:00
|
|
|
if im.mode in RAWMODE:
|
2017-02-23 18:37:14 +03:00
|
|
|
im.load()
|
2017-02-23 17:58:37 +03:00
|
|
|
return im
|
2015-07-01 02:18:05 +03:00
|
|
|
if Image.getmodebase(im.mode) == "RGB":
|
2015-08-24 16:23:07 +03:00
|
|
|
if initial_call:
|
|
|
|
palette_size = 256
|
|
|
|
if im.palette:
|
|
|
|
palette_size = len(im.palette.getdata()[1]) // 3
|
2017-02-23 17:58:37 +03:00
|
|
|
return im.convert("P", palette=Image.ADAPTIVE, colors=palette_size)
|
2015-08-24 16:23:07 +03:00
|
|
|
else:
|
|
|
|
return im.convert("P")
|
2015-07-01 02:18:05 +03:00
|
|
|
return im.convert("L")
|
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2017-03-03 18:51:31 +03:00
|
|
|
def _normalize_palette(im, palette, info):
|
|
|
|
"""
|
|
|
|
Normalizes the palette for image.
|
|
|
|
- Sets the palette to the incoming palette, if provided.
|
|
|
|
- Ensures that there's a palette for L mode images
|
|
|
|
- Optimizes the palette if necessary/desired.
|
|
|
|
|
|
|
|
:param im: Image object
|
|
|
|
:param palette: bytes object containing the source palette, or ....
|
2017-04-20 14:14:23 +03:00
|
|
|
:param info: encoderinfo
|
2017-03-03 18:51:31 +03:00
|
|
|
:returns: Image object
|
|
|
|
"""
|
2017-03-07 01:46:55 +03:00
|
|
|
source_palette = None
|
|
|
|
if palette:
|
|
|
|
# a bytes palette
|
|
|
|
if isinstance(palette, (bytes, bytearray, list)):
|
|
|
|
source_palette = bytearray(palette[:768])
|
|
|
|
if isinstance(palette, ImagePalette.ImagePalette):
|
2021-07-09 17:00:50 +03:00
|
|
|
source_palette = bytearray(palette.palette)
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2017-03-03 18:51:31 +03:00
|
|
|
if im.mode == "P":
|
2017-03-07 01:46:55 +03:00
|
|
|
if not source_palette:
|
2017-03-03 18:51:31 +03:00
|
|
|
source_palette = im.im.getpalette("RGB")[:768]
|
|
|
|
else: # L-mode
|
2017-03-07 01:46:55 +03:00
|
|
|
if not source_palette:
|
2019-03-21 16:28:20 +03:00
|
|
|
source_palette = bytearray(i // 3 for i in range(768))
|
|
|
|
im.palette = ImagePalette.ImagePalette("RGB", palette=source_palette)
|
2017-03-03 18:51:31 +03:00
|
|
|
|
2021-07-11 15:52:32 +03:00
|
|
|
if palette:
|
|
|
|
used_palette_colors = []
|
|
|
|
for i in range(0, len(source_palette), 3):
|
|
|
|
source_color = tuple(source_palette[i : i + 3])
|
|
|
|
try:
|
|
|
|
index = im.palette.colors[source_color]
|
|
|
|
except KeyError:
|
|
|
|
index = None
|
|
|
|
used_palette_colors.append(index)
|
|
|
|
for i, index in enumerate(used_palette_colors):
|
|
|
|
if index is None:
|
|
|
|
for j in range(len(used_palette_colors)):
|
|
|
|
if j not in used_palette_colors:
|
|
|
|
used_palette_colors[i] = j
|
|
|
|
break
|
|
|
|
im = im.remap_palette(used_palette_colors)
|
|
|
|
else:
|
|
|
|
used_palette_colors = _get_optimize(im, info)
|
|
|
|
if used_palette_colors is not None:
|
|
|
|
return im.remap_palette(used_palette_colors, source_palette)
|
2017-03-03 18:51:31 +03:00
|
|
|
|
|
|
|
im.palette.palette = source_palette
|
|
|
|
return im
|
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2017-02-18 13:23:42 +03:00
|
|
|
def _write_single_frame(im, fp, palette):
|
2017-02-23 17:58:37 +03:00
|
|
|
im_out = _normalize_mode(im, True)
|
2018-06-16 12:47:57 +03:00
|
|
|
for k, v in im_out.info.items():
|
|
|
|
im.encoderinfo.setdefault(k, v)
|
2017-02-23 17:14:04 +03:00
|
|
|
im_out = _normalize_palette(im_out, palette, im.encoderinfo)
|
|
|
|
|
2017-02-23 18:41:42 +03:00
|
|
|
for s in _get_global_header(im_out, im.encoderinfo):
|
2017-02-18 13:23:42 +03:00
|
|
|
fp.write(s)
|
|
|
|
|
|
|
|
# local image header
|
|
|
|
flags = 0
|
|
|
|
if get_interlace(im):
|
|
|
|
flags = flags | 64
|
2017-02-25 13:24:53 +03:00
|
|
|
_write_local_header(fp, im, (0, 0), flags)
|
2017-02-18 13:23:42 +03:00
|
|
|
|
|
|
|
im_out.encoderconfig = (8, get_interlace(im))
|
2019-03-21 16:28:20 +03:00
|
|
|
ImageFile._save(im_out, fp, [("gif", (0, 0) + im.size, 0, RAWMODE[im_out.mode])])
|
2017-02-18 13:23:42 +03:00
|
|
|
|
|
|
|
fp.write(b"\0") # end of image data
|
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2017-02-18 13:23:42 +03:00
|
|
|
def _write_multiple_frames(im, fp, palette):
|
2017-02-23 18:37:14 +03:00
|
|
|
|
2018-06-16 12:47:57 +03:00
|
|
|
duration = im.encoderinfo.get("duration", im.info.get("duration"))
|
|
|
|
disposal = im.encoderinfo.get("disposal", im.info.get("disposal"))
|
2017-02-18 13:23:42 +03:00
|
|
|
|
|
|
|
im_frames = []
|
|
|
|
frame_count = 0
|
2019-06-29 16:06:45 +03:00
|
|
|
background_im = None
|
|
|
|
for imSequence in itertools.chain([im], im.encoderinfo.get("append_images", [])):
|
2017-02-18 13:23:42 +03:00
|
|
|
for im_frame in ImageSequence.Iterator(imSequence):
|
2017-02-23 18:37:14 +03:00
|
|
|
# a copy is required here since seek can still mutate the image
|
|
|
|
im_frame = _normalize_mode(im_frame.copy())
|
2018-06-16 12:47:57 +03:00
|
|
|
if frame_count == 0:
|
|
|
|
for k, v in im_frame.info.items():
|
|
|
|
im.encoderinfo.setdefault(k, v)
|
2017-02-23 17:57:10 +03:00
|
|
|
im_frame = _normalize_palette(im_frame, palette, im.encoderinfo)
|
2017-02-23 18:38:46 +03:00
|
|
|
|
2017-02-18 13:23:42 +03:00
|
|
|
encoderinfo = im.encoderinfo.copy()
|
|
|
|
if isinstance(duration, (list, tuple)):
|
2019-03-21 16:28:20 +03:00
|
|
|
encoderinfo["duration"] = duration[frame_count]
|
2017-12-22 01:26:58 +03:00
|
|
|
if isinstance(disposal, (list, tuple)):
|
|
|
|
encoderinfo["disposal"] = disposal[frame_count]
|
2017-02-18 13:23:42 +03:00
|
|
|
frame_count += 1
|
|
|
|
|
|
|
|
if im_frames:
|
|
|
|
# delta frame
|
|
|
|
previous = im_frames[-1]
|
2019-06-29 16:06:45 +03:00
|
|
|
if encoderinfo.get("disposal") == 2:
|
|
|
|
if background_im is None:
|
2021-06-28 15:12:38 +03:00
|
|
|
color = im.encoderinfo.get(
|
|
|
|
"transparency", im.info.get("transparency", (0, 0, 0))
|
2019-06-29 16:06:45 +03:00
|
|
|
)
|
2021-06-28 15:12:38 +03:00
|
|
|
background = _get_background(im_frame, color)
|
2019-06-29 16:06:45 +03:00
|
|
|
background_im = Image.new("P", im_frame.size, background)
|
|
|
|
background_im.putpalette(im_frames[0]["im"].palette)
|
|
|
|
base_im = background_im
|
2019-03-11 16:41:14 +03:00
|
|
|
else:
|
2019-06-29 16:06:45 +03:00
|
|
|
base_im = previous["im"]
|
|
|
|
if _get_palette_bytes(im_frame) == _get_palette_bytes(base_im):
|
|
|
|
delta = ImageChops.subtract_modulo(im_frame, base_im)
|
2017-02-23 17:14:04 +03:00
|
|
|
else:
|
2018-06-24 15:32:25 +03:00
|
|
|
delta = ImageChops.subtract_modulo(
|
2019-06-29 16:06:45 +03:00
|
|
|
im_frame.convert("RGB"), base_im.convert("RGB")
|
|
|
|
)
|
2017-02-18 13:23:42 +03:00
|
|
|
bbox = delta.getbbox()
|
2019-03-22 17:19:01 +03:00
|
|
|
if not bbox:
|
2017-02-18 13:23:42 +03:00
|
|
|
# This frame is identical to the previous frame
|
|
|
|
if duration:
|
2019-03-21 16:28:20 +03:00
|
|
|
previous["encoderinfo"]["duration"] += encoderinfo["duration"]
|
2017-02-18 13:23:42 +03:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
bbox = None
|
2019-03-21 16:28:20 +03:00
|
|
|
im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo})
|
2017-02-23 18:38:46 +03:00
|
|
|
|
2017-02-18 13:23:42 +03:00
|
|
|
if len(im_frames) > 1:
|
|
|
|
for frame_data in im_frames:
|
2019-03-21 16:28:20 +03:00
|
|
|
im_frame = frame_data["im"]
|
|
|
|
if not frame_data["bbox"]:
|
2017-02-18 13:23:42 +03:00
|
|
|
# global header
|
2019-03-21 16:28:20 +03:00
|
|
|
for s in _get_global_header(im_frame, frame_data["encoderinfo"]):
|
2017-02-18 13:23:42 +03:00
|
|
|
fp.write(s)
|
|
|
|
offset = (0, 0)
|
|
|
|
else:
|
|
|
|
# compress difference
|
2021-07-11 15:52:32 +03:00
|
|
|
if not palette:
|
|
|
|
frame_data["encoderinfo"]["include_color_table"] = True
|
2017-02-18 13:23:42 +03:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
im_frame = im_frame.crop(frame_data["bbox"])
|
|
|
|
offset = frame_data["bbox"][:2]
|
|
|
|
_write_frame_data(fp, im_frame, offset, frame_data["encoderinfo"])
|
2017-02-18 13:23:42 +03:00
|
|
|
return True
|
2019-08-24 01:10:45 +03:00
|
|
|
elif "duration" in im.encoderinfo and isinstance(
|
|
|
|
im.encoderinfo["duration"], (list, tuple)
|
|
|
|
):
|
|
|
|
# Since multiple frames will not be written, add together the frame durations
|
|
|
|
im.encoderinfo["duration"] = sum(im.encoderinfo["duration"])
|
2015-07-01 02:18:05 +03:00
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2015-06-30 11:02:48 +03:00
|
|
|
def _save_all(im, fp, filename):
|
|
|
|
_save(im, fp, filename, save_all=True)
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2015-07-01 02:18:05 +03:00
|
|
|
|
2015-06-30 11:02:48 +03:00
|
|
|
def _save(im, fp, filename, save_all=False):
|
2010-07-31 06:52:47 +04:00
|
|
|
# header
|
2018-06-16 12:47:57 +03:00
|
|
|
if "palette" in im.encoderinfo or "palette" in im.info:
|
|
|
|
palette = im.encoderinfo.get("palette", im.info.get("palette"))
|
|
|
|
else:
|
2013-01-10 23:34:58 +04:00
|
|
|
palette = None
|
2013-12-04 18:07:36 +04:00
|
|
|
im.encoderinfo["optimize"] = im.encoderinfo.get("optimize", True)
|
2013-05-23 17:45:11 +04:00
|
|
|
|
2017-02-18 13:23:42 +03:00
|
|
|
if not save_all or not _write_multiple_frames(im, fp, palette):
|
|
|
|
_write_single_frame(im, fp, palette)
|
2015-04-03 15:22:13 +03:00
|
|
|
|
|
|
|
fp.write(b";") # end of file
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-12-09 07:47:53 +03:00
|
|
|
if hasattr(fp, "flush"):
|
2015-04-03 15:22:13 +03:00
|
|
|
fp.flush()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-04-23 16:40:42 +03:00
|
|
|
|
2015-04-03 15:22:13 +03:00
|
|
|
def get_interlace(im):
|
2017-02-25 05:49:05 +03:00
|
|
|
interlace = im.encoderinfo.get("interlace", 1)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# workaround for @PIL153
|
|
|
|
if min(im.size) < 16:
|
|
|
|
interlace = 0
|
|
|
|
|
2015-04-03 15:22:13 +03:00
|
|
|
return interlace
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
2017-02-25 13:24:53 +03:00
|
|
|
def _write_local_header(fp, im, offset, flags):
|
2015-04-03 15:22:13 +03:00
|
|
|
transparent_color_exists = False
|
2010-07-31 06:52:47 +04:00
|
|
|
try:
|
|
|
|
transparency = im.encoderinfo["transparency"]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
2013-05-24 15:12:40 +04:00
|
|
|
transparency = int(transparency)
|
|
|
|
# optimize the block away if transparent color is not used
|
2015-04-01 16:47:01 +03:00
|
|
|
transparent_color_exists = True
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2016-11-05 05:32:02 +03:00
|
|
|
used_palette_colors = _get_optimize(im, im.encoderinfo)
|
|
|
|
if used_palette_colors is not None:
|
2015-04-23 17:44:27 +03:00
|
|
|
# adjust the transparency index after optimize
|
2016-12-28 01:48:16 +03:00
|
|
|
try:
|
|
|
|
transparency = used_palette_colors.index(transparency)
|
|
|
|
except ValueError:
|
|
|
|
transparent_color_exists = False
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2015-04-04 02:32:17 +03:00
|
|
|
if "duration" in im.encoderinfo:
|
2015-04-04 03:45:30 +03:00
|
|
|
duration = int(im.encoderinfo["duration"] / 10)
|
2015-04-03 15:22:13 +03:00
|
|
|
else:
|
|
|
|
duration = 0
|
2017-01-30 22:34:48 +03:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
disposal = int(im.encoderinfo.get("disposal", 0))
|
2017-01-30 22:34:48 +03:00
|
|
|
|
2017-01-31 10:32:01 +03:00
|
|
|
if transparent_color_exists or duration != 0 or disposal:
|
2017-01-30 22:34:48 +03:00
|
|
|
packed_flag = 1 if transparent_color_exists else 0
|
|
|
|
packed_flag |= disposal << 2
|
2015-04-03 15:22:13 +03:00
|
|
|
if not transparent_color_exists:
|
|
|
|
transparency = 0
|
2015-04-23 16:40:42 +03:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
fp.write(
|
|
|
|
b"!"
|
|
|
|
+ o8(249) # extension intro
|
|
|
|
+ o8(4) # length
|
|
|
|
+ o8(packed_flag) # packed fields
|
|
|
|
+ o16(duration) # duration
|
|
|
|
+ o8(transparency) # transparency index
|
|
|
|
+ o8(0)
|
|
|
|
)
|
|
|
|
|
|
|
|
if "comment" in im.encoderinfo and 1 <= len(im.encoderinfo["comment"]):
|
|
|
|
fp.write(b"!" + o8(254)) # extension intro
|
2020-01-01 05:29:39 +03:00
|
|
|
comment = im.encoderinfo["comment"]
|
|
|
|
if isinstance(comment, str):
|
|
|
|
comment = comment.encode()
|
|
|
|
for i in range(0, len(comment), 255):
|
|
|
|
subblock = comment[i : i + 255]
|
2019-03-21 16:28:20 +03:00
|
|
|
fp.write(o8(len(subblock)) + subblock)
|
2018-11-27 13:05:41 +03:00
|
|
|
fp.write(o8(0))
|
2015-04-04 02:32:17 +03:00
|
|
|
if "loop" in im.encoderinfo:
|
|
|
|
number_of_loops = im.encoderinfo["loop"]
|
2019-03-21 16:28:20 +03:00
|
|
|
fp.write(
|
|
|
|
b"!"
|
|
|
|
+ o8(255) # extension intro
|
|
|
|
+ o8(11)
|
|
|
|
+ b"NETSCAPE2.0"
|
|
|
|
+ o8(3)
|
|
|
|
+ o8(1)
|
|
|
|
+ o16(number_of_loops) # number of loops
|
|
|
|
+ o8(0)
|
|
|
|
)
|
|
|
|
include_color_table = im.encoderinfo.get("include_color_table")
|
2016-09-11 04:57:45 +03:00
|
|
|
if include_color_table:
|
2017-02-23 18:38:46 +03:00
|
|
|
palette_bytes = _get_palette_bytes(im)
|
2016-09-11 04:57:45 +03:00
|
|
|
color_table_size = _get_color_table_size(palette_bytes)
|
|
|
|
if color_table_size:
|
2019-03-21 16:28:20 +03:00
|
|
|
flags = flags | 128 # local color table flag
|
2016-09-11 04:57:45 +03:00
|
|
|
flags = flags | color_table_size
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
fp.write(
|
|
|
|
b","
|
|
|
|
+ o16(offset[0]) # offset
|
|
|
|
+ o16(offset[1])
|
|
|
|
+ o16(im.size[0]) # size
|
|
|
|
+ o16(im.size[1])
|
|
|
|
+ o8(flags) # flags
|
|
|
|
)
|
2016-09-11 04:57:45 +03:00
|
|
|
if include_color_table and color_table_size:
|
|
|
|
fp.write(_get_header_palette(palette_bytes))
|
2019-03-21 16:28:20 +03:00
|
|
|
fp.write(o8(8)) # bits
|
2013-05-23 17:45:11 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def _save_netpbm(im, fp, filename):
|
|
|
|
|
2017-01-26 05:51:20 +03:00
|
|
|
# Unused by default.
|
|
|
|
# To use, uncomment the register_save call at the end of the file.
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# If you need real GIF compression and/or RGB quantization, you
|
|
|
|
# can use the external NETPBM/PBMPLUS utilities. See comments
|
|
|
|
# below for information on how to enable this.
|
2019-03-03 05:02:00 +03:00
|
|
|
tempfile = im._dump()
|
2014-06-30 01:24:32 +04:00
|
|
|
|
2019-12-25 07:22:54 +03:00
|
|
|
try:
|
|
|
|
with open(filename, "wb") as f:
|
|
|
|
if im.mode != "RGB":
|
|
|
|
subprocess.check_call(
|
|
|
|
["ppmtogif", tempfile], stdout=f, stderr=subprocess.DEVNULL
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# Pipe ppmquant output into ppmtogif
|
|
|
|
# "ppmquant 256 %s | ppmtogif > %s" % (tempfile, filename)
|
|
|
|
quant_cmd = ["ppmquant", "256", tempfile]
|
|
|
|
togif_cmd = ["ppmtogif"]
|
|
|
|
quant_proc = subprocess.Popen(
|
|
|
|
quant_cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL
|
|
|
|
)
|
|
|
|
togif_proc = subprocess.Popen(
|
|
|
|
togif_cmd,
|
|
|
|
stdin=quant_proc.stdout,
|
|
|
|
stdout=f,
|
|
|
|
stderr=subprocess.DEVNULL,
|
|
|
|
)
|
2014-06-27 19:02:36 +04:00
|
|
|
|
2019-12-25 07:22:54 +03:00
|
|
|
# Allow ppmquant to receive SIGPIPE if ppmtogif exits
|
|
|
|
quant_proc.stdout.close()
|
2014-06-27 19:02:36 +04:00
|
|
|
|
2019-12-25 07:22:54 +03:00
|
|
|
retcode = quant_proc.wait()
|
|
|
|
if retcode:
|
|
|
|
raise subprocess.CalledProcessError(retcode, quant_cmd)
|
2014-06-27 19:02:36 +04:00
|
|
|
|
2019-12-25 07:22:54 +03:00
|
|
|
retcode = togif_proc.wait()
|
|
|
|
if retcode:
|
|
|
|
raise subprocess.CalledProcessError(retcode, togif_cmd)
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
os.unlink(tempfile)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
2016-11-05 05:32:02 +03:00
|
|
|
# Force optimization so that we can test performance against
|
|
|
|
# cases where it took lots of memory and time previously.
|
|
|
|
_FORCE_OPTIMIZE = False
|
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2015-04-23 17:44:27 +03:00
|
|
|
def _get_optimize(im, info):
|
2017-02-23 15:25:04 +03:00
|
|
|
"""
|
|
|
|
Palette optimization is a potentially expensive operation.
|
2017-02-23 18:38:46 +03:00
|
|
|
|
2017-02-23 15:25:04 +03:00
|
|
|
This function determines if the palette should be optimized using
|
|
|
|
some heuristics, then returns the list of palette entries in use.
|
2017-02-23 18:38:46 +03:00
|
|
|
|
2017-02-23 15:25:04 +03:00
|
|
|
:param im: Image object
|
2017-02-23 18:38:46 +03:00
|
|
|
:param info: encoderinfo
|
2017-02-23 15:25:04 +03:00
|
|
|
:returns: list of indexes of palette entries in use, or None
|
|
|
|
"""
|
2016-11-05 05:32:02 +03:00
|
|
|
if im.mode in ("P", "L") and info and info.get("optimize", 0):
|
|
|
|
# Potentially expensive operation.
|
2015-04-23 17:44:27 +03:00
|
|
|
|
2016-11-05 05:32:02 +03:00
|
|
|
# The palette saves 3 bytes per color not used, but palette
|
|
|
|
# lengths are restricted to 3*(2**N) bytes. Max saving would
|
|
|
|
# be 768 -> 6 bytes if we went all the way down to 2 colors.
|
|
|
|
# * If we're over 128 colors, we can't save any space.
|
|
|
|
# * If there aren't any holes, it's not worth collapsing.
|
|
|
|
# * If we have a 'large' image, the palette is in the noise.
|
|
|
|
|
|
|
|
# create the new palette if not every color is used
|
2019-03-21 16:28:20 +03:00
|
|
|
optimise = _FORCE_OPTIMIZE or im.mode == "L"
|
2017-01-26 07:27:43 +03:00
|
|
|
if optimise or im.width * im.height < 512 * 512:
|
2017-01-26 06:17:20 +03:00
|
|
|
# check which colors are used
|
|
|
|
used_palette_colors = []
|
|
|
|
for i, count in enumerate(im.histogram()):
|
|
|
|
if count:
|
|
|
|
used_palette_colors.append(i)
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
if optimise or (
|
|
|
|
len(used_palette_colors) <= 128
|
|
|
|
and max(used_palette_colors) > len(used_palette_colors)
|
|
|
|
):
|
2017-01-26 07:27:43 +03:00
|
|
|
return used_palette_colors
|
2015-04-23 17:44:27 +03:00
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2016-09-11 04:57:45 +03:00
|
|
|
def _get_color_table_size(palette_bytes):
|
|
|
|
# calculate the palette size for the header
|
2019-06-29 12:24:12 +03:00
|
|
|
if not palette_bytes:
|
|
|
|
return 0
|
|
|
|
elif len(palette_bytes) < 9:
|
|
|
|
return 1
|
|
|
|
else:
|
2020-02-08 00:34:53 +03:00
|
|
|
return math.ceil(math.log(len(palette_bytes) // 3, 2)) - 1
|
2015-04-23 17:44:27 +03:00
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2016-09-11 04:57:45 +03:00
|
|
|
def _get_header_palette(palette_bytes):
|
2017-02-23 18:58:30 +03:00
|
|
|
"""
|
|
|
|
Returns the palette, null padded to the next power of 2 (*3) bytes
|
|
|
|
suitable for direct inclusion in the GIF header
|
|
|
|
|
|
|
|
:param palette_bytes: Unpadded palette bytes, in RGBRGB form
|
|
|
|
:returns: Null padded palette
|
|
|
|
"""
|
2016-09-11 04:57:45 +03:00
|
|
|
color_table_size = _get_color_table_size(palette_bytes)
|
2015-08-21 15:10:13 +03:00
|
|
|
|
2016-09-11 04:57:45 +03:00
|
|
|
# add the missing amount of bytes
|
|
|
|
# the palette has to be 2<<n in size
|
2019-03-21 16:28:20 +03:00
|
|
|
actual_target_size_diff = (2 << color_table_size) - len(palette_bytes) // 3
|
2016-09-11 04:57:45 +03:00
|
|
|
if actual_target_size_diff > 0:
|
|
|
|
palette_bytes += o8(0) * 3 * actual_target_size_diff
|
|
|
|
return palette_bytes
|
2013-05-23 17:45:11 +04:00
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2017-02-23 18:38:46 +03:00
|
|
|
def _get_palette_bytes(im):
|
2017-04-20 14:14:23 +03:00
|
|
|
"""
|
|
|
|
Gets the palette for inclusion in the gif header
|
|
|
|
|
|
|
|
:param im: Image object
|
|
|
|
:returns: Bytes, len<=768 suitable for inclusion in gif header
|
|
|
|
"""
|
|
|
|
return im.palette.palette
|
2017-02-23 17:14:04 +03:00
|
|
|
|
2016-09-11 04:57:45 +03:00
|
|
|
|
2019-06-29 16:06:45 +03:00
|
|
|
def _get_background(im, infoBackground):
|
|
|
|
background = 0
|
|
|
|
if infoBackground:
|
|
|
|
background = infoBackground
|
|
|
|
if isinstance(background, tuple):
|
|
|
|
# WebPImagePlugin stores an RGBA value in info["background"]
|
|
|
|
# So it must be converted to the same format as GifImagePlugin's
|
|
|
|
# info["background"] - a global color table index
|
2021-06-23 12:28:46 +03:00
|
|
|
try:
|
|
|
|
background = im.palette.getcolor(background, im)
|
|
|
|
except ValueError as e:
|
|
|
|
if str(e) == "cannot allocate more than 256 colors":
|
|
|
|
# If all 256 colors are in use,
|
|
|
|
# then there is no need for the background color
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
raise
|
2019-06-29 16:06:45 +03:00
|
|
|
return background
|
|
|
|
|
|
|
|
|
2017-02-23 18:41:42 +03:00
|
|
|
def _get_global_header(im, info):
|
2016-09-11 04:57:45 +03:00
|
|
|
"""Return a list of strings representing a GIF header"""
|
|
|
|
|
|
|
|
# Header Block
|
2021-07-12 14:00:36 +03:00
|
|
|
# https://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
|
2016-09-11 04:57:45 +03:00
|
|
|
|
|
|
|
version = b"87a"
|
|
|
|
for extensionKey in ["transparency", "duration", "loop", "comment"]:
|
|
|
|
if info and extensionKey in info:
|
2019-03-21 16:28:20 +03:00
|
|
|
if (extensionKey == "duration" and info[extensionKey] == 0) or (
|
|
|
|
extensionKey == "comment" and not (1 <= len(info[extensionKey]) <= 255)
|
|
|
|
):
|
2016-09-11 04:57:45 +03:00
|
|
|
continue
|
|
|
|
version = b"89a"
|
|
|
|
break
|
|
|
|
else:
|
2016-10-22 20:55:50 +03:00
|
|
|
if im.info.get("version") == b"89a":
|
2016-09-11 04:57:45 +03:00
|
|
|
version = b"89a"
|
|
|
|
|
2019-06-29 16:06:45 +03:00
|
|
|
background = _get_background(im, info.get("background"))
|
2018-10-02 13:52:07 +03:00
|
|
|
|
2017-02-23 18:38:46 +03:00
|
|
|
palette_bytes = _get_palette_bytes(im)
|
2017-02-18 11:02:52 +03:00
|
|
|
color_table_size = _get_color_table_size(palette_bytes)
|
|
|
|
|
|
|
|
return [
|
2019-03-21 16:28:20 +03:00
|
|
|
b"GIF" # signature
|
|
|
|
+ version # version
|
|
|
|
+ o16(im.size[0]) # canvas width
|
|
|
|
+ o16(im.size[1]), # canvas height
|
2017-02-18 11:02:52 +03:00
|
|
|
# Logical Screen Descriptor
|
|
|
|
# size of global color table + global color table flag
|
2019-03-21 16:28:20 +03:00
|
|
|
o8(color_table_size + 128), # packed fields
|
2017-02-18 11:02:52 +03:00
|
|
|
# background + reserved/aspect
|
2017-04-20 14:14:23 +03:00
|
|
|
o8(background) + o8(0),
|
2017-02-18 11:02:52 +03:00
|
|
|
# Global Color Table
|
2019-03-21 16:28:20 +03:00
|
|
|
_get_header_palette(palette_bytes),
|
2016-09-11 04:57:45 +03:00
|
|
|
]
|
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2017-02-25 13:29:54 +03:00
|
|
|
def _write_frame_data(fp, im_frame, offset, params):
|
|
|
|
try:
|
|
|
|
im_frame.encoderinfo = params
|
|
|
|
|
|
|
|
# local image header
|
|
|
|
_write_local_header(fp, im_frame, offset, 0)
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
ImageFile._save(
|
|
|
|
im_frame, fp, [("gif", (0, 0) + im_frame.size, 0, RAWMODE[im_frame.mode])]
|
|
|
|
)
|
2017-02-25 13:29:54 +03:00
|
|
|
|
|
|
|
fp.write(b"\0") # end of image data
|
|
|
|
finally:
|
|
|
|
del im_frame.encoderinfo
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2017-02-25 13:29:54 +03:00
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Legacy GIF utilities
|
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2017-03-07 12:32:01 +03:00
|
|
|
def getheader(im, palette=None, info=None):
|
2017-02-23 18:58:30 +03:00
|
|
|
"""
|
|
|
|
Legacy Method to get Gif data from image.
|
|
|
|
|
|
|
|
Warning:: May modify image data.
|
|
|
|
|
|
|
|
:param im: Image object
|
|
|
|
:param palette: bytes object containing the source palette, or ....
|
2017-04-20 14:14:23 +03:00
|
|
|
:param info: encoderinfo
|
2017-02-23 18:58:30 +03:00
|
|
|
:returns: tuple of(list of header items, optimized palette)
|
|
|
|
|
|
|
|
"""
|
2017-02-18 11:02:52 +03:00
|
|
|
used_palette_colors = _get_optimize(im, info)
|
2013-08-05 19:59:35 +04:00
|
|
|
|
2017-03-07 12:32:01 +03:00
|
|
|
if info is None:
|
|
|
|
info = {}
|
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
if "background" not in info and "background" in im.info:
|
2017-02-18 11:02:52 +03:00
|
|
|
info["background"] = im.info["background"]
|
|
|
|
|
2017-02-23 18:59:06 +03:00
|
|
|
im_mod = _normalize_palette(im, palette, info)
|
|
|
|
im.palette = im_mod.palette
|
|
|
|
im.im = im_mod.im
|
2017-02-23 18:41:42 +03:00
|
|
|
header = _get_global_header(im, info)
|
2013-05-23 17:45:11 +04:00
|
|
|
|
2015-04-01 16:47:01 +03:00
|
|
|
return header, used_palette_colors
|
2013-05-23 17:45:11 +04:00
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2017-02-18 13:23:42 +03:00
|
|
|
# To specify duration, add the time in milliseconds to getdata(),
|
|
|
|
# e.g. getdata(im_frame, duration=1000)
|
2014-08-26 17:47:10 +04:00
|
|
|
def getdata(im, offset=(0, 0), **params):
|
2017-02-25 05:50:49 +03:00
|
|
|
"""
|
|
|
|
Legacy Method
|
|
|
|
|
|
|
|
Return a list of strings representing this image.
|
|
|
|
The first string is a local image header, the rest contains
|
|
|
|
encoded image data.
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2017-02-25 13:29:54 +03:00
|
|
|
:param im: Image object
|
|
|
|
:param offset: Tuple of (x, y) pixels. Defaults to (0,0)
|
2018-02-05 21:53:45 +03:00
|
|
|
:param \\**params: E.g. duration or other encoder info parameters
|
2017-02-25 13:29:54 +03:00
|
|
|
:returns: List of Bytes containing gif encoded frame data
|
|
|
|
|
2017-02-25 05:50:49 +03:00
|
|
|
"""
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2019-09-30 17:56:31 +03:00
|
|
|
class Collector:
|
2010-07-31 06:52:47 +04:00
|
|
|
data = []
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def write(self, data):
|
|
|
|
self.data.append(data)
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
im.load() # make sure raster data is available
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-04-01 16:47:01 +03:00
|
|
|
fp = Collector()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2017-02-18 11:09:10 +03:00
|
|
|
_write_frame_data(fp, im, offset, params)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
return fp.data
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Registry
|
|
|
|
|
|
|
|
Image.register_open(GifImageFile.format, GifImageFile, _accept)
|
|
|
|
Image.register_save(GifImageFile.format, _save)
|
2015-06-30 11:02:48 +03:00
|
|
|
Image.register_save_all(GifImageFile.format, _save_all)
|
2010-07-31 06:52:47 +04:00
|
|
|
Image.register_extension(GifImageFile.format, ".gif")
|
|
|
|
Image.register_mime(GifImageFile.format, "image/gif")
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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)
|