Skip to content

Commit 5fefc64

Browse files
committed
Replace exception with warning for low error correction with embedded image
1 parent 258fb95 commit 5fefc64

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

qrcode/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import sys
4+
import warnings
45
from bisect import bisect_left
56
from typing import Generic, Literal, NamedTuple, Optional, TypeVar, cast, overload
67

@@ -337,8 +338,9 @@ def make_image(self, image_factory=None, **kwargs):
337338
if (
338339
kwargs.get("embedded_image_path") or kwargs.get("embedded_image")
339340
) and self.error_correction != constants.ERROR_CORRECT_H:
340-
raise ValueError(
341-
"Error correction level must be ERROR_CORRECT_H if an embedded image is provided"
341+
warnings.warn(
342+
"Low error correction level with an embedded image might lead to unreadable QR codes. "
343+
"Use ERROR_CORRECT_H for better results.",
342344
)
343345

344346
_check_box_size(self.box_size)

qrcode/tests/test_qrcode_pil.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,30 +121,36 @@ def test_render_styled_with_mask(mask):
121121

122122

123123
def test_embedded_image_and_error_correction(tmp_path):
124-
"If an embedded image is specified, error correction must be the highest so the QR code is readable"
124+
"If an embedded image is specified, error correction should be the highest so the QR code is readable"
125125
tmpfile = str(tmp_path / "test.png")
126126
embedded_img = Image.new("RGB", (10, 10), color="red")
127127
embedded_img.save(tmpfile)
128128

129129
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
130130
qr.add_data(UNICODE_TEXT)
131-
with pytest.raises(ValueError):
131+
132+
message = (
133+
"Low error correction level with an embedded image might lead to unreadable QR codes. "
134+
"Use ERROR_CORRECT_H for better results."
135+
)
136+
137+
with pytest.warns(match=message):
132138
qr.make_image(embedded_image_path=tmpfile)
133-
with pytest.raises(ValueError):
139+
with pytest.warns(match=message):
134140
qr.make_image(embedded_image=embedded_img)
135141

136142
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_M)
137143
qr.add_data(UNICODE_TEXT)
138-
with pytest.raises(ValueError):
144+
with pytest.warns(match=message):
139145
qr.make_image(embedded_image_path=tmpfile)
140-
with pytest.raises(ValueError):
146+
with pytest.warns(match=message):
141147
qr.make_image(embedded_image=embedded_img)
142148

143149
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_Q)
144150
qr.add_data(UNICODE_TEXT)
145-
with pytest.raises(ValueError):
151+
with pytest.warns(match=message):
146152
qr.make_image(embedded_image_path=tmpfile)
147-
with pytest.raises(ValueError):
153+
with pytest.warns(match=message):
148154
qr.make_image(embedded_image=embedded_img)
149155

150156
# The only accepted correction level when an embedded image is provided

0 commit comments

Comments
 (0)