Skip to content
Snippets Groups Projects
Commit 8182660b authored by Mayr, Hannes's avatar Mayr, Hannes
Browse files

Display IDs with linebreaks correctly.

parent d8fc9c50
Branches
Tags
3 merge requests!65Release v0.3.0,!64New year developments,!63Improve handling of long IDs
......@@ -103,7 +103,7 @@ def tagplot(
position = (0.35, 0.975)
case "east":
rotation = 90
position = (0.975, 0.35)
position = (0.95, 0.35)
case "south":
rotation = 0
position = (0.35, 0.015)
......
# -*- coding: utf-8 -*-
"""
Tag your picture with an ID.
Functions:
tagplot_image(PlotOptions instance) -> PlotIDTransfer instance
"""
"""Tag your picture with an ID."""
import os
import warnings
from PIL import Image, ImageDraw, ImageFont, ImageOps
from PIL import Image, ImageDraw, ImageFont
from plotid.create_id import create_id, create_qrcode
from plotid.plotoptions import PlotOptions, PlotIDTransfer
......@@ -63,15 +58,24 @@ def tagplot_image(plotid_object: PlotOptions) -> PlotIDTransfer:
img = Image.open(img)
if plotid_object.id_on_plot:
img_txt = Image.new("L", font.getsize(img_id))
# Create temporary PIL image to get correct textsize
tmp_img = Image.new("L", (100, 100))
tmp_draw = ImageDraw.Draw(tmp_img)
textsize = tmp_draw.textsize(img_id, font)
# Create new image with white background and the size of the textbox
img_txt = Image.new("RGBA", textsize, color=(255, 255, 255, 0))
draw_txt = ImageDraw.Draw(img_txt)
draw_txt.text((0, 0), img_id, font=font, fill=255)
draw_txt.text((0, 0), img_id, font=font, fill=color)
# Rotate the image by the given angle
txt = img_txt.rotate(plotid_object.rotation, expand=1)
# Paste the txt/ID image with transparent background onto the original image
img.paste(
ImageOps.colorize(txt, (0, 0, 0), color),
txt,
(
int(img.width * plotid_object.position[0]),
int(img.height * (1 - plotid_object.position[1])),
int(img.height * (1 - plotid_object.position[1]) - txt.height),
),
txt,
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment