Pillow/Tests/test_psdraw.py

72 lines
1.7 KiB
Python
Raw Normal View History

from helper import unittest, PillowTestCase
2014-12-11 14:06:53 +03:00
from PIL import Image, PSDraw
import os
import sys
2014-12-11 14:06:53 +03:00
class TestPsDraw(PillowTestCase):
2014-12-11 14:06:53 +03:00
def _create_document(self, ps):
2014-12-11 14:06:53 +03:00
im = Image.open("Tests/images/hopper.ppm")
title = "hopper"
box = (1*72, 2*72, 7*72, 10*72) # in points
2014-12-11 14:06:53 +03:00
ps.begin_document(title)
2014-12-27 23:50:17 +03:00
# draw diagonal lines in a cross
ps.line((1*72, 2*72), (7*72, 10*72))
ps.line((7*72, 2*72), (1*72, 10*72))
2014-12-11 14:06:53 +03:00
# draw the image (75 dpi)
ps.image(box, im, 75)
ps.rectangle(box)
# draw title
ps.setfont("Courier", 36)
ps.text((3*72, 4*72), title)
2014-12-11 14:06:53 +03:00
ps.end_document()
def test_draw_postscript(self):
# Based on Pillow tutorial, but there is no textsize:
# https://pillow.readthedocs.io/en/latest/handbook/tutorial.html
# Arrange
tempfile = self.tempfile('temp.ps')
fp = open(tempfile, "wb")
# Act
ps = PSDraw.PSDraw(fp)
self._create_document(ps)
2014-12-11 14:20:11 +03:00
fp.close()
2014-12-11 14:06:53 +03:00
# Assert
# Check non-zero file was created
self.assertTrue(os.path.isfile(tempfile))
self.assertGreater(os.path.getsize(tempfile), 0)
2014-12-11 14:06:53 +03:00
def test_stdout(self):
# Temporarily redirect stdout
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()
ps = PSDraw.PSDraw()
self._create_document(ps)
# Reset stdout
sys.stdout = old_stdout
self.assertNotEqual(mystdout.getvalue(), "")
2014-12-11 14:06:53 +03:00
if __name__ == '__main__':
unittest.main()
# End of file