2018-06-15 23:01:06 +03:00
|
|
|
import os
|
|
|
|
from glob import glob
|
|
|
|
from itertools import product
|
|
|
|
|
2020-02-03 12:11:32 +03:00
|
|
|
import pytest
|
2014-08-20 10:32:06 +04:00
|
|
|
from PIL import Image
|
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
from .helper import PillowTestCase, assert_image_equal, hopper
|
2014-08-20 10:32:06 +04:00
|
|
|
|
2018-06-15 23:01:06 +03:00
|
|
|
_TGA_DIR = os.path.join("Tests", "images", "tga")
|
|
|
|
_TGA_DIR_COMMON = os.path.join(_TGA_DIR, "common")
|
|
|
|
|
|
|
|
|
2014-08-20 10:39:11 +04:00
|
|
|
class TestFileTga(PillowTestCase):
|
2014-08-20 10:32:06 +04:00
|
|
|
|
2018-07-02 00:50:02 +03:00
|
|
|
_MODES = ("L", "LA", "P", "RGB", "RGBA")
|
2018-06-15 23:01:06 +03:00
|
|
|
_ORIGINS = ("tl", "bl")
|
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
_ORIGIN_TO_ORIENTATION = {"tl": 1, "bl": -1}
|
2018-06-15 23:01:06 +03:00
|
|
|
|
|
|
|
def test_sanity(self):
|
|
|
|
for mode in self._MODES:
|
|
|
|
png_paths = glob(
|
2019-06-13 18:54:11 +03:00
|
|
|
os.path.join(_TGA_DIR_COMMON, "*x*_{}.png".format(mode.lower()))
|
|
|
|
)
|
2018-06-15 23:01:06 +03:00
|
|
|
|
|
|
|
for png_path in png_paths:
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(png_path) as reference_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert reference_im.mode == mode
|
2018-06-15 23:01:06 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
path_no_ext = os.path.splitext(png_path)[0]
|
|
|
|
for origin, rle in product(self._ORIGINS, (True, False)):
|
|
|
|
tga_path = "{}_{}_{}.tga".format(
|
|
|
|
path_no_ext, origin, "rle" if rle else "raw"
|
2019-06-13 18:54:11 +03:00
|
|
|
)
|
2018-06-15 23:01:06 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(tga_path) as original_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert original_im.format == "TGA"
|
|
|
|
assert original_im.get_format_mimetype() == "image/x-tga"
|
2019-11-25 23:03:23 +03:00
|
|
|
if rle:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert original_im.info["compression"] == "tga_rle"
|
|
|
|
assert (
|
|
|
|
original_im.info["orientation"]
|
|
|
|
== self._ORIGIN_TO_ORIENTATION[origin]
|
2019-11-25 23:03:23 +03:00
|
|
|
)
|
|
|
|
if mode == "P":
|
2020-02-22 16:06:21 +03:00
|
|
|
assert (
|
|
|
|
original_im.getpalette()
|
|
|
|
== reference_im.getpalette()
|
2019-11-25 23:03:23 +03:00
|
|
|
)
|
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(original_im, reference_im)
|
2019-11-25 23:03:23 +03:00
|
|
|
|
|
|
|
# Generate a new test name every time so the
|
|
|
|
# test will not fail with permission error
|
|
|
|
# on Windows.
|
|
|
|
out = self.tempfile("temp.tga")
|
|
|
|
|
|
|
|
original_im.save(out, rle=rle)
|
|
|
|
with Image.open(out) as saved_im:
|
|
|
|
if rle:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert (
|
|
|
|
saved_im.info["compression"]
|
|
|
|
== original_im.info["compression"]
|
2019-11-25 23:03:23 +03:00
|
|
|
)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert (
|
|
|
|
saved_im.info["orientation"]
|
|
|
|
== original_im.info["orientation"]
|
2019-11-25 23:03:23 +03:00
|
|
|
)
|
|
|
|
if mode == "P":
|
2020-02-22 16:06:21 +03:00
|
|
|
assert (
|
|
|
|
saved_im.getpalette()
|
|
|
|
== original_im.getpalette()
|
2019-11-25 23:03:23 +03:00
|
|
|
)
|
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(saved_im, original_im)
|
2018-06-15 23:01:06 +03:00
|
|
|
|
2014-08-20 10:39:11 +04:00
|
|
|
def test_id_field(self):
|
2014-08-20 10:32:06 +04:00
|
|
|
# tga file with id field
|
|
|
|
test_file = "Tests/images/tga_id_field.tga"
|
|
|
|
|
|
|
|
# Act
|
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_file) as im:
|
2014-08-20 10:32:06 +04: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
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.size == (100, 100)
|
2014-08-20 10:32:06 +04:00
|
|
|
|
2016-04-21 17:40:20 +03:00
|
|
|
def test_id_field_rle(self):
|
|
|
|
# tga file with id field
|
|
|
|
test_file = "Tests/images/rgb32rle.tga"
|
|
|
|
|
|
|
|
# Act
|
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_file) as im:
|
2016-04-21 17:40:20 +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
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.size == (199, 199)
|
2016-04-21 17:40:20 +03:00
|
|
|
|
2015-07-03 08:03:25 +03:00
|
|
|
def test_save(self):
|
|
|
|
test_file = "Tests/images/tga_id_field.tga"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
out = self.tempfile("temp.tga")
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
# Save
|
|
|
|
im.save(out)
|
|
|
|
with Image.open(out) as test_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert test_im.size == (100, 100)
|
|
|
|
assert test_im.info["id_section"] == im.info["id_section"]
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
# RGBA save
|
|
|
|
im.convert("RGBA").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 test_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert test_im.size == (100, 100)
|
2015-07-03 09:22:56 +03:00
|
|
|
|
2020-01-14 12:51:56 +03:00
|
|
|
def test_save_wrong_mode(self):
|
|
|
|
im = hopper("PA")
|
|
|
|
out = self.tempfile("temp.tga")
|
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(OSError):
|
2020-01-14 12:51:56 +03:00
|
|
|
im.save(out)
|
|
|
|
|
2018-09-01 16:31:39 +03:00
|
|
|
def test_save_id_section(self):
|
|
|
|
test_file = "Tests/images/rgb32rle.tga"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
out = self.tempfile("temp.tga")
|
2018-09-01 16:31:39 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
# Check there is no id section
|
|
|
|
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 test_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert "id_section" not in test_im.info
|
2018-09-01 16:31:39 +03:00
|
|
|
|
|
|
|
# Save with custom id section
|
|
|
|
im.save(out, id_section=b"Test content")
|
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 test_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert test_im.info["id_section"] == b"Test content"
|
2018-09-01 16:31:39 +03:00
|
|
|
|
2018-09-08 01:26:32 +03:00
|
|
|
# Save with custom id section greater than 255 characters
|
|
|
|
id_section = b"Test content" * 25
|
2020-02-03 12:11:32 +03:00
|
|
|
pytest.warns(UserWarning, lambda: im.save(out, id_section=id_section))
|
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 test_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert test_im.info["id_section"] == id_section[:255]
|
2018-09-08 01:26:32 +03:00
|
|
|
|
2018-09-01 16:31:39 +03:00
|
|
|
test_file = "Tests/images/tga_id_field.tga"
|
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_file) as im:
|
2018-09-01 16:31:39 +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
|
|
|
# Save with no id section
|
|
|
|
im.save(out, id_section="")
|
|
|
|
with Image.open(out) as test_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert "id_section" not in test_im.info
|
2018-09-01 16:31:39 +03:00
|
|
|
|
|
|
|
def test_save_orientation(self):
|
|
|
|
test_file = "Tests/images/rgb32rle.tga"
|
|
|
|
out = self.tempfile("temp.tga")
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["orientation"] == -1
|
2018-09-01 16:31:39 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
im.save(out, orientation=1)
|
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 test_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert test_im.info["orientation"] == 1
|
2018-09-01 16:31:39 +03:00
|
|
|
|
2016-04-21 17:40:20 +03:00
|
|
|
def test_save_rle(self):
|
|
|
|
test_file = "Tests/images/rgb32rle.tga"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["compression"] == "tga_rle"
|
2016-04-21 17:40:20 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
out = self.tempfile("temp.tga")
|
2016-04-21 17:40:20 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
# Save
|
|
|
|
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 test_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert test_im.size == (199, 199)
|
|
|
|
assert test_im.info["compression"] == "tga_rle"
|
2018-09-01 16:31:39 +03:00
|
|
|
|
|
|
|
# Save without compression
|
|
|
|
im.save(out, compression=None)
|
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 test_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert "compression" not in test_im.info
|
2016-04-21 17:40:20 +03:00
|
|
|
|
|
|
|
# RGBA save
|
2018-09-01 16:31:39 +03:00
|
|
|
im.convert("RGBA").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 test_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert test_im.size == (199, 199)
|
2016-04-21 17:40:20 +03:00
|
|
|
|
2018-09-01 16:31:39 +03:00
|
|
|
test_file = "Tests/images/tga_id_field.tga"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert "compression" not in im.info
|
2018-09-01 16:31:39 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
# Save with compression
|
|
|
|
im.save(out, compression="tga_rle")
|
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 test_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert test_im.info["compression"] == "tga_rle"
|
2018-09-01 16:31:39 +03:00
|
|
|
|
2018-06-14 12:18:08 +03:00
|
|
|
def test_save_l_transparency(self):
|
|
|
|
# There are 559 transparent pixels in la.tga.
|
|
|
|
num_transparent = 559
|
|
|
|
|
|
|
|
in_file = "Tests/images/la.tga"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(in_file) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.mode == "LA"
|
|
|
|
assert im.getchannel("A").getcolors()[0][0] == num_transparent
|
2018-06-14 12:18:08 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
out = self.tempfile("temp.tga")
|
|
|
|
im.save(out)
|
2018-06-14 12:18:08 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(out) as test_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert test_im.mode == "LA"
|
|
|
|
assert test_im.getchannel("A").getcolors()[0][0] == num_transparent
|
2018-06-14 12:18:08 +03:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(im, test_im)
|