2014-09-05 13:36:24 +04:00
|
|
|
from helper import unittest, PillowTestCase, hopper
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2015-07-03 08:03:25 +03:00
|
|
|
from PIL import Image, BmpImagePlugin
|
2014-01-23 08:40:37 +04:00
|
|
|
import io
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestFileBmp(PillowTestCase):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def roundtrip(self, im):
|
|
|
|
outfile = self.tempfile("temp.bmp")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04: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")
|
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:
|
2015-07-03 09:22:56 +03:00
|
|
|
self.assertRaises(SyntaxError,
|
|
|
|
lambda: 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()
|
2014-09-05 13:36:24 +04:00
|
|
|
im = hopper()
|
2014-06-29 01:22:52 +04:00
|
|
|
im.save(output, "BMP", dpi=dpi)
|
|
|
|
|
|
|
|
output.seek(0)
|
|
|
|
reloaded = Image.open(output)
|
|
|
|
|
|
|
|
self.assertEqual(reloaded.info["dpi"], dpi)
|
|
|
|
|
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
|
|
|
|
im.save(outfile, 'JPEG', dpi=im.info['dpi'])
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
reloaded = Image.open(outfile)
|
|
|
|
reloaded.load()
|
|
|
|
self.assertEqual(im.info['dpi'], reloaded.info['dpi'])
|
|
|
|
self.assertEqual(im.size, reloaded.size)
|
|
|
|
self.assertEqual(reloaded.format, "JPEG")
|
|
|
|
|
2016-04-06 16:22:12 +03:00
|
|
|
def test_load_dib(self):
|
|
|
|
# test for #1293, Imagegrab returning Unsupported Bitfields Format
|
|
|
|
im = BmpImagePlugin.DibImageFile('Tests/images/clipboard.dib')
|
|
|
|
target = Image.open('Tests/images/clipboard_target.png')
|
|
|
|
self.assert_image_equal(im.convert('RGB'), target.convert('RGB'))
|
|
|
|
self.assert_image_similar(im, target, 1)
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
# End of file
|