2019-07-06 23:40:53 +03:00
|
|
|
from io import BytesIO
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-03 12:11:32 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
from PIL import GifImagePlugin, Image, ImageDraw, ImagePalette, features
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
from .helper import (
|
|
|
|
assert_image_equal,
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile,
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar,
|
|
|
|
hopper,
|
|
|
|
is_pypy,
|
|
|
|
netpbm_available,
|
|
|
|
)
|
2016-09-27 00:44:40 +03:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
# sample gif stream
|
2014-09-04 09:44:46 +04:00
|
|
|
TEST_GIF = "Tests/images/hopper.gif"
|
|
|
|
|
|
|
|
with open(TEST_GIF, "rb") as f:
|
2013-05-23 21:02:19 +04:00
|
|
|
data = f.read()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_sanity():
|
|
|
|
with Image.open(TEST_GIF) as im:
|
|
|
|
im.load()
|
|
|
|
assert im.mode == "P"
|
|
|
|
assert im.size == (128, 128)
|
|
|
|
assert im.format == "GIF"
|
|
|
|
assert im.info["version"] == b"GIF89a"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
|
|
|
|
def test_unclosed_file():
|
|
|
|
def open():
|
|
|
|
im = Image.open(TEST_GIF)
|
|
|
|
im.load()
|
|
|
|
|
|
|
|
pytest.warns(ResourceWarning, open)
|
|
|
|
|
|
|
|
|
|
|
|
def test_closed_file():
|
2021-02-10 15:37:55 +03:00
|
|
|
with pytest.warns(None) as record:
|
2020-02-23 00:03:01 +03:00
|
|
|
im = Image.open(TEST_GIF)
|
|
|
|
im.load()
|
|
|
|
im.close()
|
|
|
|
|
2021-02-11 05:48:31 +03:00
|
|
|
assert not record
|
2020-02-23 00:03:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_context_manager():
|
2021-02-10 15:37:55 +03:00
|
|
|
with pytest.warns(None) as record:
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open(TEST_GIF) as im:
|
|
|
|
im.load()
|
2019-06-13 18:53:42 +03:00
|
|
|
|
2021-02-11 05:48:31 +03:00
|
|
|
assert not record
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
|
|
|
|
def test_invalid_file():
|
|
|
|
invalid_file = "Tests/images/flower.jpg"
|
|
|
|
|
|
|
|
with pytest.raises(SyntaxError):
|
|
|
|
GifImagePlugin.GifImageFile(invalid_file)
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize():
|
|
|
|
def test_grayscale(optimize):
|
|
|
|
im = Image.new("L", (1, 1), 0)
|
|
|
|
filename = BytesIO()
|
|
|
|
im.save(filename, "GIF", optimize=optimize)
|
|
|
|
return len(filename.getvalue())
|
|
|
|
|
|
|
|
def test_bilevel(optimize):
|
|
|
|
im = Image.new("1", (1, 1), 0)
|
2015-04-24 11:24:52 +03:00
|
|
|
test_file = BytesIO()
|
2020-02-23 00:03:01 +03:00
|
|
|
im.save(test_file, "GIF", optimize=optimize)
|
|
|
|
return len(test_file.getvalue())
|
|
|
|
|
2020-12-26 21:07:16 +03:00
|
|
|
assert test_grayscale(0) == 799
|
|
|
|
assert test_grayscale(1) == 43
|
|
|
|
assert test_bilevel(0) == 799
|
|
|
|
assert test_bilevel(1) == 799
|
2020-02-23 00:03:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_correctness():
|
|
|
|
# 256 color Palette image, posterize to > 128 and < 128 levels
|
|
|
|
# Size bigger and smaller than 512x512
|
|
|
|
# Check the palette for number of colors allocated.
|
|
|
|
# Check for correctness after conversion back to RGB
|
|
|
|
def check(colors, size, expected_palette_length):
|
|
|
|
# make an image with empty colors in the start of the palette range
|
|
|
|
im = Image.frombytes(
|
|
|
|
"P", (colors, colors), bytes(range(256 - colors, 256)) * colors
|
|
|
|
)
|
|
|
|
im = im.resize((size, size))
|
|
|
|
outfile = BytesIO()
|
|
|
|
im.save(outfile, "GIF")
|
|
|
|
outfile.seek(0)
|
|
|
|
with Image.open(outfile) as reloaded:
|
|
|
|
# check palette length
|
|
|
|
palette_length = max(i + 1 for i, v in enumerate(reloaded.histogram()) if v)
|
|
|
|
assert expected_palette_length == palette_length
|
2014-08-19 13:24:44 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
assert_image_equal(im.convert("RGB"), reloaded.convert("RGB"))
|
2013-11-08 04:39:36 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# These do optimize the palette
|
|
|
|
check(128, 511, 128)
|
|
|
|
check(64, 511, 64)
|
|
|
|
check(4, 511, 4)
|
2013-11-08 04:39:36 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# These don't optimize the palette
|
|
|
|
check(128, 513, 256)
|
|
|
|
check(64, 513, 256)
|
|
|
|
check(4, 513, 256)
|
2013-11-08 04:39:57 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Other limits that don't optimize the palette
|
|
|
|
check(129, 511, 256)
|
|
|
|
check(255, 511, 256)
|
|
|
|
check(256, 511, 256)
|
2013-11-08 04:39:57 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
|
|
|
|
def test_optimize_full_l():
|
|
|
|
im = Image.frombytes("L", (16, 16), bytes(range(256)))
|
|
|
|
test_file = BytesIO()
|
|
|
|
im.save(test_file, "GIF", optimize=True)
|
|
|
|
assert im.mode == "L"
|
|
|
|
|
|
|
|
|
|
|
|
def test_roundtrip(tmp_path):
|
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im = hopper()
|
|
|
|
im.save(out)
|
|
|
|
with Image.open(out) as reread:
|
|
|
|
|
|
|
|
assert_image_similar(reread.convert("RGB"), im, 50)
|
|
|
|
|
|
|
|
|
|
|
|
def test_roundtrip2(tmp_path):
|
|
|
|
# see https://github.com/python-pillow/Pillow/issues/403
|
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
with Image.open(TEST_GIF) as im:
|
|
|
|
im2 = im.copy()
|
|
|
|
im2.save(out)
|
|
|
|
with Image.open(out) as reread:
|
|
|
|
|
|
|
|
assert_image_similar(reread.convert("RGB"), hopper(), 50)
|
|
|
|
|
|
|
|
|
|
|
|
def test_roundtrip_save_all(tmp_path):
|
|
|
|
# Single frame image
|
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im = hopper()
|
|
|
|
im.save(out, save_all=True)
|
|
|
|
with Image.open(out) as reread:
|
|
|
|
|
|
|
|
assert_image_similar(reread.convert("RGB"), im, 50)
|
|
|
|
|
|
|
|
# Multiframe image
|
|
|
|
with Image.open("Tests/images/dispose_bgnd.gif") as im:
|
|
|
|
out = str(tmp_path / "temp.gif")
|
2015-06-30 11:07:23 +03:00
|
|
|
im.save(out, save_all=True)
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(out) as reread:
|
|
|
|
assert reread.n_frames == 5
|
2015-06-30 11:07:23 +03:00
|
|
|
|
2015-07-01 02:18:05 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_headers_saving_for_animated_gifs(tmp_path):
|
|
|
|
important_headers = ["background", "version", "duration", "loop"]
|
|
|
|
# Multiframe image
|
|
|
|
with Image.open("Tests/images/dispose_bgnd.gif") as im:
|
2015-07-01 02:18:05 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
info = im.info.copy()
|
2015-07-01 02:18:05 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im.save(out, save_all=True)
|
|
|
|
with Image.open(out) as reread:
|
2015-07-24 12:14:20 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
for header in important_headers:
|
|
|
|
assert info[header] == reread.info[header]
|
2018-09-01 02:28:22 +03:00
|
|
|
|
2015-07-24 12:14:20 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_palette_handling(tmp_path):
|
|
|
|
# see https://github.com/python-pillow/Pillow/issues/513
|
2015-07-24 12:14:20 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(TEST_GIF) as im:
|
|
|
|
im = im.convert("RGB")
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im = im.resize((100, 100), Image.LANCZOS)
|
|
|
|
im2 = im.convert("P", palette=Image.ADAPTIVE, colors=256)
|
2014-03-05 10:02:03 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
f = str(tmp_path / "temp.gif")
|
|
|
|
im2.save(f, optimize=True)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(f) as reloaded:
|
2014-03-05 10:02:03 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
assert_image_similar(im, reloaded.convert("RGB"), 10)
|
2014-03-05 10:02:03 +04:00
|
|
|
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_palette_434(tmp_path):
|
|
|
|
# see https://github.com/python-pillow/Pillow/issues/434
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def roundtrip(im, *args, **kwargs):
|
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im.copy().save(out, *args, **kwargs)
|
|
|
|
reloaded = Image.open(out)
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
return reloaded
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
orig = "Tests/images/test.colors.gif"
|
|
|
|
with Image.open(orig) as im:
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with roundtrip(im) as reloaded:
|
|
|
|
assert_image_similar(im, reloaded, 1)
|
|
|
|
with roundtrip(im, optimize=True) as reloaded:
|
|
|
|
assert_image_similar(im, reloaded, 1)
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im = im.convert("RGB")
|
|
|
|
# check automatic P conversion
|
|
|
|
with roundtrip(im) as reloaded:
|
|
|
|
reloaded = reloaded.convert("RGB")
|
|
|
|
assert_image_equal(im, reloaded)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-27 07:37:49 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
@pytest.mark.skipif(not netpbm_available(), reason="Netpbm not available")
|
|
|
|
def test_save_netpbm_bmp_mode(tmp_path):
|
|
|
|
with Image.open(TEST_GIF) as img:
|
|
|
|
img = img.convert("RGB")
|
2014-06-27 07:37:49 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
tempfile = str(tmp_path / "temp.gif")
|
|
|
|
GifImagePlugin._save_netpbm(img, 0, tempfile)
|
|
|
|
with Image.open(tempfile) as reloaded:
|
|
|
|
assert_image_similar(img, reloaded.convert("RGB"), 0)
|
2014-06-27 07:37:49 +04:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
@pytest.mark.skipif(not netpbm_available(), reason="Netpbm not available")
|
|
|
|
def test_save_netpbm_l_mode(tmp_path):
|
|
|
|
with Image.open(TEST_GIF) as img:
|
|
|
|
img = img.convert("L")
|
|
|
|
|
|
|
|
tempfile = str(tmp_path / "temp.gif")
|
|
|
|
GifImagePlugin._save_netpbm(img, 0, tempfile)
|
|
|
|
with Image.open(tempfile) as reloaded:
|
|
|
|
assert_image_similar(img, reloaded.convert("L"), 0)
|
2014-07-07 22:47:18 +04:00
|
|
|
|
2018-09-01 02:28:22 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_seek():
|
|
|
|
with Image.open("Tests/images/dispose_none.gif") as img:
|
|
|
|
frame_count = 0
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
frame_count += 1
|
|
|
|
img.seek(img.tell() + 1)
|
|
|
|
except EOFError:
|
|
|
|
assert frame_count == 5
|
2018-09-01 02:28:22 +03:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_seek_info():
|
|
|
|
with Image.open("Tests/images/iss634.gif") as im:
|
|
|
|
info = im.info.copy()
|
2019-03-16 12:02:24 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im.seek(1)
|
|
|
|
im.seek(0)
|
2019-03-16 12:02:24 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
assert im.info == info
|
2017-08-18 13:20:27 +03:00
|
|
|
|
2015-06-18 17:49:18 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_seek_rewind():
|
|
|
|
with Image.open("Tests/images/iss634.gif") as im:
|
|
|
|
im.seek(2)
|
|
|
|
im.seek(1)
|
2017-09-06 06:19:33 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open("Tests/images/iss634.gif") as expected:
|
|
|
|
expected.seek(1)
|
|
|
|
assert_image_equal(im, expected)
|
2017-09-06 06:19:33 +03:00
|
|
|
|
2015-06-07 18:01:34 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_n_frames():
|
|
|
|
for path, n_frames in [[TEST_GIF, 1], ["Tests/images/iss634.gif", 42]]:
|
|
|
|
# Test is_animated before n_frames
|
|
|
|
with Image.open(path) as im:
|
|
|
|
assert im.is_animated == (n_frames != 1)
|
2014-07-07 22:47:18 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Test is_animated after n_frames
|
|
|
|
with Image.open(path) as im:
|
|
|
|
assert im.n_frames == n_frames
|
|
|
|
assert im.is_animated == (n_frames != 1)
|
2014-07-07 22:47:18 +04:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_eoferror():
|
|
|
|
with Image.open(TEST_GIF) as im:
|
|
|
|
n_frames = im.n_frames
|
|
|
|
|
|
|
|
# Test seeking past the last frame
|
|
|
|
with pytest.raises(EOFError):
|
|
|
|
im.seek(n_frames)
|
|
|
|
assert im.tell() < n_frames
|
|
|
|
|
|
|
|
# Test that seeking to the last frame does not raise an error
|
|
|
|
im.seek(n_frames - 1)
|
2017-12-22 01:26:58 +03:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_dispose_none():
|
|
|
|
with Image.open("Tests/images/dispose_none.gif") as img:
|
|
|
|
try:
|
|
|
|
while True:
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
img.seek(img.tell() + 1)
|
2020-02-23 00:03:01 +03:00
|
|
|
assert img.disposal_method == 1
|
|
|
|
except EOFError:
|
|
|
|
pass
|
2017-01-31 10:22:54 +03:00
|
|
|
|
2019-03-15 02:29:33 +03:00
|
|
|
|
2020-12-23 05:22:53 +03:00
|
|
|
def test_dispose_none_load_end():
|
|
|
|
# Test image created with:
|
|
|
|
#
|
|
|
|
# im = Image.open("transparent.gif")
|
|
|
|
# im_rotated = im.rotate(180)
|
|
|
|
# im.save("dispose_none_load_end.gif",
|
|
|
|
# save_all=True, append_images=[im_rotated], disposal=[1,2])
|
|
|
|
with Image.open("Tests/images/dispose_none_load_end.gif") as img:
|
|
|
|
img.seek(1)
|
|
|
|
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(img, "Tests/images/dispose_none_load_end_second.gif")
|
2020-12-23 05:22:53 +03:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_dispose_background():
|
|
|
|
with Image.open("Tests/images/dispose_bgnd.gif") as img:
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
img.seek(img.tell() + 1)
|
|
|
|
assert img.disposal_method == 2
|
|
|
|
except EOFError:
|
|
|
|
pass
|
2019-03-15 02:29:33 +03:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_dispose_previous():
|
|
|
|
with Image.open("Tests/images/dispose_prev.gif") as img:
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
img.seek(img.tell() + 1)
|
|
|
|
assert img.disposal_method == 3
|
|
|
|
except EOFError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-04-15 12:01:12 +03:00
|
|
|
def test_dispose_previous_first_frame():
|
|
|
|
with Image.open("Tests/images/dispose_prev_first_frame.gif") as im:
|
|
|
|
im.seek(1)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
im, "Tests/images/dispose_prev_first_frame_seeked.gif"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-04-06 12:31:51 +03:00
|
|
|
def test_previous_frame_loaded():
|
|
|
|
with Image.open("Tests/images/dispose_none.gif") as img:
|
|
|
|
img.load()
|
|
|
|
img.seek(1)
|
|
|
|
img.load()
|
|
|
|
img.seek(2)
|
|
|
|
with Image.open("Tests/images/dispose_none.gif") as img_skipped:
|
|
|
|
img_skipped.seek(2)
|
|
|
|
assert_image_equal(img_skipped, img)
|
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_save_dispose(tmp_path):
|
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im_list = [
|
|
|
|
Image.new("L", (100, 100), "#000"),
|
|
|
|
Image.new("L", (100, 100), "#111"),
|
|
|
|
Image.new("L", (100, 100), "#222"),
|
|
|
|
]
|
|
|
|
for method in range(0, 4):
|
|
|
|
im_list[0].save(out, save_all=True, append_images=im_list[1:], disposal=method)
|
|
|
|
with Image.open(out) as img:
|
|
|
|
for _ in range(2):
|
|
|
|
img.seek(img.tell() + 1)
|
|
|
|
assert img.disposal_method == method
|
2019-03-14 23:40:31 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Check per frame disposal
|
|
|
|
im_list[0].save(
|
|
|
|
out,
|
|
|
|
save_all=True,
|
|
|
|
append_images=im_list[1:],
|
|
|
|
disposal=tuple(range(len(im_list))),
|
|
|
|
)
|
2019-03-15 02:29:33 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(out) as img:
|
2019-03-14 23:40:31 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
for i in range(2):
|
|
|
|
img.seek(img.tell() + 1)
|
|
|
|
assert img.disposal_method == i + 1
|
2019-03-14 23:40:31 +03:00
|
|
|
|
2019-03-15 02:29:33 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_dispose2_palette(tmp_path):
|
|
|
|
out = str(tmp_path / "temp.gif")
|
2019-03-22 17:19:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# 4 backgrounds: White, Grey, Black, Red
|
|
|
|
circles = [(255, 255, 255), (153, 153, 153), (0, 0, 0), (255, 0, 0)]
|
2019-03-22 17:19:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im_list = []
|
|
|
|
for circle in circles:
|
|
|
|
img = Image.new("RGB", (100, 100), (255, 0, 0))
|
2019-03-22 17:19:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Red circle in center of each frame
|
|
|
|
d = ImageDraw.Draw(img)
|
|
|
|
d.ellipse([(40, 40), (60, 60)], fill=circle)
|
2019-03-22 17:19:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im_list.append(img)
|
2019-03-22 17:19:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im_list[0].save(out, save_all=True, append_images=im_list[1:], disposal=2)
|
2019-03-22 17:19:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(out) as img:
|
|
|
|
for i, circle in enumerate(circles):
|
|
|
|
img.seek(i)
|
|
|
|
rgb_img = img.convert("RGB")
|
2019-03-22 17:19:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Check top left pixel matches background
|
|
|
|
assert rgb_img.getpixel((0, 0)) == (255, 0, 0)
|
2019-03-22 17:19:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Center remains red every frame
|
|
|
|
assert rgb_img.getpixel((50, 50)) == circle
|
2019-03-22 17:19:01 +03:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_dispose2_diff(tmp_path):
|
|
|
|
out = str(tmp_path / "temp.gif")
|
2019-03-14 23:40:31 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# 4 frames: red/blue, red/red, blue/blue, red/blue
|
|
|
|
circles = [
|
|
|
|
((255, 0, 0, 255), (0, 0, 255, 255)),
|
|
|
|
((255, 0, 0, 255), (255, 0, 0, 255)),
|
|
|
|
((0, 0, 255, 255), (0, 0, 255, 255)),
|
|
|
|
((255, 0, 0, 255), (0, 0, 255, 255)),
|
|
|
|
]
|
2019-06-29 16:06:45 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im_list = []
|
|
|
|
for i in range(len(circles)):
|
|
|
|
# Transparent BG
|
|
|
|
img = Image.new("RGBA", (100, 100), (255, 255, 255, 0))
|
2019-06-29 16:06:45 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Two circles per frame
|
|
|
|
d = ImageDraw.Draw(img)
|
|
|
|
d.ellipse([(0, 30), (40, 70)], fill=circles[i][0])
|
|
|
|
d.ellipse([(60, 30), (100, 70)], fill=circles[i][1])
|
2019-06-29 16:06:45 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im_list.append(img)
|
2019-06-29 16:06:45 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im_list[0].save(
|
|
|
|
out, save_all=True, append_images=im_list[1:], disposal=2, transparency=0
|
|
|
|
)
|
2019-06-29 16:06:45 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(out) as img:
|
|
|
|
for i, colours in enumerate(circles):
|
|
|
|
img.seek(i)
|
|
|
|
rgb_img = img.convert("RGBA")
|
2019-06-29 16:06:45 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Check left circle is correct colour
|
|
|
|
assert rgb_img.getpixel((20, 50)) == colours[0]
|
2014-07-07 22:47:18 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Check right circle is correct colour
|
|
|
|
assert rgb_img.getpixel((80, 50)) == colours[1]
|
2015-04-04 03:45:30 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Check BG is correct colour
|
|
|
|
assert rgb_img.getpixel((1, 1)) == (255, 255, 255, 0)
|
2018-04-11 01:57:31 +03:00
|
|
|
|
2015-04-04 03:45:30 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_dispose2_background(tmp_path):
|
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
|
|
|
|
im_list = []
|
|
|
|
|
|
|
|
im = Image.new("P", (100, 100))
|
|
|
|
d = ImageDraw.Draw(im)
|
|
|
|
d.rectangle([(50, 0), (100, 100)], fill="#f00")
|
|
|
|
d.rectangle([(0, 0), (50, 100)], fill="#0f0")
|
|
|
|
im_list.append(im)
|
|
|
|
|
|
|
|
im = Image.new("P", (100, 100))
|
|
|
|
d = ImageDraw.Draw(im)
|
|
|
|
d.rectangle([(0, 0), (100, 50)], fill="#f00")
|
|
|
|
d.rectangle([(0, 50), (100, 100)], fill="#0f0")
|
|
|
|
im_list.append(im)
|
|
|
|
|
|
|
|
im_list[0].save(
|
|
|
|
out, save_all=True, append_images=im_list[1:], disposal=[0, 2], background=1
|
|
|
|
)
|
|
|
|
|
|
|
|
with Image.open(out) as im:
|
|
|
|
im.seek(1)
|
|
|
|
assert im.getpixel((0, 0)) == 0
|
|
|
|
|
|
|
|
|
2021-03-16 16:24:57 +03:00
|
|
|
def test_transparency_in_second_frame():
|
|
|
|
with Image.open("Tests/images/different_transparency.gif") as im:
|
|
|
|
assert im.info["transparency"] == 0
|
|
|
|
|
|
|
|
# Seek to the second frame
|
|
|
|
im.seek(im.tell() + 1)
|
|
|
|
assert im.info["transparency"] == 0
|
|
|
|
|
|
|
|
assert_image_equal_tofile(im, "Tests/images/different_transparency_merged.gif")
|
|
|
|
|
|
|
|
|
|
|
|
def test_no_transparency_in_second_frame():
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open("Tests/images/iss634.gif") as img:
|
|
|
|
# Seek to the second frame
|
|
|
|
img.seek(img.tell() + 1)
|
2021-03-16 16:24:57 +03:00
|
|
|
assert "transparency" not in img.info
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# All transparent pixels should be replaced with the color from the first frame
|
2021-03-16 16:24:57 +03:00
|
|
|
assert img.histogram()[255] == 0
|
2020-02-23 00:03:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_duration(tmp_path):
|
|
|
|
duration = 1000
|
|
|
|
|
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im = Image.new("L", (100, 100), "#000")
|
|
|
|
|
|
|
|
# Check that the argument has priority over the info settings
|
|
|
|
im.info["duration"] = 100
|
|
|
|
im.save(out, duration=duration)
|
|
|
|
|
|
|
|
with Image.open(out) as reread:
|
|
|
|
assert reread.info["duration"] == duration
|
|
|
|
|
|
|
|
|
|
|
|
def test_multiple_duration(tmp_path):
|
|
|
|
duration_list = [1000, 2000, 3000]
|
|
|
|
|
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im_list = [
|
|
|
|
Image.new("L", (100, 100), "#000"),
|
|
|
|
Image.new("L", (100, 100), "#111"),
|
|
|
|
Image.new("L", (100, 100), "#222"),
|
|
|
|
]
|
|
|
|
|
|
|
|
# Duration as list
|
|
|
|
im_list[0].save(
|
|
|
|
out, save_all=True, append_images=im_list[1:], duration=duration_list
|
|
|
|
)
|
|
|
|
with Image.open(out) as reread:
|
|
|
|
|
|
|
|
for duration in duration_list:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert reread.info["duration"] == duration
|
2020-02-23 00:03:01 +03:00
|
|
|
try:
|
|
|
|
reread.seek(reread.tell() + 1)
|
|
|
|
except EOFError:
|
|
|
|
pass
|
2015-04-04 03:45:30 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Duration as tuple
|
|
|
|
im_list[0].save(
|
|
|
|
out, save_all=True, append_images=im_list[1:], duration=tuple(duration_list)
|
|
|
|
)
|
|
|
|
with Image.open(out) as reread:
|
2016-12-27 13:42:58 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
for duration in duration_list:
|
|
|
|
assert reread.info["duration"] == duration
|
|
|
|
try:
|
|
|
|
reread.seek(reread.tell() + 1)
|
|
|
|
except EOFError:
|
|
|
|
pass
|
2016-12-27 14:04:37 +03:00
|
|
|
|
2016-12-27 13:42:58 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_identical_frames(tmp_path):
|
|
|
|
duration_list = [1000, 1500, 2000, 4000]
|
2016-12-27 13:42:58 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im_list = [
|
|
|
|
Image.new("L", (100, 100), "#000"),
|
|
|
|
Image.new("L", (100, 100), "#000"),
|
|
|
|
Image.new("L", (100, 100), "#000"),
|
|
|
|
Image.new("L", (100, 100), "#111"),
|
|
|
|
]
|
|
|
|
|
|
|
|
# Duration as list
|
|
|
|
im_list[0].save(
|
|
|
|
out, save_all=True, append_images=im_list[1:], duration=duration_list
|
|
|
|
)
|
|
|
|
with Image.open(out) as reread:
|
|
|
|
|
|
|
|
# Assert that the first three frames were combined
|
|
|
|
assert reread.n_frames == 2
|
2016-12-27 14:04:37 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Assert that the new duration is the total of the identical frames
|
|
|
|
assert reread.info["duration"] == 4500
|
2016-12-27 14:04:37 +03:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_identical_frames_to_single_frame(tmp_path):
|
|
|
|
for duration in ([1000, 1500, 2000, 4000], (1000, 1500, 2000, 4000), 8500):
|
|
|
|
out = str(tmp_path / "temp.gif")
|
2016-12-29 03:28:58 +03:00
|
|
|
im_list = [
|
2019-06-13 18:53:42 +03:00
|
|
|
Image.new("L", (100, 100), "#000"),
|
|
|
|
Image.new("L", (100, 100), "#000"),
|
|
|
|
Image.new("L", (100, 100), "#000"),
|
2016-12-29 03:28:58 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
im_list[0].save(
|
2020-02-23 00:03:01 +03:00
|
|
|
out, save_all=True, append_images=im_list[1:], duration=duration
|
2016-12-29 03:28:58 +03:00
|
|
|
)
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open(out) as reread:
|
2020-02-23 00:03:01 +03:00
|
|
|
# Assert that all frames were combined
|
|
|
|
assert reread.n_frames == 1
|
2016-12-29 03:28:58 +03:00
|
|
|
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
# Assert that the new duration is the total of the identical frames
|
2020-02-23 00:03:01 +03:00
|
|
|
assert reread.info["duration"] == 8500
|
2015-04-04 03:45:30 +03:00
|
|
|
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_number_of_loops(tmp_path):
|
|
|
|
number_of_loops = 2
|
2015-06-11 04:10:05 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im = Image.new("L", (100, 100), "#000")
|
|
|
|
im.save(out, loop=number_of_loops)
|
|
|
|
with Image.open(out) as reread:
|
2015-06-11 04:10:05 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
assert reread.info["loop"] == number_of_loops
|
2018-10-02 13:52:07 +03:00
|
|
|
|
2016-05-07 06:57:40 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_background(tmp_path):
|
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im = Image.new("L", (100, 100), "#000")
|
|
|
|
im.info["background"] = 1
|
|
|
|
im.save(out)
|
|
|
|
with Image.open(out) as reread:
|
2016-05-07 06:57:40 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
assert reread.info["background"] == im.info["background"]
|
|
|
|
|
|
|
|
if features.check("webp") and features.check("webp_anim"):
|
|
|
|
with Image.open("Tests/images/hopper.webp") as im:
|
|
|
|
assert isinstance(im.info["background"], tuple)
|
|
|
|
im.save(out)
|
|
|
|
|
|
|
|
|
|
|
|
def test_comment(tmp_path):
|
|
|
|
with Image.open(TEST_GIF) as im:
|
|
|
|
assert im.info["comment"] == b"File written by Adobe Photoshop\xa8 4.0"
|
|
|
|
|
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im = Image.new("L", (100, 100), "#000")
|
|
|
|
im.info["comment"] = b"Test comment text"
|
|
|
|
im.save(out)
|
|
|
|
with Image.open(out) as reread:
|
|
|
|
assert reread.info["comment"] == im.info["comment"]
|
|
|
|
|
|
|
|
im.info["comment"] = "Test comment text"
|
|
|
|
im.save(out)
|
|
|
|
with Image.open(out) as reread:
|
|
|
|
assert reread.info["comment"] == im.info["comment"].encode()
|
|
|
|
|
|
|
|
|
|
|
|
def test_comment_over_255(tmp_path):
|
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im = Image.new("L", (100, 100), "#000")
|
|
|
|
comment = b"Test comment text"
|
|
|
|
while len(comment) < 256:
|
|
|
|
comment += comment
|
|
|
|
im.info["comment"] = comment
|
|
|
|
im.save(out)
|
|
|
|
with Image.open(out) as reread:
|
|
|
|
|
|
|
|
assert reread.info["comment"] == comment
|
|
|
|
|
|
|
|
|
|
|
|
def test_zero_comment_subblocks():
|
|
|
|
with Image.open("Tests/images/hopper_zero_comment_subblocks.gif") as im:
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, TEST_GIF)
|
2020-02-23 00:03:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_version(tmp_path):
|
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
|
|
|
|
def assertVersionAfterSave(im, version):
|
2018-11-27 13:05:41 +03:00
|
|
|
im.save(out)
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open(out) as reread:
|
2020-02-23 00:03:01 +03:00
|
|
|
assert reread.info["version"] == version
|
2018-11-27 13:05:41 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Test that GIF87a is used by default
|
|
|
|
im = Image.new("L", (100, 100), "#000")
|
|
|
|
assertVersionAfterSave(im, b"GIF87a")
|
2018-11-27 13:05:41 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Test setting the version to 89a
|
|
|
|
im = Image.new("L", (100, 100), "#000")
|
|
|
|
im.info["version"] = b"89a"
|
|
|
|
assertVersionAfterSave(im, b"GIF89a")
|
2018-11-27 13:09:28 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Test that adding a GIF89a feature changes the version
|
|
|
|
im.info["transparency"] = 1
|
|
|
|
assertVersionAfterSave(im, b"GIF89a")
|
2015-08-21 15:10:13 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Test that a GIF87a image is also saved in that format
|
|
|
|
with Image.open("Tests/images/test.colors.gif") as im:
|
|
|
|
assertVersionAfterSave(im, b"GIF87a")
|
2017-09-01 13:36:51 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Test that a GIF89a image is also saved in that format
|
|
|
|
im.info["version"] = b"GIF89a"
|
2017-09-01 13:36:51 +03:00
|
|
|
assertVersionAfterSave(im, b"GIF87a")
|
|
|
|
|
2015-08-21 15:10:13 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_append_images(tmp_path):
|
|
|
|
out = str(tmp_path / "temp.gif")
|
2015-08-21 15:10:13 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Test appending single frame images
|
|
|
|
im = Image.new("RGB", (100, 100), "#f00")
|
|
|
|
ims = [Image.new("RGB", (100, 100), color) for color in ["#0f0", "#00f"]]
|
|
|
|
im.copy().save(out, save_all=True, append_images=ims)
|
2015-08-21 15:10:13 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(out) as reread:
|
|
|
|
assert reread.n_frames == 3
|
2015-08-21 15:10:13 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Tests appending using a generator
|
|
|
|
def imGenerator(ims):
|
|
|
|
yield from ims
|
2016-09-11 05:04:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im.save(out, save_all=True, append_images=imGenerator(ims))
|
2017-11-04 02:46:15 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(out) as reread:
|
|
|
|
assert reread.n_frames == 3
|
2017-11-04 02:46:15 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Tests appending single and multiple frame images
|
|
|
|
with Image.open("Tests/images/dispose_none.gif") as im:
|
|
|
|
with Image.open("Tests/images/dispose_prev.gif") as im2:
|
|
|
|
im.save(out, save_all=True, append_images=[im2])
|
2019-06-13 18:53:42 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(out) as reread:
|
|
|
|
assert reread.n_frames == 10
|
2016-09-11 05:04:01 +03:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_transparent_optimize(tmp_path):
|
|
|
|
# From issue #2195, if the transparent color is incorrectly optimized out, GIF loses
|
|
|
|
# transparency.
|
|
|
|
# Need a palette that isn't using the 0 color, and one that's > 128 items where the
|
|
|
|
# transparent color is actually the top palette entry to trigger the bug.
|
2016-09-11 05:04:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
data = bytes(range(1, 254))
|
|
|
|
palette = ImagePalette.ImagePalette("RGB", list(range(256)) * 3)
|
2015-08-21 15:10:13 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im = Image.new("L", (253, 1))
|
|
|
|
im.frombytes(data)
|
|
|
|
im.putpalette(palette)
|
2016-12-27 14:30:47 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im.save(out, transparency=253)
|
|
|
|
with Image.open(out) as reloaded:
|
2016-12-27 14:30:47 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
assert reloaded.info["transparency"] == 253
|
2016-12-27 14:30:47 +03:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_rgb_transparency(tmp_path):
|
|
|
|
out = str(tmp_path / "temp.gif")
|
2017-03-03 13:31:58 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Single frame
|
|
|
|
im = Image.new("RGB", (1, 1))
|
|
|
|
im.info["transparency"] = (255, 0, 0)
|
2021-06-27 08:09:39 +03:00
|
|
|
im.save(out)
|
2018-06-16 12:47:57 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(out) as reloaded:
|
2021-06-27 08:09:39 +03:00
|
|
|
assert "transparency" in reloaded.info
|
2018-06-16 12:47:57 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
# Multiple frames
|
|
|
|
im = Image.new("RGB", (1, 1))
|
|
|
|
im.info["transparency"] = b""
|
|
|
|
ims = [Image.new("RGB", (1, 1))]
|
|
|
|
pytest.warns(UserWarning, im.save, out, save_all=True, append_images=ims)
|
2018-06-16 12:47:57 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
assert "transparency" not in reloaded.info
|
2018-06-16 12:47:57 +03:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_bbox(tmp_path):
|
|
|
|
out = str(tmp_path / "temp.gif")
|
2017-01-26 11:39:59 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im = Image.new("RGB", (100, 100), "#fff")
|
|
|
|
ims = [Image.new("RGB", (100, 100), "#000")]
|
|
|
|
im.save(out, save_all=True, append_images=ims)
|
|
|
|
|
|
|
|
with Image.open(out) as reread:
|
|
|
|
assert reread.n_frames == 2
|
2017-01-26 11:39:59 +03:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_palette_save_L(tmp_path):
|
|
|
|
# Generate an L mode image with a separate palette
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im = hopper("P")
|
|
|
|
im_l = Image.frombytes("L", im.size, im.tobytes())
|
|
|
|
palette = bytes(im.getpalette())
|
2017-03-03 19:25:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im_l.save(out, palette=palette)
|
2017-03-03 19:25:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
assert_image_equal(reloaded.convert("RGB"), im.convert("RGB"))
|
2017-03-03 19:25:01 +03:00
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_palette_save_P(tmp_path):
|
|
|
|
# Pass in a different palette, then construct what the image would look like.
|
|
|
|
# Forcing a non-straight grayscale palette.
|
2017-03-07 01:46:55 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im = hopper("P")
|
|
|
|
palette = bytes([255 - i // 3 for i in range(768)])
|
2017-03-07 01:46:55 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im.save(out, palette=palette)
|
2017-03-07 01:46:55 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
im.putpalette(palette)
|
|
|
|
assert_image_equal(reloaded, im)
|
2017-03-07 01:46:55 +03:00
|
|
|
|
2017-03-03 19:25:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_palette_save_ImagePalette(tmp_path):
|
|
|
|
# Pass in a different palette, as an ImagePalette.ImagePalette
|
|
|
|
# effectively the same as test_palette_save_P
|
2017-03-03 19:25:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im = hopper("P")
|
|
|
|
palette = ImagePalette.ImagePalette("RGB", list(range(256))[::-1] * 3)
|
2017-03-03 19:25:01 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im.save(out, palette=palette)
|
2016-12-27 14:30:47 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
im.putpalette(palette)
|
|
|
|
assert_image_equal(reloaded, im)
|
2017-03-03 19:38:30 +03:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_save_I(tmp_path):
|
|
|
|
# Test saving something that would trigger the auto-convert to 'L'
|
2017-03-03 19:38:30 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
im = hopper("I")
|
2017-03-03 19:38:30 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
out = str(tmp_path / "temp.gif")
|
|
|
|
im.save(out)
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
assert_image_equal(reloaded.convert("L"), im.convert("L"))
|
|
|
|
|
|
|
|
|
|
|
|
def test_getdata():
|
|
|
|
# Test getheader/getdata against legacy values.
|
|
|
|
# Create a 'P' image with holes in the palette.
|
|
|
|
im = Image._wedge().resize((16, 16), Image.NEAREST)
|
|
|
|
im.putpalette(ImagePalette.ImagePalette("RGB"))
|
|
|
|
im.info = {"background": 0}
|
|
|
|
|
|
|
|
passed_palette = bytes([255 - i // 3 for i in range(768)])
|
|
|
|
|
|
|
|
GifImagePlugin._FORCE_OPTIMIZE = True
|
|
|
|
try:
|
|
|
|
h = GifImagePlugin.getheader(im, passed_palette)
|
|
|
|
d = GifImagePlugin.getdata(im)
|
|
|
|
|
|
|
|
import pickle
|
|
|
|
|
|
|
|
# Enable to get target values on pre-refactor version
|
|
|
|
# with open('Tests/images/gif_header_data.pkl', 'wb') as f:
|
|
|
|
# pickle.dump((h, d), f, 1)
|
|
|
|
with open("Tests/images/gif_header_data.pkl", "rb") as f:
|
|
|
|
(h_target, d_target) = pickle.load(f)
|
|
|
|
|
|
|
|
assert h == h_target
|
|
|
|
assert d == d_target
|
|
|
|
finally:
|
|
|
|
GifImagePlugin._FORCE_OPTIMIZE = False
|
|
|
|
|
|
|
|
|
|
|
|
def test_lzw_bits():
|
|
|
|
# see https://github.com/python-pillow/Pillow/issues/2811
|
|
|
|
with Image.open("Tests/images/issue_2811.gif") as im:
|
|
|
|
assert im.tile[0][3][0] == 11 # LZW bits
|
|
|
|
# codec error prepatch
|
|
|
|
im.load()
|
2017-03-07 12:52:31 +03:00
|
|
|
|
2019-05-02 12:46:17 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_extents():
|
|
|
|
with Image.open("Tests/images/test_extents.gif") as im:
|
|
|
|
assert im.size == (100, 100)
|
|
|
|
im.seek(1)
|
|
|
|
assert im.size == (150, 150)
|
2021-04-08 01:04:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_missing_background():
|
|
|
|
# The Global Color Table Flag isn't set, so there is no background color index,
|
|
|
|
# but the disposal method is "Restore to background color"
|
|
|
|
with Image.open("Tests/images/missing_background.gif") as im:
|
|
|
|
im.seek(1)
|
|
|
|
assert_image_equal_tofile(im, "Tests/images/missing_background_first_frame.gif")
|