Merge pull request #6077 from radarhere/gif

This commit is contained in:
Hugo van Kemenade 2022-03-11 23:05:10 +02:00 committed by GitHub
commit 3e16c3dc28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -97,7 +97,7 @@ class GifImageFile(ImageFile.ImageFile):
current = self.tell() current = self.tell()
try: try:
while True: while True:
self.seek(self.tell() + 1) self._seek(self.tell() + 1, False)
except EOFError: except EOFError:
self._n_frames = self.tell() + 1 self._n_frames = self.tell() + 1
self.seek(current) self.seek(current)
@ -110,9 +110,11 @@ class GifImageFile(ImageFile.ImageFile):
self._is_animated = self._n_frames != 1 self._is_animated = self._n_frames != 1
else: else:
current = self.tell() current = self.tell()
if current:
self._is_animated = True
else:
try: try:
self.seek(1) self._seek(1, False)
self._is_animated = True self._is_animated = True
except EOFError: except EOFError:
self._is_animated = False self._is_animated = False
@ -135,7 +137,7 @@ class GifImageFile(ImageFile.ImageFile):
self.seek(last_frame) self.seek(last_frame)
raise EOFError("no more images in GIF file") from e raise EOFError("no more images in GIF file") from e
def _seek(self, frame): def _seek(self, frame, update_image=True):
if frame == 0: if frame == 0:
# rewind # rewind
@ -147,14 +149,11 @@ class GifImageFile(ImageFile.ImageFile):
self.disposal_method = 0 self.disposal_method = 0
else: else:
# ensure that the previous frame was loaded # ensure that the previous frame was loaded
if self.tile: if self.tile and update_image:
self.load() self.load()
if frame != self.__frame + 1: if frame != self.__frame + 1:
raise ValueError(f"cannot seek to frame {frame}") raise ValueError(f"cannot seek to frame {frame}")
self.__frame = frame
self.tile = []
self.fp = self.__fp self.fp = self.__fp
if self.__offset: if self.__offset:
@ -164,6 +163,15 @@ class GifImageFile(ImageFile.ImageFile):
pass pass
self.__offset = 0 self.__offset = 0
s = self.fp.read(1)
if not s or s == b";":
raise EOFError
self.__frame = frame
self.tile = []
if update_image:
if self.__frame == 1: if self.__frame == 1:
self.pyaccess = None self.pyaccess = None
if "transparency" in self.info: if "transparency" in self.info:
@ -191,6 +199,7 @@ class GifImageFile(ImageFile.ImageFile):
interlace = None interlace = None
while True: while True:
if not s:
s = self.fp.read(1) s = self.fp.read(1)
if not s or s == b";": if not s or s == b";":
break break
@ -229,6 +238,7 @@ class GifImageFile(ImageFile.ImageFile):
else: else:
info["comment"] = block info["comment"] = block
block = self.data() block = self.data()
s = None
continue continue
elif s[0] == 255: elif s[0] == 255:
# #
@ -270,6 +280,13 @@ class GifImageFile(ImageFile.ImageFile):
else: else:
pass pass
# raise OSError, "illegal GIF tag `%x`" % s[0] # raise OSError, "illegal GIF tag `%x`" % s[0]
s = None
if interlace is None:
# self.__fp = None
raise EOFError
if not update_image:
return
frame_palette = palette or self.global_palette frame_palette = palette or self.global_palette
@ -329,9 +346,6 @@ class GifImageFile(ImageFile.ImageFile):
(bits, interlace), (bits, interlace),
) )
] ]
else:
# self.__fp = None
raise EOFError
for k in ["duration", "comment", "extension", "loop"]: for k in ["duration", "comment", "extension", "loop"]:
if k in info: if k in info: