Example #1
0
 def image(self, box, im, dpi=None):
     """Draw a PIL image, centered in the given box."""
     # default resolution depends on mode
     if not dpi:
         if im.mode == "1":
             dpi = 200  # fax
         else:
             dpi = 100  # greyscale
     # image size (on paper)
     x = float(im.size[0] * 72) / dpi
     y = float(im.size[1] * 72) / dpi
     # max allowed size
     xmax = float(box[2] - box[0])
     ymax = float(box[3] - box[1])
     if x > xmax:
         y = y * xmax / x
         x = xmax
     if y > ymax:
         x = x * ymax / y
         y = ymax
     dx = (xmax - x) / 2 + box[0]
     dy = (ymax - y) / 2 + box[1]
     self._fp_write("gsave\n%f %f translate\n" % (dx, dy))
     if (x, y) != im.size:
         # EpsImagePlugin._save prints the image at (0,0,xsize,ysize)
         sx = x / im.size[0]
         sy = y / im.size[1]
         self._fp_write("%f %f scale\n" % (sx, sy))
     EpsImagePlugin._save(im, self.fp, None, 0)
     self._fp_write("\ngrestore\n")
Example #2
0
 def image(self, box, im, dpi = None):
     """Draw a PIL image, centered in the given box."""
     # default resolution depends on mode
     if not dpi:
         if im.mode == "1":
             dpi = 200 # fax
         else:
             dpi = 100 # greyscale
     # image size (on paper)
     x = float(im.size[0] * 72) / dpi
     y = float(im.size[1] * 72) / dpi
     # max allowed size
     xmax = float(box[2] - box[0])
     ymax = float(box[3] - box[1])
     if x > xmax:
         y = y * xmax / x; x = xmax
     if y > ymax:
         x = x * ymax / y; y = ymax
     dx = (xmax - x) / 2 + box[0]
     dy = (ymax - y) / 2 + box[1]
     self.fp.write("gsave\n%f %f translate\n" % (dx, dy))
     if (x, y) != im.size:
         # EpsImagePlugin._save prints the image at (0,0,xsize,ysize)
         sx = x / im.size[0]
         sy = y / im.size[1]
         self.fp.write("%f %f scale\n" % (sx, sy))
     EpsImagePlugin._save(im, self.fp, None, 0)
     self.fp.write("\ngrestore\n")
Example #3
0
    def _test_readline_file_psfile(test_string, ending):
        f = str(tmp_path / "temp.txt")
        with open(f, "wb") as w:
            w.write(test_string.encode("latin-1"))

        with open(f, "rb") as r:
            t = EpsImagePlugin.PSFile(r)
            _test_readline(t, ending)
Example #4
0
    def _test_readline_file_psfile(self, test_string, ending):
        f = self.tempfile('temp.txt')
        with open(f, 'wb') as w:
            w.write(test_string.encode('latin-1'))

        with open(f, 'rb') as r:
            t = EpsImagePlugin.PSFile(r)
            self._test_readline(t, ending)
Example #5
0
def generate_barcode(barcode_type, data, options=None):
    if barcode_type not in barcode_types:
        raise NotImplementedError('unsupported barcode type {!r}'.format(barcode_type))
    if options is None:
        options = {}
    code = _format_code(barcode_type, data, options)
    bbox_lines = _get_bbox(code)
    full_code = EPS_TEMPLATE.format(bbox=bbox_lines, bwipp=BWIPP, code=code)
    return EpsImagePlugin.EpsImageFile(io.BytesIO(full_code.encode('utf8')))
