2014-01-23 08:40:37 +04:00
|
|
|
import io
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from PIL import BmpImagePlugin, Image
|
|
|
|
|
|
|
|
from .helper import PillowTestCase, hopper
|
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestFileBmp(PillowTestCase):
|
|
|
|
def roundtrip(self, im):
|
|
|
|
outfile = self.tempfile("temp.bmp")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-06-13 18:53:42 +03:00
|
|
|
im.save(outfile, "BMP")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
reloaded = Image.open(outfile)
|
|
|
|
reloaded.load()
|
|
|
|
self.assertEqual(im.mode, reloaded.mode)
|
|
|
|
self.assertEqual(im.size, reloaded.size)
|
|
|
|
self.assertEqual(reloaded.format, "BMP")
|
2019-03-06 05:59:07 +03:00
|
|
|
self.assertEqual(reloaded.get_format_mimetype(), "image/bmp")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_sanity(self):
|
2014-09-05 13:36:24 +04:00
|
|
|
self.roundtrip(hopper())
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-09-05 13:36:24 +04:00
|
|
|
self.roundtrip(hopper("1"))
|
|
|
|
self.roundtrip(hopper("L"))
|
|
|
|
self.roundtrip(hopper("P"))
|
|
|
|
self.roundtrip(hopper("RGB"))
|
2014-01-23 08:40:37 +04:00
|
|
|
|
2015-07-03 08:03:25 +03:00
|
|
|
def test_invalid_file(self):
|
|
|
|
with open("Tests/images/flower.jpg", "rb") as fp:
|
2019-06-13 18:53:42 +03:00
|
|
|
self.assertRaises(SyntaxError, BmpImagePlugin.BmpImageFile, fp)
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_save_to_bytes(self):
|
|
|
|
output = io.BytesIO()
|
2014-09-05 13:36:24 +04:00
|
|
|
im = hopper()
|
2014-06-10 13:10:47 +04:00
|
|
|
im.save(output, "BMP")
|
|
|
|
|
|
|
|
output.seek(0)
|
|
|
|
reloaded = Image.open(output)
|
|
|
|
|
|
|
|
self.assertEqual(im.mode, reloaded.mode)
|
|
|
|
self.assertEqual(im.size, reloaded.size)
|
|
|
|
self.assertEqual(reloaded.format, "BMP")
|
|
|
|
|
2014-06-29 01:22:52 +04:00
|
|
|
def test_dpi(self):
|
|
|
|
dpi = (72, 72)
|
|
|
|
|
|
|
|
output = io.BytesIO()
|
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 hopper() as im:
|
|
|
|
im.save(output, "BMP", dpi=dpi)
|
2014-06-29 01:22:52 +04:00
|
|
|
|
|
|
|
output.seek(0)
|
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(output) as reloaded:
|
|
|
|
self.assertEqual(reloaded.info["dpi"], dpi)
|
2014-06-29 01:22:52 +04:00
|
|
|
|
2015-06-30 07:09:35 +03:00
|
|
|
def test_save_bmp_with_dpi(self):
|
|
|
|
# Test for #1301
|
|
|
|
# Arrange
|
|
|
|
outfile = self.tempfile("temp.jpg")
|
|
|
|
im = Image.open("Tests/images/hopper.bmp")
|
|
|
|
|
|
|
|
# Act
|
2019-06-13 18:53:42 +03:00
|
|
|
im.save(outfile, "JPEG", dpi=im.info["dpi"])
|
2015-06-30 07:09:35 +03:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
reloaded = Image.open(outfile)
|
|
|
|
reloaded.load()
|
2019-06-13 18:53:42 +03:00
|
|
|
self.assertEqual(im.info["dpi"], reloaded.info["dpi"])
|
2015-06-30 07:09:35 +03:00
|
|
|
self.assertEqual(im.size, reloaded.size)
|
|
|
|
self.assertEqual(reloaded.format, "JPEG")
|
|
|
|
|
2019-03-30 07:03:57 +03:00
|
|
|
def test_load_dpi_rounding(self):
|
|
|
|
# Round up
|
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("Tests/images/hopper.bmp") as im:
|
|
|
|
self.assertEqual(im.info["dpi"], (96, 96))
|
2019-03-30 07:03:57 +03:00
|
|
|
|
|
|
|
# Round down
|
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("Tests/images/hopper_roundDown.bmp") as im:
|
|
|
|
self.assertEqual(im.info["dpi"], (72, 72))
|
2019-03-30 07:03:57 +03:00
|
|
|
|
|
|
|
def test_save_dpi_rounding(self):
|
|
|
|
outfile = self.tempfile("temp.bmp")
|
2019-06-13 18:53:42 +03:00
|
|
|
im = Image.open("Tests/images/hopper.bmp")
|
2019-03-30 07:03:57 +03:00
|
|
|
|
|
|
|
im.save(outfile, dpi=(72.2, 72.2))
|
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(outfile) as reloaded:
|
|
|
|
self.assertEqual(reloaded.info["dpi"], (72, 72))
|
2019-03-30 07:03:57 +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
|
|
|
im.save(outfile, dpi=(72.8, 72.8))
|
|
|
|
with Image.open(outfile) as reloaded:
|
|
|
|
self.assertEqual(reloaded.info["dpi"], (73, 73))
|
2019-03-30 07:03:57 +03:00
|
|
|
|
2016-04-06 16:22:12 +03:00
|
|
|
def test_load_dib(self):
|
|
|
|
# test for #1293, Imagegrab returning Unsupported Bitfields Format
|
2019-06-13 18:53:42 +03:00
|
|
|
im = Image.open("Tests/images/clipboard.dib")
|
2019-03-06 05:59:07 +03:00
|
|
|
self.assertEqual(im.format, "DIB")
|
|
|
|
self.assertEqual(im.get_format_mimetype(), "image/bmp")
|
|
|
|
|
2019-06-13 18:53:42 +03:00
|
|
|
target = Image.open("Tests/images/clipboard_target.png")
|
2016-04-29 23:17:18 +03:00
|
|
|
self.assert_image_equal(im, target)
|
2019-03-06 05:59:07 +03:00
|
|
|
|
|
|
|
def test_save_dib(self):
|
|
|
|
outfile = self.tempfile("temp.dib")
|
|
|
|
|
2019-06-13 18:53:42 +03:00
|
|
|
im = Image.open("Tests/images/clipboard.dib")
|
2019-03-06 05:59:07 +03:00
|
|
|
im.save(outfile)
|
|
|
|
|
|
|
|
reloaded = Image.open(outfile)
|
|
|
|
self.assertEqual(reloaded.format, "DIB")
|
|
|
|
self.assertEqual(reloaded.get_format_mimetype(), "image/bmp")
|
|
|
|
self.assert_image_equal(im, reloaded)
|
2019-03-08 11:26:01 +03:00
|
|
|
|
|
|
|
def test_rgba_bitfields(self):
|
|
|
|
# This test image has been manually hexedited
|
|
|
|
# to change the bitfield compression in the header from XBGR to RGBA
|
|
|
|
im = Image.open("Tests/images/rgb32bf-rgba.bmp")
|
|
|
|
|
|
|
|
# So before the comparing the image, swap the channels
|
|
|
|
b, g, r = im.split()[1:]
|
|
|
|
im = Image.merge("RGB", (r, g, b))
|
|
|
|
|
|
|
|
target = Image.open("Tests/images/bmp/q/rgb32bf-xbgr.bmp")
|
|
|
|
self.assert_image_equal(im, target)
|