|
1 | 1 | import io |
2 | | -import os |
3 | 2 | import unittest |
4 | | -import warnings |
5 | | -from tempfile import mkdtemp |
6 | 3 | from unittest import mock |
7 | 4 |
|
8 | | -import png |
9 | | - |
10 | 5 | import qrcode |
11 | 6 | import qrcode.util |
12 | | -from qrcode.compat.pil import Image as pil_Image |
13 | 7 | from qrcode.exceptions import DataOverflowError |
14 | 8 | from qrcode.image.base import BaseImage |
15 | | -from qrcode.image.pure import PyPNGImage |
16 | | -from qrcode.image.styledpil import StyledPilImage |
17 | | -from qrcode.image.styles import colormasks, moduledrawers |
18 | | -from qrcode.tests.consts import UNICODE_TEXT, WHITE, BLACK, RED |
| 9 | +from qrcode.tests.consts import UNICODE_TEXT |
19 | 10 | from qrcode.util import MODE_8BIT_BYTE, MODE_ALPHA_NUM, MODE_NUMBER, QRData |
20 | 11 |
|
21 | 12 |
|
22 | 13 | class QRCodeTests(unittest.TestCase): |
23 | | - def setUp(self): |
24 | | - self.tmpdir = mkdtemp() |
25 | | - |
26 | | - def tearDown(self): |
27 | | - os.rmdir(self.tmpdir) |
28 | | - |
29 | 14 | def test_basic(self): |
30 | 15 | qr = qrcode.QRCode(version=1) |
31 | 16 | qr.add_data("a") |
@@ -95,42 +80,6 @@ def test_mode_8bit_newline(self): |
95 | 80 | qr.make() |
96 | 81 | self.assertEqual(qr.data_list[0].mode, MODE_8BIT_BYTE) |
97 | 82 |
|
98 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
99 | | - def test_render_pil(self): |
100 | | - qr = qrcode.QRCode() |
101 | | - qr.add_data(UNICODE_TEXT) |
102 | | - img = qr.make_image() |
103 | | - img.save(io.BytesIO()) |
104 | | - self.assertIsInstance(img.get_image(), pil_Image.Image) |
105 | | - |
106 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
107 | | - def test_render_pil_with_transparent_background(self): |
108 | | - qr = qrcode.QRCode() |
109 | | - qr.add_data(UNICODE_TEXT) |
110 | | - img = qr.make_image(back_color="TransParent") |
111 | | - img.save(io.BytesIO()) |
112 | | - |
113 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
114 | | - def test_render_pil_with_red_background(self): |
115 | | - qr = qrcode.QRCode() |
116 | | - qr.add_data(UNICODE_TEXT) |
117 | | - img = qr.make_image(back_color="red") |
118 | | - img.save(io.BytesIO()) |
119 | | - |
120 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
121 | | - def test_render_pil_with_rgb_color_tuples(self): |
122 | | - qr = qrcode.QRCode() |
123 | | - qr.add_data(UNICODE_TEXT) |
124 | | - img = qr.make_image(back_color=(255, 195, 235), fill_color=(55, 95, 35)) |
125 | | - img.save(io.BytesIO()) |
126 | | - |
127 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
128 | | - def test_render_with_pattern(self): |
129 | | - qr = qrcode.QRCode(mask_pattern=3) |
130 | | - qr.add_data(UNICODE_TEXT) |
131 | | - img = qr.make_image() |
132 | | - img.save(io.BytesIO()) |
133 | | - |
134 | 83 | def test_make_image_with_wrong_pattern(self): |
135 | 84 | with self.assertRaises(TypeError): |
136 | 85 | qrcode.QRCode(mask_pattern="string pattern") |
@@ -171,189 +120,6 @@ class MockFactory(BaseImage): |
171 | 120 | self.assertTrue(MockFactory.new_image.called) |
172 | 121 | self.assertTrue(MockFactory.drawrect.called) |
173 | 122 |
|
174 | | - def test_render_pypng(self): |
175 | | - qr = qrcode.QRCode() |
176 | | - qr.add_data(UNICODE_TEXT) |
177 | | - img = qr.make_image(image_factory=PyPNGImage) |
178 | | - self.assertIsInstance(img.get_image(), png.Writer) |
179 | | - |
180 | | - print(img.width, img.box_size, img.border) |
181 | | - img.save(io.BytesIO()) |
182 | | - |
183 | | - def test_render_pypng_to_str(self): |
184 | | - qr = qrcode.QRCode() |
185 | | - qr.add_data(UNICODE_TEXT) |
186 | | - img = qr.make_image(image_factory=PyPNGImage) |
187 | | - self.assertIsInstance(img.get_image(), png.Writer) |
188 | | - |
189 | | - mock_open = mock.mock_open() |
190 | | - with mock.patch("qrcode.image.pure.open", mock_open, create=True): |
191 | | - img.save("test_file.png") |
192 | | - mock_open.assert_called_once_with("test_file.png", "wb") |
193 | | - mock_open("test_file.png", "wb").write.assert_called() |
194 | | - |
195 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
196 | | - def test_render_styled_Image(self): |
197 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
198 | | - qr.add_data(UNICODE_TEXT) |
199 | | - img = qr.make_image(image_factory=StyledPilImage) |
200 | | - img.save(io.BytesIO()) |
201 | | - |
202 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
203 | | - def test_render_styled_with_embeded_image(self): |
204 | | - embeded_img = pil_Image.new("RGB", (10, 10), color="red") |
205 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
206 | | - qr.add_data(UNICODE_TEXT) |
207 | | - img = qr.make_image(image_factory=StyledPilImage, embeded_image=embeded_img) |
208 | | - img.save(io.BytesIO()) |
209 | | - |
210 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
211 | | - def test_render_styled_with_embeded_image_path(self): |
212 | | - tmpfile = os.path.join(self.tmpdir, "test.png") |
213 | | - embeded_img = pil_Image.new("RGB", (10, 10), color="red") |
214 | | - embeded_img.save(tmpfile) |
215 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
216 | | - qr.add_data(UNICODE_TEXT) |
217 | | - img = qr.make_image(image_factory=StyledPilImage, embeded_image_path=tmpfile) |
218 | | - img.save(io.BytesIO()) |
219 | | - os.remove(tmpfile) |
220 | | - |
221 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
222 | | - def test_render_styled_with_square_module_drawer(self): |
223 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
224 | | - qr.add_data(UNICODE_TEXT) |
225 | | - img = qr.make_image( |
226 | | - image_factory=StyledPilImage, |
227 | | - module_drawer=moduledrawers.SquareModuleDrawer(), |
228 | | - ) |
229 | | - img.save(io.BytesIO()) |
230 | | - |
231 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
232 | | - def test_render_styled_with_gapped_module_drawer(self): |
233 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
234 | | - qr.add_data(UNICODE_TEXT) |
235 | | - img = qr.make_image( |
236 | | - image_factory=StyledPilImage, |
237 | | - module_drawer=moduledrawers.GappedSquareModuleDrawer(), |
238 | | - ) |
239 | | - img.save(io.BytesIO()) |
240 | | - |
241 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
242 | | - def test_render_styled_with_circle_module_drawer(self): |
243 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
244 | | - qr.add_data(UNICODE_TEXT) |
245 | | - img = qr.make_image( |
246 | | - image_factory=StyledPilImage, |
247 | | - module_drawer=moduledrawers.CircleModuleDrawer(), |
248 | | - ) |
249 | | - img.save(io.BytesIO()) |
250 | | - |
251 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
252 | | - def test_render_styled_with_rounded_module_drawer(self): |
253 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
254 | | - qr.add_data(UNICODE_TEXT) |
255 | | - img = qr.make_image( |
256 | | - image_factory=StyledPilImage, |
257 | | - module_drawer=moduledrawers.RoundedModuleDrawer(), |
258 | | - ) |
259 | | - img.save(io.BytesIO()) |
260 | | - |
261 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
262 | | - def test_render_styled_with_vertical_bars_module_drawer(self): |
263 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
264 | | - qr.add_data(UNICODE_TEXT) |
265 | | - img = qr.make_image( |
266 | | - image_factory=StyledPilImage, |
267 | | - module_drawer=moduledrawers.VerticalBarsDrawer(), |
268 | | - ) |
269 | | - img.save(io.BytesIO()) |
270 | | - |
271 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
272 | | - def test_render_styled_with_horizontal_bars_module_drawer(self): |
273 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
274 | | - qr.add_data(UNICODE_TEXT) |
275 | | - img = qr.make_image( |
276 | | - image_factory=StyledPilImage, |
277 | | - module_drawer=moduledrawers.HorizontalBarsDrawer(), |
278 | | - ) |
279 | | - img.save(io.BytesIO()) |
280 | | - |
281 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
282 | | - def test_render_styled_with_default_solid_color_mask(self): |
283 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
284 | | - qr.add_data(UNICODE_TEXT) |
285 | | - mask = colormasks.SolidFillColorMask() |
286 | | - img = qr.make_image(image_factory=StyledPilImage, color_mask=mask) |
287 | | - img.save(io.BytesIO()) |
288 | | - |
289 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
290 | | - def test_render_styled_with_solid_color_mask(self): |
291 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
292 | | - qr.add_data(UNICODE_TEXT) |
293 | | - mask = colormasks.SolidFillColorMask(back_color=WHITE, front_color=RED) |
294 | | - img = qr.make_image(image_factory=StyledPilImage, color_mask=mask) |
295 | | - img.save(io.BytesIO()) |
296 | | - |
297 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
298 | | - def test_render_styled_with_color_mask_with_transparency(self): |
299 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
300 | | - qr.add_data(UNICODE_TEXT) |
301 | | - mask = colormasks.SolidFillColorMask( |
302 | | - back_color=(255, 0, 255, 255), front_color=RED |
303 | | - ) |
304 | | - img = qr.make_image(image_factory=StyledPilImage, color_mask=mask) |
305 | | - img.save(io.BytesIO()) |
306 | | - assert img.mode == "RGBA" |
307 | | - |
308 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
309 | | - def test_render_styled_with_radial_gradient_color_mask(self): |
310 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
311 | | - qr.add_data(UNICODE_TEXT) |
312 | | - mask = colormasks.RadialGradiantColorMask( |
313 | | - back_color=WHITE, center_color=BLACK, edge_color=RED |
314 | | - ) |
315 | | - img = qr.make_image(image_factory=StyledPilImage, color_mask=mask) |
316 | | - img.save(io.BytesIO()) |
317 | | - |
318 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
319 | | - def test_render_styled_with_square_gradient_color_mask(self): |
320 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
321 | | - qr.add_data(UNICODE_TEXT) |
322 | | - mask = colormasks.SquareGradiantColorMask( |
323 | | - back_color=WHITE, center_color=BLACK, edge_color=RED |
324 | | - ) |
325 | | - img = qr.make_image(image_factory=StyledPilImage, color_mask=mask) |
326 | | - img.save(io.BytesIO()) |
327 | | - |
328 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
329 | | - def test_render_styled_with_horizontal_gradient_color_mask(self): |
330 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
331 | | - qr.add_data(UNICODE_TEXT) |
332 | | - mask = colormasks.HorizontalGradiantColorMask( |
333 | | - back_color=WHITE, left_color=RED, right_color=BLACK |
334 | | - ) |
335 | | - img = qr.make_image(image_factory=StyledPilImage, color_mask=mask) |
336 | | - img.save(io.BytesIO()) |
337 | | - |
338 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
339 | | - def test_render_styled_with_vertical_gradient_color_mask(self): |
340 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
341 | | - qr.add_data(UNICODE_TEXT) |
342 | | - mask = colormasks.VerticalGradiantColorMask( |
343 | | - back_color=WHITE, top_color=RED, bottom_color=BLACK |
344 | | - ) |
345 | | - img = qr.make_image(image_factory=StyledPilImage, color_mask=mask) |
346 | | - img.save(io.BytesIO()) |
347 | | - |
348 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
349 | | - def test_render_styled_with_image_color_mask(self): |
350 | | - img_mask = pil_Image.new("RGB", (10, 10), color="red") |
351 | | - qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L) |
352 | | - qr.add_data(UNICODE_TEXT) |
353 | | - mask = colormasks.ImageColorMask(back_color=WHITE, color_mask_image=img_mask) |
354 | | - img = qr.make_image(image_factory=StyledPilImage, color_mask=mask) |
355 | | - img.save(io.BytesIO()) |
356 | | - |
357 | 123 | def test_optimize(self): |
358 | 124 | qr = qrcode.QRCode() |
359 | 125 | text = "A1abc12345def1HELLOa" |
@@ -475,9 +241,3 @@ def test_negative_size_at_usage(self): |
475 | 241 | qr = qrcode.QRCode() |
476 | 242 | qr.box_size = -1 |
477 | 243 | self.assertRaises(ValueError, qr.make_image) |
478 | | - |
479 | | - |
480 | | -class ShortcutTest(unittest.TestCase): |
481 | | - @unittest.skipIf(not pil_Image, "Requires PIL") |
482 | | - def runTest(self): |
483 | | - qrcode.make("image") |
0 commit comments