Skip to content

Allow embedded image location to be erased before placing image #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion qrcode/image/styledpil.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def __init__(self, *args, **kwargs):
)
if not self.embeded_image and embeded_image_path:
self.embeded_image = Image.open(embeded_image_path)
self.embeded_image_overwrite = kwargs.get("embeded_image_overwrite", False)


# the paint_color is the color the module drawer will use to draw upon
# a canvas During the color mask process, pixels that are paint_color
Expand Down Expand Up @@ -87,7 +89,14 @@ def draw_embeded_image(self):
return
total_width, _ = self._img.size
total_width = int(total_width)
logo_width_ish = int(total_width / 4)

logo_divider = 4

if self.embeded_image_overwrite:
# if we're doing this destructive thing to the QR code, better to make it smaller to increase chances of not breaking it
logo_divider = 8

logo_width_ish = int(total_width / logo_divider)
logo_offset = (
int((int(total_width / 2) - int(logo_width_ish / 2)) / self.box_size)
* self.box_size
Expand All @@ -97,8 +106,18 @@ def draw_embeded_image(self):
region = self.embeded_image
region = region.resize((logo_width, logo_width), self.embeded_image_resample)
if "A" in region.getbands():
if self.embeded_image_overwrite:
# "erase" the logo region using background color and apply the embedded image on top
embed_backing_region = Image.new(region.mode, region.size, self.color_mask.back_color)
self._img.alpha_composite(embed_backing_region, logo_position)

self._img.alpha_composite(region, logo_position)
else:
if self.embeded_image_overwrite:
# "erase" the logo region using background color and apply the embedded image on top
embed_backing_region = Image.new(region.mode, region.size, self.color_mask.back_color)
self._img.paste(embed_backing_region, logo_position)

self._img.paste(region, logo_position)

def save(self, stream, format=None, **kwargs):
Expand Down