Skip to content

Commit 453c118

Browse files
committed
Raise error if image is provided & correction != H
1 parent d44a409 commit 453c118

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

qrcode/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ def make_image(self, image_factory=None, **kwargs):
348348
349349
If the data has not been compiled yet, make it first.
350350
"""
351+
if kwargs.get("embeded_image_path") and self.error_correction != constants.ERROR_CORRECT_H:
352+
raise ValueError("Error correction level must be ERROR_CORRECT_H if an embedded image is provided")
351353
_check_box_size(self.box_size)
352354
if self.data_cache is None:
353355
self.make()

qrcode/tests/test_qrcode.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,35 @@ def test_render_styled_with_embeded_image_path(self):
230230
img.save(io.BytesIO())
231231
os.remove(tmpfile)
232232

233+
@unittest.skipIf(not pil_Image, "Requires PIL")
234+
def test_embedded_image_and_error_correction(self):
235+
"If an embedded image is specified, error correction must be the highest so the QR code is readable"
236+
tmpfile = os.path.join(self.tmpdir, "test.png")
237+
embedded_img = pil_Image.new("RGB", (10, 10), color="red")
238+
embedded_img.save(tmpfile)
239+
240+
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
241+
qr.add_data(UNICODE_TEXT)
242+
with self.assertRaises(ValueError):
243+
qr.make_image(embeded_image_path=tmpfile)
244+
245+
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_M)
246+
qr.add_data(UNICODE_TEXT)
247+
with self.assertRaises(ValueError):
248+
qr.make_image(embeded_image_path=tmpfile)
249+
250+
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_Q)
251+
qr.add_data(UNICODE_TEXT)
252+
with self.assertRaises(ValueError):
253+
qr.make_image(embeded_image_path=tmpfile)
254+
255+
# The only accepted correction level when an embedded image is provided
256+
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_H)
257+
qr.add_data(UNICODE_TEXT)
258+
qr.make_image(embeded_image_path=tmpfile)
259+
260+
os.remove(tmpfile)
261+
233262
@unittest.skipIf(not pil_Image, "Requires PIL")
234263
def test_render_styled_with_square_module_drawer(self):
235264
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)

0 commit comments

Comments
 (0)