diff --git a/qrcode/tests/test_qrcode.py b/qrcode/tests/test_qrcode.py index ce9a807..9709974 100644 --- a/qrcode/tests/test_qrcode.py +++ b/qrcode/tests/test_qrcode.py @@ -17,9 +17,12 @@ import qrcode.image.svg import qrcode.util from qrcode.exceptions import DataOverflowError from qrcode.image.base import BaseImage -from qrcode.image.pil import Image as pil_Image -from qrcode.image.styledpil import StyledPilImage -from qrcode.image.styles import colormasks, moduledrawers +try: + from qrcode.image.pil import Image as pil_Image + from qrcode.image.styledpil import StyledPilImage + from qrcode.image.styles import colormasks, moduledrawers +except ImportError: + pil_Image = None from qrcode.tests.svg import SvgImageWhite from qrcode.util import MODE_8BIT_BYTE, MODE_ALPHA_NUM, MODE_NUMBER, QRData @@ -106,6 +109,7 @@ class QRCodeTests(unittest.TestCase): qr.make() self.assertEqual(qr.data_list[0].mode, MODE_8BIT_BYTE) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_pil(self): qr = qrcode.QRCode() qr.add_data(UNICODE_TEXT) @@ -113,24 +117,28 @@ class QRCodeTests(unittest.TestCase): img.save(io.BytesIO()) self.assertIsInstance(img.get_image(), pil_Image.Image) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_pil_with_transparent_background(self): qr = qrcode.QRCode() qr.add_data(UNICODE_TEXT) img = qr.make_image(back_color='TransParent') img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_pil_with_red_background(self): qr = qrcode.QRCode() qr.add_data(UNICODE_TEXT) img = qr.make_image(back_color='red') img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_pil_with_rgb_color_tuples(self): qr = qrcode.QRCode() qr.add_data(UNICODE_TEXT) img = qr.make_image(back_color=(255, 195, 235), fill_color=(55, 95, 35)) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_with_pattern(self): qr = qrcode.QRCode(mask_pattern=3) qr.add_data(UNICODE_TEXT) @@ -230,12 +238,14 @@ class QRCodeTests(unittest.TestCase): with self.assertRaises(ValueError): img.save(io.BytesIO(), kind='FISH') + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_pil_image(self): qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) qr.add_data(UNICODE_TEXT) img = qr.make_image(image_factory=StyledPilImage) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_embeded_image(self): embeded_img = pil_Image.new('RGB', (10, 10), color='red') qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) @@ -243,6 +253,7 @@ class QRCodeTests(unittest.TestCase): img = qr.make_image(image_factory=StyledPilImage, embeded_image=embeded_img) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_embeded_image_path(self): tmpfile = os.path.join(self.tmpdir, "test.png") embeded_img = pil_Image.new('RGB', (10, 10), color='red') @@ -253,42 +264,49 @@ class QRCodeTests(unittest.TestCase): img.save(io.BytesIO()) os.remove(tmpfile) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_square_module_drawer(self): qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) qr.add_data(UNICODE_TEXT) img = qr.make_image(image_factory=StyledPilImage, module_drawer=moduledrawers.SquareModuleDrawer()) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_gapped_module_drawer(self): qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) qr.add_data(UNICODE_TEXT) img = qr.make_image(image_factory=StyledPilImage, module_drawer=moduledrawers.GappedSquareModuleDrawer()) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_circle_module_drawer(self): qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) qr.add_data(UNICODE_TEXT) img = qr.make_image(image_factory=StyledPilImage, module_drawer=moduledrawers.CircleModuleDrawer()) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_rounded_module_drawer(self): qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) qr.add_data(UNICODE_TEXT) img = qr.make_image(image_factory=StyledPilImage, module_drawer=moduledrawers.RoundedModuleDrawer()) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_vertical_bars_module_drawer(self): qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) qr.add_data(UNICODE_TEXT) img = qr.make_image(image_factory=StyledPilImage, module_drawer=moduledrawers.VerticalBarsDrawer()) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_horizontal_bars_module_drawer(self): qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) qr.add_data(UNICODE_TEXT) img = qr.make_image(image_factory=StyledPilImage, module_drawer=moduledrawers.HorizontalBarsDrawer()) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_default_solid_color_mask(self): qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) qr.add_data(UNICODE_TEXT) @@ -296,6 +314,7 @@ class QRCodeTests(unittest.TestCase): img = qr.make_image(image_factory=StyledPilImage, color_mask=mask) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_solid_color_mask(self): qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) qr.add_data(UNICODE_TEXT) @@ -303,6 +322,7 @@ class QRCodeTests(unittest.TestCase): img = qr.make_image(image_factory=StyledPilImage, color_mask=mask) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_color_mask_with_transparency(self): qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) qr.add_data(UNICODE_TEXT) @@ -311,6 +331,7 @@ class QRCodeTests(unittest.TestCase): img.save(io.BytesIO()) assert img.mode == "RGBA" + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_radial_gradient_color_mask(self): qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) qr.add_data(UNICODE_TEXT) @@ -318,6 +339,7 @@ class QRCodeTests(unittest.TestCase): img = qr.make_image(image_factory=StyledPilImage, color_mask=mask) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_square_gradient_color_mask(self): qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) qr.add_data(UNICODE_TEXT) @@ -325,6 +347,7 @@ class QRCodeTests(unittest.TestCase): img = qr.make_image(image_factory=StyledPilImage, color_mask=mask) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_horizontal_gradient_color_mask(self): qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) qr.add_data(UNICODE_TEXT) @@ -332,6 +355,7 @@ class QRCodeTests(unittest.TestCase): img = qr.make_image(image_factory=StyledPilImage, color_mask=mask) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_vertical_gradient_color_mask(self): qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L) qr.add_data(UNICODE_TEXT) @@ -339,6 +363,7 @@ class QRCodeTests(unittest.TestCase): img = qr.make_image(image_factory=StyledPilImage, color_mask=mask) img.save(io.BytesIO()) + @unittest.skipIf(not pil_Image, "Requires PIL") def test_render_styled_with_image_color_mask(self): img_mask = pil_Image.new('RGB', (10, 10), color='red') qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L)