def test_pillow(self): """Test that pillow is absent/present with the correct plugins.""" have_pillow = get_envar('PILLOW') if not have_pillow: raise RuntimeError("No 'PILLOW' envar has been set") if have_pillow == 'both': try: from PIL import features except ImportError: pytest.fail("PILLOW is both but PIL is not importable") assert features.check_codec("jpg") assert features.check_codec("jpg_2000") elif have_pillow == 'jpeg': try: from PIL import features except ImportError: pytest.fail("PILLOW is both but PIL is not importable") assert features.check_codec("jpg") assert not features.check_codec("jpg_2000") elif have_pillow == 'false': with pytest.raises(ImportError): import PIL else: raise NotImplementedError( "Unknown 'PILLOW' value of '{}'".format(have_pillow) )
def test_unsupported_codec(): # Arrange codec = "unsupported_codec" # Act / Assert with pytest.raises(ValueError): features.check_codec(codec) with pytest.raises(ValueError): features.version_codec(codec)
def test_check(): # Check the correctness of the convenience function for module in features.modules: assert features.check_module(module) == features.check(module) for codec in features.codecs: assert features.check_codec(codec) == features.check(codec) for feature in features.features: assert features.check_feature(feature) == features.check(feature)
def test_check(self): # Check the correctness of the convenience function for module in features.modules: self.assertEqual(features.check_module(module), features.check(module)) for codec in features.codecs: self.assertEqual(features.check_codec(codec), features.check(codec)) for feature in features.features: self.assertEqual(features.check_feature(feature), features.check(feature))
def test_pathlib(self, tmp_path): from PIL.Image import Path with Image.open(Path("Tests/images/multipage-mmap.tiff")) as im: assert im.mode == "P" assert im.size == (10, 10) with Image.open(Path("Tests/images/hopper.jpg")) as im: assert im.mode == "RGB" assert im.size == (128, 128) for ext in (".jpg", ".jp2"): if ext == ".jp2" and not features.check_codec("jpg_2000"): pytest.skip("jpg_2000 not available") temp_file = str(tmp_path / ("temp." + ext)) if os.path.exists(temp_file): os.remove(temp_file) im.save(Path(temp_file))
from pydicom.data import get_testdata_file from pydicom.uid import (UID, JPEG2000, JPEG2000Lossless, JPEGBaseline8Bit, JPEGExtended12Bit) from pydicom.encaps import defragment_data, decode_data_sequence from io import BytesIO from compressed import parse_comppressed try: import PIL from PIL import Image, features HAVE_PIL = True HAVE_JPEG = features.check_codec("jpg") HAVE_JPEG2K = features.check_codec("jpg_2000") print("import pillow done") except ImportError: HAVE_PIL = False HAVE_JPEG = False HAVE_JPEG2K = False PillowJPEG2000TransferSyntaxes = [JPEG2000, JPEG2000Lossless] PillowJPEGTransferSyntaxes = [JPEGBaseline8Bit, JPEGExtended12Bit] PillowSupportedTransferSyntaxes = (PillowJPEGTransferSyntaxes + PillowJPEG2000TransferSyntaxes) def check_pillow(ds): transfer_syntax = "1.2.840.10008.1.2.4.51" # ds.file_meta.TransferSyntaxUID
import io import sys import pytest from PIL import IcnsImagePlugin, Image, features from .helper import assert_image_equal, assert_image_similar # sample icon file TEST_FILE = "Tests/images/pillow.icns" ENABLE_JPEG2K = features.check_codec("jpg_2000") def test_sanity(): # Loading this icon by default should result in the largest size # (512x512@2x) being loaded with Image.open(TEST_FILE) as im: # Assert that there is no unclosed file warning pytest.warns(None, im.load) assert im.mode == "RGBA" assert im.size == (1024, 1024) assert im.format == "ICNS" @pytest.mark.skipif(sys.platform != "darwin", reason="Requires macOS") def test_save(tmp_path): temp_file = str(tmp_path / "temp.icns")
def test_unsupported_codec(self): # Arrange codec = "unsupported_codec" # Act / Assert self.assertRaises(ValueError, lambda: features.check_codec(codec))
# Copyright (c) 2004 by Secret Labs. # Copyright (c) 2004 by Fredrik Lundh. # Copyright (c) 2014 by Alastair Houghton. # Copyright (c) 2020 by Pan Jing. # # See the README file for information on usage and redistribution. # import io import os import struct import sys from PIL import Image, ImageFile, PngImagePlugin, features enable_jpeg2k = features.check_codec("jpg_2000") if enable_jpeg2k: from PIL import Jpeg2KImagePlugin MAGIC = b"icns" HEADERSIZE = 8 def nextheader(fobj): return struct.unpack(">4sI", fobj.read(HEADERSIZE)) def read_32t(fobj, start_length, size): # The 128x128 icon seems to have an extra header for some reason. (start, length) = start_length fobj.seek(start)
if supported is None: # A method was being tested, but the module required # for the method could not be correctly imported pass elif supported: print("---", feature, "support ok") else: print("***", feature, "support not installed") for name, feature in [ ("jpg", "JPEG"), ("jpg_2000", "OPENJPEG (JPEG2000)"), ("zlib", "ZLIB (PNG/ZIP)"), ("libtiff", "LIBTIFF") ]: if features.check_codec(name): print("---", feature, "support ok") else: print("***", feature, "support not installed") print("-"*68) # use doctest to make sure the test program behaves as documented! import doctest print("Running selftest:") status = doctest.testmod(sys.modules[__name__]) if status[0]: print("*** %s tests of %d failed." % status) exit_status = 1 else: print("--- %s tests passed." % status[1])
def test_check_codecs(): for feature in features.codecs: assert features.check_codec(feature) in [True, False]
assert pim.bits_per_component == bpc outstream = BytesIO() pim.extract_to(stream=outstream) im = pim.as_pil_image().convert('RGB') assert im.getpixel((1, 1)) == rgb def test_bool_in_inline_image(): piim = PdfInlineImage(image_data=b'', image_object=(Name.IM, True)) assert piim.image_mask @pytest.mark.skipif( not PIL_features.check_codec('jpg_2000'), reason='no JPEG2000 codec' ) def test_jp2(resources): pdf = Pdf.open(resources / 'pike-jp2.pdf') xobj = next(iter(pdf.pages[0].images.values())) pim = PdfImage(xobj) assert isinstance(pim, PdfJpxImage) assert '/JPXDecode' in pim.filters assert pim.colorspace == '/DeviceRGB' assert not pim.is_inline assert not pim.indexed assert pim.mode == 'RGB' assert pim.bits_per_component == 8 assert pim.__eq__(42) is NotImplemented assert pim == PdfImage(xobj)
def test_check_modules(self): for feature in features.modules: self.assertIn(features.check_module(feature), [True, False]) for feature in features.codecs: self.assertIn(features.check_codec(feature), [True, False])
def test_check_features(self): for feature in features.modules: self.assertTrue( features.check_module(feature) in [True, False, None]) for feature in features.codecs: self.assertTrue(features.check_codec(feature) in [True, False])
assert pim.palette[0] == 'RGB' assert pim.colorspace == '/DeviceRGB' assert not pim.is_inline assert pim.mode == 'P' assert pim.bits_per_component == bpc outstream = BytesIO() pim.extract_to(stream=outstream) def test_bool_in_inline_image(): piim = PdfInlineImage(image_data=b'', image_object=(Name.IM, True)) assert piim.image_mask @pytest.mark.skipif(not PIL_features.check_codec('jpg_2000'), reason='no JPEG2000 codec') def test_jp2(resources): pdf = Pdf.open(resources / 'pike-jp2.pdf') xobj = next(iter(pdf.pages[0].images.values())) pim = PdfImage(xobj) assert '/JPXDecode' in pim.filters assert pim.colorspace == '/DeviceRGB' assert not pim.is_inline assert not pim.indexed assert pim.mode == 'RGB' assert pim.bits_per_component == 8 outstream = BytesIO() pim.extract_to(stream=outstream)
def test_check_features(self): for feature in features.modules: self.assertTrue(features.check_module(feature) in [True, False, None]) for feature in features.codecs: self.assertTrue(features.check_codec(feature) in [True, False])
("freetype2", "FREETYPE2"), ("littlecms2", "LITTLECMS2"), ("webp", "WEBP"), ("transp_webp", "Transparent WEBP")]: supported = features.check_module(name) if supported is None: # A method was being tested, but the module required # for the method could not be correctly imported pass elif supported: print("---", feature, "support ok") else: print("***", feature, "support not installed") for name, feature in [("jpg", "JPEG"), ("jpg_2000", "OPENJPEG (JPEG2000)"), ("zlib", "ZLIB (PNG/ZIP)"), ("libtiff", "LIBTIFF")]: if features.check_codec(name): print("---", feature, "support ok") else: print("***", feature, "support not installed") print("-" * 68) # use doctest to make sure the test program behaves as documented! import doctest print("Running selftest:") status = doctest.testmod(sys.modules[__name__]) if status[0]: print("*** %s tests of %d failed." % status) exit_status = 1 else: print("--- %s tests passed." % status[1])
# Copyright 2014-2016 OpenMarket Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from PIL.features import check_codec # check for JPEG support. if not check_codec("jpg"): raise Exception( "FATAL: jpeg codec not supported. Install pillow correctly! " " 'sudo apt-get install libjpeg-dev' then 'pip uninstall pillow &&" " pip install pillow --user'") # check for PNG support. if not check_codec("zlib"): raise Exception( "FATAL: zip codec not supported. Install pillow correctly! " " 'sudo apt-get install libjpeg-dev' then 'pip uninstall pillow &&" " pip install pillow --user'")