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.
|
|
|
|
#
|
|
|
|
|
2015-08-21 15:09:05 +03:00
|
|
|
from PIL import Image, ImageFile, ImagePalette, \
|
|
|
|
ImageChops, ImageSequence, _binary
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
__version__ = "0.9"
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Helpers
|
|
|
|
|
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
|
|
|
i8 = _binary.i8
|
|
|
|
i16 = _binary.i16le
|
|
|
|
o8 = _binary.o8
|
|
|
|
o16 = _binary.o16le
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Identify/read GIF files
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
class GifImageFile(ImageFile.ImageFile):
|
|
|
|
|
|
|
|
format = "GIF"
|
|
|
|
format_description = "Compuserve GIF"
|
|
|
|
global_palette = None
|
|
|
|
|
|
|
|
def data(self):
|
|
|
|
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 s and i8(s):
|
|
|
|
return self.fp.read(i8(s))
|
2010-07-31 06:52:47 +04:00
|
|
|
return None
|
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
|
|
|
|
# Screen
|
|
|
|
s = self.fp.read(13)
|
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 s[:6] not in [b"GIF87a", b"GIF89a"]:
|
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]
|
|
|
|
self.size = i16(s[6:]), i16(s[8:])
|
|
|
|
self.tile = []
|
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
|
|
|
flags = i8(s[10])
|
2010-07-31 06:52:47 +04:00
|
|
|
bits = (flags & 7) + 1
|
|
|
|
|
|
|
|
if flags & 128:
|
|
|
|
# get global palette
|
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
|
|
|
self.info["background"] = i8(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):
|
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 (i//3 == i8(p[i]) == i8(p[i+1]) == i8(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:
|
|
|
|
current = self.tell()
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.seek(1)
|
|
|
|
self._is_animated = True
|
|
|
|
except EOFError:
|
|
|
|
self._is_animated = False
|
|
|
|
|
|
|
|
self.seek(current)
|
|
|
|
return self._is_animated
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def seek(self, frame):
|
2015-04-15 03:43:05 +03:00
|
|
|
if frame == self.__frame:
|
|
|
|
return
|
|
|
|
if frame < self.__frame:
|
|
|
|
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)
|
|
|
|
except EOFError:
|
|
|
|
self.seek(last_frame)
|
|
|
|
raise EOFError("no more images in GIF file")
|
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._prev_im = None
|
|
|
|
self.disposal_method = 0
|
2014-07-07 22:46:54 +04:00
|
|
|
else:
|
|
|
|
# ensure that the previous frame was loaded
|
|
|
|
if not self.im:
|
|
|
|
self.load()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if frame != self.__frame + 1:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise ValueError("cannot seek to frame %d" % 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
|
|
|
|
self.palette = copy(self.global_palette)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
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()
|
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 i8(s) == 249:
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# graphic control extension
|
|
|
|
#
|
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
|
|
|
flags = i8(block[0])
|
2010-07-31 06:52:47 +04:00
|
|
|
if flags & 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
|
|
|
self.info["transparency"] = i8(block[3])
|
2010-07-31 06:52:47 +04:00
|
|
|
self.info["duration"] = i16(block[1:3]) * 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
|
2016-05-07 06:57:40 +03:00
|
|
|
elif i8(s) == 254:
|
|
|
|
#
|
|
|
|
# comment extension
|
|
|
|
#
|
|
|
|
self.info["comment"] = block
|
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 i8(s) == 255:
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# application extension
|
|
|
|
#
|
|
|
|
self.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()
|
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 len(block) >= 3 and i8(block[0]) == 1:
|
2010-07-31 06:52:47 +04:00
|
|
|
self.info["loop"] = i16(block[1:3])
|
|
|
|
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
|
|
|
|
x0, y0 = i16(s[0:]), i16(s[2:])
|
|
|
|
x1, y1 = x0 + i16(s[4:]), y0 + i16(s[6:])
|
2014-07-02 23:27:52 +04:00
|
|
|
self.dispose_extent = x0, y0, x1, y1
|
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
|
|
|
flags = i8(s[8])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
interlace = (flags & 64) != 0
|
|
|
|
|
|
|
|
if flags & 128:
|
|
|
|
bits = (flags & 7) + 1
|
|
|
|
self.palette =\
|
2014-08-26 17:47:10 +04:00
|
|
|
ImagePalette.raw("RGB", self.fp.read(3 << bits))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# image data
|
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
|
|
|
bits = i8(self.fp.read(1))
|
2010-07-31 06:52:47 +04:00
|
|
|
self.__offset = self.fp.tell()
|
|
|
|
self.tile = [("gif",
|
|
|
|
(x0, y0, x1, y1),
|
|
|
|
self.__offset,
|
|
|
|
(bits, interlace))]
|
|
|
|
break
|
|
|
|
|
|
|
|
else:
|
|
|
|
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
|
|
|
# raise IOError, "illegal GIF tag `%x`" % i8(s)
|
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
|
|
|
|
self.dispose = Image.core.fill("P", self.size,
|
|
|
|
self.info["background"])
|
|
|
|
else:
|
|
|
|
# replace with previous contents
|
|
|
|
if self.im:
|
|
|
|
self.dispose = self.im.copy()
|
|
|
|
|
|
|
|
# only dispose the extent in this frame
|
|
|
|
if self.dispose:
|
|
|
|
self.dispose = self.dispose.crop(self.dispose_extent)
|
|
|
|
except (AttributeError, KeyError):
|
|
|
|
pass
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
if not self.tile:
|
|
|
|
# self.__fp = None
|
2015-06-18 17:49:18 +03:00
|
|
|
raise EOFError
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
self.mode = "L"
|
|
|
|
if self.palette:
|
|
|
|
self.mode = "P"
|
|
|
|
|
|
|
|
def tell(self):
|
|
|
|
return self.__frame
|
|
|
|
|
2014-07-03 20:48:12 +04:00
|
|
|
def load_end(self):
|
|
|
|
ImageFile.ImageFile.load_end(self)
|
|
|
|
|
|
|
|
# if the disposal method is 'do not dispose', transparent
|
|
|
|
# pixels should show the content of the previous frame
|
|
|
|
if self._prev_im and self.disposal_method == 1:
|
|
|
|
# we do this by pasting the updated area onto the previous
|
|
|
|
# frame which we then use as the current image content
|
|
|
|
updated = self.im.crop(self.dispose_extent)
|
2014-08-26 17:47:10 +04:00
|
|
|
self._prev_im.paste(updated, self.dispose_extent,
|
|
|
|
updated.convert('RGBA'))
|
2014-07-03 20:48:12 +04:00
|
|
|
self.im = self._prev_im
|
|
|
|
self._prev_im = self.im.copy()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Write GIF files
|
|
|
|
|
|
|
|
try:
|
|
|
|
import _imaging_gif
|
|
|
|
except ImportError:
|
|
|
|
_imaging_gif = None
|
|
|
|
|
|
|
|
RAWMODE = {
|
|
|
|
"1": "L",
|
|
|
|
"L": "L",
|
|
|
|
"P": "P",
|
|
|
|
}
|
|
|
|
|
2015-07-01 02:18:05 +03:00
|
|
|
|
2015-08-24 16:23:07 +03:00
|
|
|
def _convert_mode(im, initial_call=False):
|
2015-07-01 02:18:05 +03:00
|
|
|
# convert on the fly (EXPERIMENTAL -- I'm not sure PIL
|
|
|
|
# should automatically convert images on save...)
|
|
|
|
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
|
|
|
|
return im.convert("P", palette=1, colors=palette_size)
|
|
|
|
else:
|
|
|
|
return im.convert("P")
|
2015-07-01 02:18:05 +03:00
|
|
|
return im.convert("L")
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-08-21 15:09:05 +03:00
|
|
|
im.encoderinfo.update(im.info)
|
2010-07-31 06:52:47 +04:00
|
|
|
if _imaging_gif:
|
|
|
|
# call external driver
|
|
|
|
try:
|
|
|
|
_imaging_gif.save(im, fp, filename)
|
|
|
|
return
|
|
|
|
except IOError:
|
2014-08-26 17:47:10 +04:00
|
|
|
pass # write uncompressed file
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-07 17:23:08 +04:00
|
|
|
if im.mode in RAWMODE:
|
2015-05-13 09:39:25 +03:00
|
|
|
im_out = im.copy()
|
2014-08-07 17:23:08 +04:00
|
|
|
else:
|
2015-08-24 16:23:07 +03:00
|
|
|
im_out = _convert_mode(im, True)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# header
|
2013-01-10 23:34:58 +04:00
|
|
|
try:
|
|
|
|
palette = im.encoderinfo["palette"]
|
|
|
|
except KeyError:
|
|
|
|
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
|
|
|
|
2015-06-30 11:02:48 +03:00
|
|
|
if save_all:
|
|
|
|
previous = None
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-08-24 16:51:02 +03:00
|
|
|
first_frame = None
|
2016-09-11 05:04:01 +03:00
|
|
|
append_images = im.encoderinfo.get("append_images", [])
|
|
|
|
for imSequence in [im]+append_images:
|
|
|
|
for im_frame in ImageSequence.Iterator(imSequence):
|
|
|
|
encoderinfo = im.encoderinfo.copy()
|
|
|
|
im_frame = _convert_mode(im_frame)
|
|
|
|
|
|
|
|
# To specify duration, add the time in milliseconds to getdata(),
|
|
|
|
# e.g. getdata(im_frame, duration=1000)
|
|
|
|
if not previous:
|
|
|
|
# global header
|
|
|
|
first_frame = getheader(im_frame, palette, encoderinfo)[0]
|
|
|
|
first_frame += getdata(im_frame, (0, 0), **encoderinfo)
|
2015-06-30 11:02:48 +03:00
|
|
|
else:
|
2016-09-11 05:04:01 +03:00
|
|
|
if first_frame:
|
|
|
|
for s in first_frame:
|
|
|
|
fp.write(s)
|
|
|
|
first_frame = None
|
|
|
|
|
|
|
|
# delta frame
|
|
|
|
delta = ImageChops.subtract_modulo(im_frame, previous.copy())
|
|
|
|
bbox = delta.getbbox()
|
|
|
|
|
|
|
|
if bbox:
|
|
|
|
# compress difference
|
|
|
|
encoderinfo['include_color_table'] = True
|
|
|
|
for s in getdata(im_frame.crop(bbox),
|
|
|
|
bbox[:2], **encoderinfo):
|
|
|
|
fp.write(s)
|
|
|
|
else:
|
|
|
|
# FIXME: what should we do in this case?
|
|
|
|
pass
|
|
|
|
previous = im_frame
|
2015-08-24 16:51:02 +03:00
|
|
|
if first_frame:
|
|
|
|
save_all = False
|
|
|
|
if not save_all:
|
2015-06-30 11:02:48 +03:00
|
|
|
header = getheader(im_out, palette, im.encoderinfo)[0]
|
|
|
|
for s in header:
|
|
|
|
fp.write(s)
|
2015-06-09 13:21:00 +03:00
|
|
|
|
2015-06-30 11:02:48 +03:00
|
|
|
flags = 0
|
2015-06-09 13:21:00 +03:00
|
|
|
|
2015-06-30 11:02:48 +03:00
|
|
|
if get_interlace(im):
|
|
|
|
flags = flags | 64
|
2015-04-03 15:22:13 +03:00
|
|
|
|
2015-06-30 11:02:48 +03:00
|
|
|
# local image header
|
|
|
|
_get_local_header(fp, im, (0, 0), flags)
|
|
|
|
|
|
|
|
im_out.encoderconfig = (8, get_interlace(im))
|
|
|
|
ImageFile._save(im_out, fp, [("gif", (0, 0)+im.size, 0,
|
|
|
|
RAWMODE[im_out.mode])])
|
2015-04-03 15:22:13 +03:00
|
|
|
|
2015-06-30 11:02:48 +03:00
|
|
|
fp.write(b"\0") # end of image data
|
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):
|
2010-07-31 06:52:47 +04:00
|
|
|
try:
|
|
|
|
interlace = im.encoderinfo["interlace"]
|
|
|
|
except KeyError:
|
|
|
|
interlace = 1
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
2015-06-30 11:02:48 +03:00
|
|
|
def _get_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
|
|
|
|
2015-04-23 17:44:27 +03:00
|
|
|
if _get_optimize(im, im.encoderinfo):
|
|
|
|
used_palette_colors = _get_used_palette_colors(im)
|
|
|
|
|
|
|
|
# adjust the transparency index after optimize
|
|
|
|
if len(used_palette_colors) < 256:
|
|
|
|
for i in range(len(used_palette_colors)):
|
|
|
|
if used_palette_colors[i] == transparency:
|
|
|
|
transparency = i
|
|
|
|
transparent_color_exists = True
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
if transparent_color_exists or duration != 0:
|
|
|
|
transparency_flag = 1 if transparent_color_exists else 0
|
|
|
|
if not transparent_color_exists:
|
|
|
|
transparency = 0
|
2015-04-23 16:40:42 +03:00
|
|
|
|
2015-04-03 15:22:13 +03:00
|
|
|
fp.write(b"!" +
|
2015-04-23 16:40:42 +03:00
|
|
|
o8(249) + # extension intro
|
|
|
|
o8(4) + # length
|
2016-09-11 04:57:45 +03:00
|
|
|
o8(transparency_flag) + # packed fields
|
2015-04-23 16:40:42 +03:00
|
|
|
o16(duration) + # duration
|
|
|
|
o8(transparency) + # transparency index
|
2015-04-03 15:22:13 +03:00
|
|
|
o8(0))
|
|
|
|
|
2016-05-07 06:57:40 +03:00
|
|
|
if "comment" in im.encoderinfo and 1 <= len(im.encoderinfo["comment"]) <= 255:
|
|
|
|
fp.write(b"!" +
|
|
|
|
o8(254) + # extension intro
|
|
|
|
o8(len(im.encoderinfo["comment"])) +
|
|
|
|
im.encoderinfo["comment"] +
|
|
|
|
o8(0))
|
2015-04-04 02:32:17 +03:00
|
|
|
if "loop" in im.encoderinfo:
|
|
|
|
number_of_loops = im.encoderinfo["loop"]
|
|
|
|
fp.write(b"!" +
|
2015-04-23 16:40:42 +03:00
|
|
|
o8(255) + # extension intro
|
2015-04-04 03:45:30 +03:00
|
|
|
o8(11) +
|
2015-04-04 02:32:17 +03:00
|
|
|
b"NETSCAPE2.0" +
|
|
|
|
o8(3) +
|
|
|
|
o8(1) +
|
2015-04-23 16:40:42 +03:00
|
|
|
o16(number_of_loops) + # number of loops
|
2015-04-04 02:32:17 +03:00
|
|
|
o8(0))
|
2016-09-11 04:57:45 +03:00
|
|
|
include_color_table = im.encoderinfo.get('include_color_table')
|
|
|
|
if include_color_table:
|
|
|
|
try:
|
|
|
|
palette = im.encoderinfo["palette"]
|
|
|
|
except KeyError:
|
|
|
|
palette = None
|
|
|
|
palette_bytes = _get_palette_bytes(im, palette, im.encoderinfo)[0]
|
|
|
|
color_table_size = _get_color_table_size(palette_bytes)
|
|
|
|
if color_table_size:
|
|
|
|
flags = flags | 128 # local color table flag
|
|
|
|
flags = flags | color_table_size
|
|
|
|
|
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
|
|
|
fp.write(b"," +
|
2015-04-23 16:40:42 +03:00
|
|
|
o16(offset[0]) + # offset
|
2015-04-03 15:22:13 +03:00
|
|
|
o16(offset[1]) +
|
2015-04-23 16:40:42 +03:00
|
|
|
o16(im.size[0]) + # size
|
2010-07-31 06:52:47 +04:00
|
|
|
o16(im.size[1]) +
|
2016-09-11 04:57:45 +03:00
|
|
|
o8(flags)) # flags
|
|
|
|
if include_color_table and color_table_size:
|
|
|
|
fp.write(_get_header_palette(palette_bytes))
|
|
|
|
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):
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import os
|
2014-06-27 19:02:36 +04:00
|
|
|
from subprocess import Popen, check_call, PIPE, CalledProcessError
|
2014-08-26 17:47:10 +04:00
|
|
|
import tempfile
|
2010-07-31 06:52:47 +04:00
|
|
|
file = im._dump()
|
2014-06-30 01:24:32 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
if im.mode != "RGB":
|
2014-06-27 08:28:44 +04:00
|
|
|
with open(filename, 'wb') as f:
|
2014-06-30 01:24:32 +04:00
|
|
|
stderr = tempfile.TemporaryFile()
|
|
|
|
check_call(["ppmtogif", file], stdout=f, stderr=stderr)
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2014-06-27 08:28:44 +04:00
|
|
|
with open(filename, 'wb') as f:
|
2014-06-27 19:02:36 +04:00
|
|
|
|
|
|
|
# Pipe ppmquant output into ppmtogif
|
|
|
|
# "ppmquant 256 %s | ppmtogif > %s" % (file, filename)
|
|
|
|
quant_cmd = ["ppmquant", "256", file]
|
|
|
|
togif_cmd = ["ppmtogif"]
|
2014-06-30 01:24:32 +04:00
|
|
|
stderr = tempfile.TemporaryFile()
|
|
|
|
quant_proc = Popen(quant_cmd, stdout=PIPE, stderr=stderr)
|
|
|
|
stderr = tempfile.TemporaryFile()
|
2014-08-26 17:47:10 +04:00
|
|
|
togif_proc = Popen(togif_cmd, stdin=quant_proc.stdout, stdout=f,
|
|
|
|
stderr=stderr)
|
|
|
|
|
2014-06-27 19:02:36 +04:00
|
|
|
# Allow ppmquant to receive SIGPIPE if ppmtogif exits
|
|
|
|
quant_proc.stdout.close()
|
|
|
|
|
|
|
|
retcode = quant_proc.wait()
|
|
|
|
if retcode:
|
|
|
|
raise CalledProcessError(retcode, quant_cmd)
|
|
|
|
|
|
|
|
retcode = togif_proc.wait()
|
|
|
|
if retcode:
|
|
|
|
raise CalledProcessError(retcode, togif_cmd)
|
|
|
|
|
2014-06-27 08:28:44 +04:00
|
|
|
try:
|
|
|
|
os.unlink(file)
|
2015-12-02 08:23:49 +03:00
|
|
|
except OSError:
|
2014-06-27 08:28:44 +04:00
|
|
|
pass
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# GIF utilities
|
|
|
|
|
2015-04-23 17:44:27 +03:00
|
|
|
def _get_optimize(im, info):
|
|
|
|
return im.mode in ("P", "L") and info and info.get("optimize", 0)
|
|
|
|
|
|
|
|
|
|
|
|
def _get_used_palette_colors(im):
|
|
|
|
used_palette_colors = []
|
|
|
|
|
|
|
|
# check which colors are used
|
|
|
|
i = 0
|
|
|
|
for count in im.histogram():
|
|
|
|
if count:
|
|
|
|
used_palette_colors.append(i)
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
return used_palette_colors
|
|
|
|
|
2016-09-11 04:57:45 +03:00
|
|
|
def _get_color_table_size(palette_bytes):
|
|
|
|
# calculate the palette size for the header
|
|
|
|
import math
|
|
|
|
color_table_size = int(math.ceil(math.log(len(palette_bytes)//3, 2)))-1
|
|
|
|
if color_table_size < 0:
|
|
|
|
color_table_size = 0
|
|
|
|
return color_table_size
|
2015-04-23 17:44:27 +03:00
|
|
|
|
2016-09-11 04:57:45 +03:00
|
|
|
def _get_header_palette(palette_bytes):
|
|
|
|
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
|
|
|
|
actual_target_size_diff = (2 << color_table_size) - len(palette_bytes)//3
|
|
|
|
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
|
|
|
|
2016-09-27 00:44:40 +03:00
|
|
|
# Force optimization so that we can test performance against
|
|
|
|
# cases where it took lots of memory and time previously.
|
|
|
|
_FORCE_OPTIMIZE = False
|
|
|
|
|
2016-09-11 04:57:45 +03:00
|
|
|
def _get_palette_bytes(im, palette, info):
|
2013-08-05 19:59:35 +04:00
|
|
|
if im.mode == "P":
|
|
|
|
if palette and isinstance(palette, bytes):
|
2015-04-01 16:47:01 +03:00
|
|
|
source_palette = palette[:768]
|
2013-08-05 19:59:35 +04:00
|
|
|
else:
|
2015-04-01 16:47:01 +03:00
|
|
|
source_palette = im.im.getpalette("RGB")[:768]
|
2014-08-26 17:47:10 +04:00
|
|
|
else: # L-mode
|
2013-08-06 13:36:02 +04:00
|
|
|
if palette and isinstance(palette, bytes):
|
2015-04-01 16:47:01 +03:00
|
|
|
source_palette = palette[:768]
|
2013-08-06 13:36:02 +04:00
|
|
|
else:
|
2015-04-01 16:47:01 +03:00
|
|
|
source_palette = bytearray([i//3 for i in range(768)])
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2015-04-01 16:47:01 +03:00
|
|
|
used_palette_colors = palette_bytes = None
|
2013-08-05 19:59:35 +04:00
|
|
|
|
2015-04-23 17:44:27 +03:00
|
|
|
if _get_optimize(im, info):
|
|
|
|
used_palette_colors = _get_used_palette_colors(im)
|
2013-08-05 19:59:35 +04:00
|
|
|
|
2016-09-27 00:44:40 +03:00
|
|
|
# Potentially expensive operation.
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
2013-08-05 19:59:35 +04:00
|
|
|
# create the new palette if not every color is used
|
2016-09-27 00:44:40 +03:00
|
|
|
if _FORCE_OPTIMIZE or im.mode == 'L' or \
|
|
|
|
(len(used_palette_colors) <= 128 and
|
|
|
|
max(used_palette_colors) > len(used_palette_colors) and
|
|
|
|
im.width * im.height < 512 * 512):
|
2015-04-01 16:47:01 +03:00
|
|
|
palette_bytes = b""
|
2016-09-27 00:44:40 +03:00
|
|
|
new_positions = [0]*256
|
2013-08-05 19:59:35 +04:00
|
|
|
|
2013-08-06 13:36:02 +04:00
|
|
|
# pick only the used colors from the palette
|
2016-09-27 00:44:40 +03:00
|
|
|
for i, oldPosition in enumerate(used_palette_colors):
|
2015-04-01 16:47:01 +03:00
|
|
|
palette_bytes += source_palette[oldPosition*3:oldPosition*3+3]
|
|
|
|
new_positions[oldPosition] = i
|
2013-05-23 17:45:11 +04:00
|
|
|
|
2013-08-05 19:59:35 +04:00
|
|
|
# replace the palette color id of all pixel with the new id
|
2016-09-27 00:44:40 +03:00
|
|
|
|
|
|
|
# Palette images are [0..255], mapped through a 1 or 3
|
|
|
|
# byte/color map. We need to remap the whole image
|
|
|
|
# from palette 1 to palette 2. New_positions is
|
|
|
|
# an array of indexes into palette 1. Palette 2 is
|
|
|
|
# palette 1 with any holes removed.
|
|
|
|
|
|
|
|
# We're going to leverage the convert mechanism to use the
|
|
|
|
# C code to remap the image from palette 1 to palette 2,
|
|
|
|
# by forcing the source image into 'L' mode and adding a
|
|
|
|
# mapping 'L' mode palette, then converting back to 'L'
|
|
|
|
# sans palette thus converting the image bytes, then
|
|
|
|
# assigning the optimized RGB palette.
|
|
|
|
|
|
|
|
# perf reference, 9500x4000 gif, w/~135 colors
|
|
|
|
# 14 sec prepatch, 1 sec postpatch with optimization forced.
|
|
|
|
|
|
|
|
mapping_palette = bytearray(new_positions)
|
|
|
|
|
|
|
|
m_im = im.copy()
|
|
|
|
m_im.mode = 'P'
|
|
|
|
|
|
|
|
m_im.palette = ImagePalette.ImagePalette("RGB",
|
|
|
|
palette=mapping_palette*3,
|
|
|
|
size=768)
|
|
|
|
#possibly set palette dirty, then
|
|
|
|
#m_im.putpalette(mapping_palette, 'L') # converts to 'P'
|
|
|
|
# or just force it.
|
|
|
|
# UNDONE -- this is part of the general issue with palettes
|
|
|
|
m_im.im.putpalette(*m_im.palette.getdata())
|
|
|
|
|
|
|
|
m_im = m_im.convert('L')
|
|
|
|
|
|
|
|
# Internally, we require 768 bytes for a palette.
|
2015-04-01 16:47:01 +03:00
|
|
|
new_palette_bytes = (palette_bytes +
|
|
|
|
(768 - len(palette_bytes)) * b'\x00')
|
2016-09-27 00:44:40 +03:00
|
|
|
m_im.putpalette(new_palette_bytes)
|
|
|
|
m_im.palette = ImagePalette.ImagePalette("RGB",
|
2015-04-01 16:47:01 +03:00
|
|
|
palette=palette_bytes,
|
|
|
|
size=len(palette_bytes))
|
|
|
|
|
2016-09-27 00:44:40 +03:00
|
|
|
# oh gawd, this is modifying the image in place so I can pass by ref.
|
|
|
|
# REFACTOR SOONEST
|
|
|
|
im.frombytes(m_im.tobytes())
|
|
|
|
|
2015-04-01 16:47:01 +03:00
|
|
|
if not palette_bytes:
|
|
|
|
palette_bytes = source_palette
|
2016-09-27 00:44:40 +03:00
|
|
|
|
|
|
|
# returning palette, _not_ padded to 768 bytes like our internal ones.
|
2016-09-11 04:57:45 +03:00
|
|
|
return palette_bytes, used_palette_colors
|
|
|
|
|
|
|
|
def getheader(im, palette=None, info=None):
|
|
|
|
"""Return a list of strings representing a GIF header"""
|
|
|
|
|
|
|
|
# Header Block
|
|
|
|
# http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
|
|
|
|
|
|
|
|
version = b"87a"
|
|
|
|
for extensionKey in ["transparency", "duration", "loop", "comment"]:
|
|
|
|
if info and extensionKey in info:
|
|
|
|
if ((extensionKey == "duration" and info[extensionKey] == 0) or
|
|
|
|
(extensionKey == "comment" and not (1 <= len(info[extensionKey]) <= 255))):
|
|
|
|
continue
|
|
|
|
version = b"89a"
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if im.info.get("version") == "89a":
|
|
|
|
version = b"89a"
|
|
|
|
|
|
|
|
header = [
|
|
|
|
b"GIF"+version + # signature + version
|
|
|
|
o16(im.size[0]) + # canvas width
|
|
|
|
o16(im.size[1]) # canvas height
|
|
|
|
]
|
|
|
|
|
|
|
|
palette_bytes, used_palette_colors = _get_palette_bytes(im, palette, info)
|
2013-08-05 19:59:35 +04:00
|
|
|
|
|
|
|
# Logical Screen Descriptor
|
2016-09-11 04:57:45 +03:00
|
|
|
color_table_size = _get_color_table_size(palette_bytes)
|
2013-08-05 19:59:35 +04:00
|
|
|
# size of global color table + global color table flag
|
2016-09-11 04:57:45 +03:00
|
|
|
header.append(o8(color_table_size + 128)) # packed fields
|
2013-08-05 19:59:35 +04:00
|
|
|
# background + reserved/aspect
|
2015-08-21 15:09:05 +03:00
|
|
|
if info and "background" in info:
|
|
|
|
background = info["background"]
|
|
|
|
elif "background" in im.info:
|
|
|
|
# This elif is redundant within GifImagePlugin
|
|
|
|
# since im.info parameters are bundled into the info dictionary
|
|
|
|
# However, external scripts may call getheader directly
|
|
|
|
# So this maintains earlier behaviour
|
|
|
|
background = im.info["background"]
|
|
|
|
else:
|
|
|
|
background = 0
|
2015-06-11 04:10:05 +03:00
|
|
|
header.append(o8(background) + o8(0))
|
2013-08-05 19:59:35 +04:00
|
|
|
# end of Logical Screen Descriptor
|
2013-05-23 17:45:11 +04:00
|
|
|
|
2013-08-05 19:59:35 +04:00
|
|
|
# Header + Logical Screen Descriptor + Global Color Table
|
2016-09-11 04:57:45 +03:00
|
|
|
header.append(_get_header_palette(palette_bytes))
|
2015-04-01 16:47:01 +03:00
|
|
|
return header, used_palette_colors
|
2013-05-23 17:45:11 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
def getdata(im, offset=(0, 0), **params):
|
2010-07-31 06:52:47 +04:00
|
|
|
"""Return a list of strings representing this image.
|
|
|
|
The first string is a local image header, the rest contains
|
|
|
|
encoded image data."""
|
|
|
|
|
2015-05-26 17:07:21 +03:00
|
|
|
class Collector(object):
|
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
|
|
|
|
|
|
|
try:
|
|
|
|
im.encoderinfo = params
|
|
|
|
|
|
|
|
# local image header
|
2015-06-30 11:02:48 +03:00
|
|
|
_get_local_header(fp, im, offset, 0)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
ImageFile._save(im, fp, [("gif", (0, 0)+im.size, 0, RAWMODE[im.mode])])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
fp.write(b"\0") # end of image data
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
finally:
|
|
|
|
del im.encoderinfo
|
|
|
|
|
|
|
|
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)
|