Pillow/Tests/test_file_eps.py

255 lines
9.1 KiB
Python
Raw Normal View History

2014-01-12 00:30:09 +04:00
import io
from PIL import EpsImagePlugin, Image
from .helper import PillowTestCase, hopper, unittest
HAS_GHOSTSCRIPT = EpsImagePlugin.has_ghostscript()
# Our two EPS test files (they are identical except for their bounding boxes)
file1 = "Tests/images/zero_bb.eps"
file2 = "Tests/images/non_zero_bb.eps"
# Due to palletization, we'll need to convert these to RGB after load
file1_compare = "Tests/images/zero_bb.png"
file1_compare_scale2 = "Tests/images/zero_bb_scale2.png"
file2_compare = "Tests/images/non_zero_bb.png"
file2_compare_scale2 = "Tests/images/non_zero_bb_scale2.png"
2014-05-15 01:14:55 +04:00
# EPS test files with binary preview
file3 = "Tests/images/binary_preview_map.eps"
2014-06-10 13:10:47 +04:00
class TestFileEps(PillowTestCase):
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
2014-06-10 13:10:47 +04:00
def test_sanity(self):
# Regular scale
image1 = Image.open(file1)
image1.load()
self.assertEqual(image1.mode, "RGB")
self.assertEqual(image1.size, (460, 352))
self.assertEqual(image1.format, "EPS")
image2 = Image.open(file2)
image2.load()
self.assertEqual(image2.mode, "RGB")
self.assertEqual(image2.size, (360, 252))
self.assertEqual(image2.format, "EPS")
# Double scale
image1_scale2 = Image.open(file1)
image1_scale2.load(scale=2)
self.assertEqual(image1_scale2.mode, "RGB")
self.assertEqual(image1_scale2.size, (920, 704))
self.assertEqual(image1_scale2.format, "EPS")
image2_scale2 = Image.open(file2)
image2_scale2.load(scale=2)
self.assertEqual(image2_scale2.mode, "RGB")
self.assertEqual(image2_scale2.size, (720, 504))
self.assertEqual(image2_scale2.format, "EPS")
2015-07-03 08:03:25 +03:00
def test_invalid_file(self):
2015-07-03 09:22:56 +03:00
invalid_file = "Tests/images/flower.jpg"
2019-06-13 18:53:42 +03:00
self.assertRaises(SyntaxError, EpsImagePlugin.EpsImageFile, invalid_file)
2015-07-03 08:03:25 +03:00
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
2016-04-13 11:27:46 +03:00
def test_cmyk(self):
cmyk_image = Image.open("Tests/images/pil_sample_cmyk.eps")
2016-04-19 18:20:15 +03:00
2016-04-13 11:27:46 +03:00
self.assertEqual(cmyk_image.mode, "CMYK")
self.assertEqual(cmyk_image.size, (100, 100))
self.assertEqual(cmyk_image.format, "EPS")
2016-04-19 18:20:15 +03:00
cmyk_image.load()
self.assertEqual(cmyk_image.mode, "RGB")
2019-06-13 18:53:42 +03:00
if "jpeg_decoder" in dir(Image.core):
target = Image.open("Tests/images/pil_sample_rgb.jpg")
2016-04-19 18:20:15 +03:00
self.assert_image_similar(cmyk_image, target, 10)
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
def test_showpage(self):
2017-09-09 12:52:06 +03:00
# See https://github.com/python-pillow/Pillow/issues/2615
plot_image = Image.open("Tests/images/reqd_showpage.eps")
target = Image.open("Tests/images/reqd_showpage.png")
2018-04-10 16:00:33 +03:00
# should not crash/hang
plot_image.load()
2017-09-09 12:52:06 +03:00
# fonts could be slightly different
self.assert_image_similar(plot_image, target, 6)
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
2014-06-10 13:10:47 +04:00
def test_file_object(self):
# issue 479
image1 = Image.open(file1)
2019-06-13 18:53:42 +03:00
with open(self.tempfile("temp_file.eps"), "wb") as fh:
image1.save(fh, "EPS")
2014-06-10 13:10:47 +04:00
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
2014-06-10 13:10:47 +04:00
def test_iobase_object(self):
# issue 479
image1 = Image.open(file1)
2019-06-13 18:53:42 +03:00
with io.open(self.tempfile("temp_iobase.eps"), "wb") as fh:
image1.save(fh, "EPS")
2014-06-10 13:10:47 +04:00
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
2014-08-05 02:27:08 +04:00
def test_bytesio_object(self):
2019-06-13 18:53:42 +03:00
with open(file1, "rb") as f:
2014-08-05 02:27:08 +04:00
img_bytes = io.BytesIO(f.read())
img = Image.open(img_bytes)
img.load()
image1_scale1_compare = Image.open(file1_compare).convert("RGB")
image1_scale1_compare.load()
self.assert_image_similar(img, image1_scale1_compare, 5)
2017-03-01 12:20:18 +03:00
def test_image_mode_not_supported(self):
im = hopper("RGBA")
2019-06-13 18:53:42 +03:00
tmpfile = self.tempfile("temp.eps")
2017-09-01 14:05:40 +03:00
self.assertRaises(ValueError, im.save, tmpfile)
2017-03-01 12:20:18 +03:00
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
2014-06-10 13:10:47 +04:00
def test_render_scale1(self):
# We need png support for these render test
codecs = dir(Image.core)
if "zip_encoder" not in codecs or "zip_decoder" not in codecs:
self.skipTest("zip/deflate support not available")
# Zero bounding box
image1_scale1 = Image.open(file1)
image1_scale1.load()
image1_scale1_compare = Image.open(file1_compare).convert("RGB")
image1_scale1_compare.load()
self.assert_image_similar(image1_scale1, image1_scale1_compare, 5)
# Non-Zero bounding box
image2_scale1 = Image.open(file2)
image2_scale1.load()
image2_scale1_compare = Image.open(file2_compare).convert("RGB")
image2_scale1_compare.load()
self.assert_image_similar(image2_scale1, image2_scale1_compare, 10)
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
2014-06-10 13:10:47 +04:00
def test_render_scale2(self):
# We need png support for these render test
codecs = dir(Image.core)
if "zip_encoder" not in codecs or "zip_decoder" not in codecs:
self.skipTest("zip/deflate support not available")
# Zero bounding box
image1_scale2 = Image.open(file1)
image1_scale2.load(scale=2)
image1_scale2_compare = Image.open(file1_compare_scale2).convert("RGB")
image1_scale2_compare.load()
self.assert_image_similar(image1_scale2, image1_scale2_compare, 5)
# Non-Zero bounding box
image2_scale2 = Image.open(file2)
image2_scale2.load(scale=2)
image2_scale2_compare = Image.open(file2_compare_scale2).convert("RGB")
image2_scale2_compare.load()
self.assert_image_similar(image2_scale2, image2_scale2_compare, 10)
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
2014-06-10 13:10:47 +04:00
def test_resize(self):
# Arrange
image1 = Image.open(file1)
image2 = Image.open(file2)
2015-10-12 15:12:38 +03:00
image3 = Image.open("Tests/images/illu10_preview.eps")
2014-06-10 13:10:47 +04:00
new_size = (100, 100)
# Act
image1 = image1.resize(new_size)
image2 = image2.resize(new_size)
2015-10-12 15:12:38 +03:00
image3 = image3.resize(new_size)
2014-06-10 13:10:47 +04:00
# Assert
self.assertEqual(image1.size, new_size)
self.assertEqual(image2.size, new_size)
2015-10-12 15:12:38 +03:00
self.assertEqual(image3.size, new_size)
2014-06-10 13:10:47 +04:00
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
2014-06-10 13:10:47 +04:00
def test_thumbnail(self):
# Issue #619
# Arrange
image1 = Image.open(file1)
image2 = Image.open(file2)
new_size = (100, 100)
# Act
image1.thumbnail(new_size)
image2.thumbnail(new_size)
# Assert
self.assertEqual(max(image1.size), max(new_size))
self.assertEqual(max(image2.size), max(new_size))
def test_read_binary_preview(self):
# Issue 302
# open image with binary preview
Image.open(file3)
2015-04-24 02:26:52 +03:00
def _test_readline(self, t, ending):
2019-06-13 18:53:42 +03:00
ending = "Failure with line ending: %s" % (
"".join("%s" % ord(s) for s in ending)
)
self.assertEqual(t.readline().strip("\r\n"), "something", ending)
self.assertEqual(t.readline().strip("\r\n"), "else", ending)
self.assertEqual(t.readline().strip("\r\n"), "baz", ending)
self.assertEqual(t.readline().strip("\r\n"), "bif", ending)
def _test_readline_io_psfile(self, test_string, ending):
2019-06-13 18:53:42 +03:00
f = io.BytesIO(test_string.encode("latin-1"))
t = EpsImagePlugin.PSFile(f)
self._test_readline(t, ending)
def _test_readline_file_psfile(self, test_string, ending):
2019-06-13 18:53:42 +03:00
f = self.tempfile("temp.txt")
with open(f, "wb") as w:
w.write(test_string.encode("latin-1"))
2019-06-13 18:53:42 +03:00
with open(f, "rb") as r:
t = EpsImagePlugin.PSFile(r)
self._test_readline(t, ending)
def test_readline(self):
# check all the freaking line endings possible from the spec
2015-04-24 02:26:52 +03:00
# test_string = u'something\r\nelse\n\rbaz\rbif\n'
2019-06-13 18:53:42 +03:00
line_endings = ["\r\n", "\n", "\n\r", "\r"]
strings = ["something", "else", "baz", "bif"]
for ending in line_endings:
s = ending.join(strings)
self._test_readline_io_psfile(s, ending)
self._test_readline_file_psfile(s, ending)
2015-03-26 15:05:17 +03:00
def test_open_eps(self):
# https://github.com/python-pillow/Pillow/issues/1104
# Arrange
2019-06-13 18:53:42 +03:00
FILES = [
"Tests/images/illu10_no_preview.eps",
"Tests/images/illu10_preview.eps",
"Tests/images/illuCS6_no_preview.eps",
"Tests/images/illuCS6_preview.eps",
]
2015-03-26 15:05:17 +03:00
2018-10-02 11:37:10 +03:00
# Act / Assert
2015-03-26 15:05:17 +03:00
for filename in FILES:
img = Image.open(filename)
2018-10-02 11:37:10 +03:00
self.assertEqual(img.mode, "RGB")
2015-03-26 15:05:17 +03:00
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
def test_emptyline(self):
# Test file includes an empty line in the header data
emptyline_file = "Tests/images/zero_bb_emptyline.eps"
image = Image.open(emptyline_file)
image.load()
self.assertEqual(image.mode, "RGB")
self.assertEqual(image.size, (460, 352))
self.assertEqual(image.format, "EPS")