You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.9 KiB

# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2021, The Wownero Project.
import os
from io import BytesIO
import pyqrcode
from PIL import Image, ImageDraw
import settings
def qr_png(
address: str,
size=settings.PIL_IMAGE_DIMENSIONS,
color_from=(210, 83, 200),
color_to=(255, 169, 62)
) -> BytesIO:
"""Pretty QR c0dez j00"""
created = pyqrcode.create(address, error='L')
buffer = BytesIO()
result = BytesIO()
created.png(buffer, scale=14, quiet_zone=2)
im = Image.open(buffer)
im = im.convert("RGBA")
im.thumbnail(size)
im_data = im.getdata()
# make black color transparent
im_transparent = []
for color_point in im_data:
if sum(color_point[:3]) == 255 * 3:
im_transparent.append(color_point)
else:
# get rid of the subtle grey borders
alpha = 0 if color_from and color_to else 1
im_transparent.append((0, 0, 0, alpha))
continue
if not color_from and not color_to:
im.save(result, format="PNG", quality=settings.PIL_QUALITY, optimize=settings.PIL_OPTIMIZE)
result.seek(0)
return result
# turn QR into a gradient
im.putdata(im_transparent)
gradient = Image.new('RGBA', im.size, color=0)
draw = ImageDraw.Draw(gradient)
for i, color in enumerate(gradient_interpolate(color_from, color_to, im.width * 2)):
draw.line([(i, 0), (0, i)], tuple(color), width=1)
im_gradient = Image.alpha_composite(gradient, im)
im_gradient.save(result, format="PNG", quality=settings.PIL_QUALITY, optimize=settings.PIL_OPTIMIZE)
result.seek(0)
return result
def gradient_interpolate(color_from, color_to, interval):
det_co = [(t - f) / interval for f, t in zip(color_from, color_to)]
for i in range(interval):
yield [round(f + det * i) for f, det in zip(color_from, det_co)]