2
0
mirror of https://github.com/xcat2/confluent.git synced 2026-01-11 02:22:31 +00:00

Preserve aspect ratio if Pillow available

Since we are controlling the scaling, we can manage
things more precisely and get the aspect ratio right.
This commit is contained in:
Jarrod Johnson
2025-05-16 09:44:57 -04:00
parent ee53ee47c1
commit 11939c4d57

View File

@@ -257,10 +257,21 @@ def draw_image(data, width, height, doscale=True):
errstr = 'Error rendering image:\n' + str(e)
return draw_text(errstr, width, height)
imgwidth, imgheight = get_pix_dimensions(width, height)
img = img.resize((imgwidth - 4, imgheight - 4))
nimg = Image.new(mode='RGB', size=(img.width + 4, img.height + 4), color='black')
nimg = Image.new(mode='RGBA', size=(imgwidth, imgheight))
imgwidth -= 4
imgheight -= 4
hscalefact = imgwidth / img.width
vscalefact = imgheight / img.height
if hscalefact < vscalefact:
rzwidth = imgwidth
rzheight = int(img.height * hscalefact)
else:
rzwidth = int(img.width * vscalefact)
rzheight = imgheight
img = img.resize((rzwidth, rzheight))
nd = ImageDraw.Draw(nimg)
nd.rectangle((0, 0, nimg.width - 1, nimg.height -1), outline='white', width=1)
nd.rectangle((1, 1, rzwidth + 2, rzheight + 2), outline='black', width=1)
nd.rectangle((0, 0, rzwidth + 3, rzheight + 3), outline='white', width=1)
nimg.paste(img, box=(2, 2))
outfile = io.BytesIO()
nimg.save(outfile, format='PNG')