Example #6
0
    def test_parser(self):

        def roundtrip(format):

            im = hopper("L").resize((1000, 1000))
            if format in ("MSP", "XBM"):
                im = im.convert("1")

            test_file = BytesIO()

            im.copy().save(test_file, format)

            data = test_file.getvalue()

            parser = ImageFile.Parser()
            parser.feed(data)
            imOut = parser.close()

            return im, imOut

        self.assert_image_equal(*roundtrip("BMP"))
        im1, im2 = roundtrip("GIF")
        self.assert_image_similar(im1.convert('P'), im2, 1)
        self.assert_image_equal(*roundtrip("IM"))
        self.assert_image_equal(*roundtrip("MSP"))
        if "zip_encoder" in codecs:
            try:
                # force multiple blocks in PNG driver
                ImageFile.MAXBLOCK = 8192
                self.assert_image_equal(*roundtrip("PNG"))
            finally:
                ImageFile.MAXBLOCK = MAXBLOCK
        self.assert_image_equal(*roundtrip("PPM"))
        self.assert_image_equal(*roundtrip("TIFF"))
        self.assert_image_equal(*roundtrip("XBM"))
        self.assert_image_equal(*roundtrip("TGA"))
        self.assert_image_equal(*roundtrip("PCX"))

        if EpsImagePlugin.has_ghostscript():
            im1, im2 = roundtrip("EPS")
            # This test fails on Ubuntu 12.04, PPC (Bigendian) It
            # appears to be a ghostscript 9.05 bug, since the
            # ghostscript rendering is wonky and the file is identical
            # to that written on ubuntu 12.04 x64
            # md5sum: ba974835ff2d6f3f2fd0053a23521d4a

            # EPS comes back in RGB:
            self.assert_image_similar(im1, im2.convert('L'), 20)

        if "jpeg_encoder" in codecs:
            im1, im2 = roundtrip("JPEG")  # lossy compression
            self.assert_image(im1, im2.mode, im2.size)

        self.assertRaises(IOError, lambda: roundtrip("PDF"))
Example #7
0
    def test_parser(self):
        def roundtrip(format):

            im = hopper("L").resize((1000, 1000), Image.NEAREST)
            if format in ("MSP", "XBM"):
                im = im.convert("1")

            test_file = BytesIO()

            im.copy().save(test_file, format)

            data = test_file.getvalue()

            parser = ImageFile.Parser()
            parser.feed(data)
            imOut = parser.close()

            return im, imOut

        assert_image_equal(*roundtrip("BMP"))
        im1, im2 = roundtrip("GIF")
        assert_image_similar(im1.convert("P"), im2, 1)
        assert_image_equal(*roundtrip("IM"))
        assert_image_equal(*roundtrip("MSP"))
        if features.check("zlib"):
            try:
                # force multiple blocks in PNG driver
                ImageFile.MAXBLOCK = 8192
                assert_image_equal(*roundtrip("PNG"))
            finally:
                ImageFile.MAXBLOCK = MAXBLOCK
        assert_image_equal(*roundtrip("PPM"))
        assert_image_equal(*roundtrip("TIFF"))
        assert_image_equal(*roundtrip("XBM"))
        assert_image_equal(*roundtrip("TGA"))
        assert_image_equal(*roundtrip("PCX"))

        if EpsImagePlugin.has_ghostscript():
            im1, im2 = roundtrip("EPS")
            # This test fails on Ubuntu 12.04, PPC (Bigendian) It
            # appears to be a ghostscript 9.05 bug, since the
            # ghostscript rendering is wonky and the file is identical
            # to that written on ubuntu 12.04 x64
            # md5sum: ba974835ff2d6f3f2fd0053a23521d4a

            # EPS comes back in RGB:
            assert_image_similar(im1, im2.convert("L"), 20)

        if features.check("jpg"):
            im1, im2 = roundtrip("JPEG")  # lossy compression
            assert_image(im1, im2.mode, im2.size)

        with pytest.raises(OSError):
            roundtrip("PDF")
Example #8
0
    def test_parser(self):
        def roundtrip(format):

            im = hopper("L").resize((1000, 1000))
            if format in ("MSP", "XBM"):
                im = im.convert("1")

            file = BytesIO()

            im.save(file, format)

            data = file.getvalue()

            parser = ImageFile.Parser()
            parser.feed(data)
            imOut = parser.close()

            return im, imOut

        self.assert_image_equal(*roundtrip("BMP"))
        self.assert_image_equal(*roundtrip("GIF"))
        self.assert_image_equal(*roundtrip("IM"))
        self.assert_image_equal(*roundtrip("MSP"))
        if "zip_encoder" in codecs:
            try:
                # force multiple blocks in PNG driver
                ImageFile.MAXBLOCK = 8192
                self.assert_image_equal(*roundtrip("PNG"))
            finally:
                ImageFile.MAXBLOCK = MAXBLOCK
        self.assert_image_equal(*roundtrip("PPM"))
        self.assert_image_equal(*roundtrip("TIFF"))
        self.assert_image_equal(*roundtrip("XBM"))
        self.assert_image_equal(*roundtrip("TGA"))
        self.assert_image_equal(*roundtrip("PCX"))

        if EpsImagePlugin.has_ghostscript():
            im1, im2 = roundtrip("EPS")
            # This test fails on Ubuntu 12.04, PPC (Bigendian) It
            # appears to be a ghostscript 9.05 bug, since the
            # ghostscript rendering is wonky and the file is identical
            # to that written on ubuntu 12.04 x64
            # md5sum: ba974835ff2d6f3f2fd0053a23521d4a

            # EPS comes back in RGB:
            self.assert_image_similar(im1, im2.convert('L'), 20)

        if "jpeg_encoder" in codecs:
            im1, im2 = roundtrip("JPEG")  # lossy compression
            self.assert_image(im1, im2.mode, im2.size)

        self.assertRaises(IOError, lambda: roundtrip("PDF"))
