Skip to content

Commit

Permalink
Raise error if embedded image is provided and error correction is low…
Browse files Browse the repository at this point in the history
…er than H
  • Loading branch information
maribedran committed Sep 27, 2024
1 parent ffa2b4a commit 68f3b29
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 6 additions & 2 deletions qrcode/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,12 @@ def make_image(self, image_factory=None, **kwargs):
If the data has not been compiled yet, make it first.
"""
if kwargs.get("embeded_image_path") and self.error_correction != constants.ERROR_CORRECT_H:
raise ValueError("Error correction level must be ERROR_CORRECT_H if an embedded image is provided")
if (
kwargs.get("embeded_image_path") or kwargs.get("embeded_image")
) and self.error_correction != constants.ERROR_CORRECT_H:
raise ValueError(
"Error correction level must be ERROR_CORRECT_H if an embedded image is provided"
)
_check_box_size(self.box_size)
if self.data_cache is None:
self.make()
Expand Down
9 changes: 8 additions & 1 deletion qrcode/tests/test_qrcode_pil.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_render_styled_Image():

def test_render_styled_with_embeded_image():
embeded_img = Image.new("RGB", (10, 10), color="red")
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_H)
qr.add_data(UNICODE_TEXT)
img = qr.make_image(image_factory=StyledPilImage, embeded_image=embeded_img)
img.save(io.BytesIO())
Expand Down Expand Up @@ -129,21 +129,28 @@ def test_embedded_image_and_error_correction(tmp_path):
qr.add_data(UNICODE_TEXT)
with pytest.raises(ValueError):
qr.make_image(embeded_image_path=tmpfile)
with pytest.raises(ValueError):
qr.make_image(embeded_image=embedded_img)

qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_M)
qr.add_data(UNICODE_TEXT)
with pytest.raises(ValueError):
qr.make_image(embeded_image_path=tmpfile)
with pytest.raises(ValueError):
qr.make_image(embeded_image=embedded_img)

qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_Q)
qr.add_data(UNICODE_TEXT)
with pytest.raises(ValueError):
qr.make_image(embeded_image_path=tmpfile)
with pytest.raises(ValueError):
qr.make_image(embeded_image=embedded_img)

# The only accepted correction level when an embedded image is provided
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_H)
qr.add_data(UNICODE_TEXT)
qr.make_image(embeded_image_path=tmpfile)
qr.make_image(embeded_image=embedded_img)


def test_shortcut():
Expand Down

0 comments on commit 68f3b29

Please sign in to comment.