Example #9
0
def generate_barcode(
    barcode_type: str,
    data: str | bytes,
    options: dict[str, str | bool] | None = None,
) -> EpsImagePlugin.EpsImageFile:
    if barcode_type not in barcode_types:
        raise NotImplementedError(f"unsupported barcode type {barcode_type!r}")
    if options is None:
        options = {}
    code = _format_code(barcode_type, data, options)
    bbox_lines = _get_bbox(code)
    full_code = EPS_TEMPLATE.format(bbox=bbox_lines, bwipp=BWIPP, code=code)
    return EpsImagePlugin.EpsImageFile(io.BytesIO(full_code.encode()))
Example #10
0
    def test_parser(self):

        def roundtrip(format):

            im = lena("L").resize((1000, 1000))
            if format in ("MSP", "XBM"):
                im = im.convert("1")

            file = BytesIO()

            im.save(file, format)

            data = file.getvalue()

            parser = ImageFile.Parser()
            parser.feed(data)
            imOut = parser.close()

            return im, imOut

        self.assert_image_equal(*roundtrip("BMP"))
        self.assert_image_equal(*roundtrip("GIF"))
        self.assert_image_equal(*roundtrip("IM"))
        self.assert_image_equal(*roundtrip("MSP"))
        if "zip_encoder" in codecs:
            try:
                # force multiple blocks in PNG driver
                ImageFile.MAXBLOCK = 8192
                self.assert_image_equal(*roundtrip("PNG"))
            finally:
                ImageFile.MAXBLOCK = MAXBLOCK
        self.assert_image_equal(*roundtrip("PPM"))
        self.assert_image_equal(*roundtrip("TIFF"))
        self.assert_image_equal(*roundtrip("XBM"))
        self.assert_image_equal(*roundtrip("TGA"))
        self.assert_image_equal(*roundtrip("PCX"))

        if EpsImagePlugin.has_ghostscript():
            im1, im2 = roundtrip("EPS")
            # EPS comes back in RGB:
            self.assert_image_similar(im1, im2.convert('L'), 20)

        if "jpeg_encoder" in codecs:
            im1, im2 = roundtrip("JPEG")  # lossy compression
            self.assert_image(im1, im2.mode, im2.size)

        self.assertRaises(IOError, lambda: roundtrip("PDF"))
Example #11
0
    def test_parser(self):

        def roundtrip(format):

            im = hopper("L").resize((1000, 1000))
            if format in ("MSP", "XBM"):
                im = im.convert("1")

            file = BytesIO()

            im.save(file, format)

            data = file.getvalue()

            parser = ImageFile.Parser()
            parser.feed(data)
            imOut = parser.close()

            return im, imOut

        self.assert_image_equal(*roundtrip("BMP"))
        self.assert_image_equal(*roundtrip("GIF"))
        self.assert_image_equal(*roundtrip("IM"))
        self.assert_image_equal(*roundtrip("MSP"))
        if "zip_encoder" in codecs:
            try:
                # force multiple blocks in PNG driver
                ImageFile.MAXBLOCK = 8192
                self.assert_image_equal(*roundtrip("PNG"))
            finally:
                ImageFile.MAXBLOCK = MAXBLOCK
        self.assert_image_equal(*roundtrip("PPM"))
        self.assert_image_equal(*roundtrip("TIFF"))
        self.assert_image_equal(*roundtrip("XBM"))
        self.assert_image_equal(*roundtrip("TGA"))
        self.assert_image_equal(*roundtrip("PCX"))

        if EpsImagePlugin.has_ghostscript():
            im1, im2 = roundtrip("EPS")
            # EPS comes back in RGB:
            self.assert_image_similar(im1, im2.convert('L'), 20)

        if "jpeg_encoder" in codecs:
            im1, im2 = roundtrip("JPEG")  # lossy compression
            self.assert_image(im1, im2.mode, im2.size)

        self.assertRaises(IOError, lambda: roundtrip("PDF"))
Example #12
0
def generate_barcode(barcode_type, data, options=None, template_options=None):
    if barcode_type not in barcode_types:
        raise NotImplementedError(
            "unsupported barcode type {!r}".format(barcode_type))
    if options is None:
        options = {}

    template_options_with_defaults = dict(scale_x=2, scale_y=2)
    if template_options is not None:
        template_options_with_defaults.update(template_options)
    code = _format_code(barcode_type, data, options)
    bbox_lines = _get_bbox(code, template_options_with_defaults)
    full_code = EPS_TEMPLATE.format(bbox=bbox_lines,
                                    bwipp=BWIPP,
                                    code=code,
                                    **template_options_with_defaults)
    return EpsImagePlugin.EpsImageFile(io.BytesIO(full_code.encode("utf8")))
Example #13
0
from .helper import unittest, PillowTestCase, hopper

from PIL import Image, EpsImagePlugin
import io

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"

# EPS test files with binary preview
file3 = "Tests/images/binary_preview_map.eps"


class TestFileEps(PillowTestCase):

    @unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
    def test_sanity(self):
        # Regular scale
        image1 = Image.open(file1)
        image1.load()
        self.assertEqual(image1.mode, "RGB")
        self.assertEqual(image1.size, (460, 352))
Example #14
0
    def test_invalid_file(self):
        invalid_file = "Tests/images/flower.jpg"

        self.assertRaises(SyntaxError,
                          lambda: EpsImagePlugin.EpsImageFile(invalid_file))
Example #15
0
 def setUp(self):
     if not EpsImagePlugin.has_ghostscript():
         self.skipTest("Ghostscript not available")
Example #16
0
def test_invalid_file():
    invalid_file = "Tests/images/flower.jpg"

    with pytest.raises(SyntaxError):
        EpsImagePlugin.EpsImageFile(invalid_file)
Example #17
0
 def _test_readline_io_psfile(test_string, ending):
     f = io.BytesIO(test_string.encode("latin-1"))
     t = EpsImagePlugin.PSFile(f)
     _test_readline(t, ending)
Example #18
0
import io

import pytest

from PIL import EpsImagePlugin, Image, features

from .helper import assert_image_similar, hopper, skip_unless_feature

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"

# EPS test files with binary preview
FILE3 = "Tests/images/binary_preview_map.eps"


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_sanity():
    # Regular scale
    with Image.open(FILE1) as image1:
        image1.load()
        assert image1.mode == "RGB"
Example #19
0
from tester import *

from PIL import Image, EpsImagePlugin
import sys
import io

if not EpsImagePlugin.has_ghostscript():
    skip()

#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"

def test_sanity():
    #Regular scale
    image1 = Image.open(file1)
    image1.load()
    assert_equal(image1.mode, "RGB")
    assert_equal(image1.size, (460, 352))
    assert_equal(image1.format, "EPS")

    image2 = Image.open(file2)
    image2.load()
    assert_equal(image2.mode, "RGB")
Example #20
0
from tester import *

from PIL import Image, EpsImagePlugin
import sys
import io

if not EpsImagePlugin.has_ghostscript():
    skip()

#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"


def test_sanity():
    #Regular scale
    image1 = Image.open(file1)
    image1.load()
    assert_equal(image1.mode, "RGB")
    assert_equal(image1.size, (460, 352))
    assert_equal(image1.format, "EPS")

    image2 = Image.open(file2)
    image2.load()
Example #21
0
def generate_barcode(barcode_type, data, options):
    code = _format_code(barcode_type, data, options)
    bbox_lines = _get_bbox(code)
    full_code = EPS_TEMPLATE.format(bbox=bbox_lines, bwipp=BWIPP, code=code)
    return EpsImagePlugin.EpsImageFile(io.BytesIO(full_code.encode('utf8')))
Example #22
0
 def _test_readline_io_psfile(self, test_string, ending):
     f = io.BytesIO(test_string.encode('latin-1'))
     t = EpsImagePlugin.PSFile(f)
     self._test_readline(t, ending)