Last active
October 19, 2022 16:26
-
-
Save ejs94/65a6d663427ba21eaa8b6305d3f51b22 to your computer and use it in GitHub Desktop.
I created this script to use python and the pillow lib to increase the size of apriltags, I believe it's easier to adapt than a postscript. You need to change the data in the variables area for desired family and tag, put this in root of the cloned apriltags-imgs repository.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ======================================= IMPORTS ====================================# | |
''' | |
pip install Pillow | |
pip install numpy | |
''' | |
from PIL import Image, ImageDraw, ImageFont | |
import glob | |
import numpy as np | |
# ======================================= VARIABLES ====================================# | |
# Dots per inch - Printer parameters | |
dpi = 300 | |
# Image size disired in mm | |
resize_mm = 150 # In millimetrs | |
# Need a Apriltag wiht this values in folder | |
tagFamily = 'tagStandard41h12' # Must be same name that folder | |
tagID = '112' | |
# ======================================= CONVERSIONS ====================================# | |
''' | |
The general formula to convert a number of pixels in millimeters: | |
DPI - dots per inch | |
25.4 is simply the amount of millimeters in an inch | |
decimal_mm = (pixels * 25.4) / dpi | |
or | |
pixels = (decimal_mm * dpi) / 25.4 | |
''' | |
# (width, height) - A4 size 297 x 210 mm | |
# Convert paper A4 dimension to based DPI | |
a4_size = (int(np.around((210 * dpi)/25.4)) , int(np.around((297 * dpi)/25.4))) | |
# Desired apriltag size to pixel based on dpi | |
size_to_pixels = int(np.around((resize_mm * dpi)/25.4)) | |
# ======================================= CREATE A4 BACKGROUND ====================================# | |
# A4 size - (width, height) in pixels based on DPI. | |
# Create a black page with A4 dimension | |
a4im = Image.new('RGB', | |
(a4_size[0],a4_size[1]), # A4 at 300dpi | |
(255, 255, 255)) # White | |
print('Creating a empty white A4 page') | |
# ======================================= LOAD AND RESIZE IMAGE ====================================# | |
path = f"{tagFamily}\\*{tagID}.png" | |
# Opens a image in RGB mode | |
for filename in glob.glob(path): | |
im = Image.open(filename) | |
print(f'Founded ID: {tagID} Family: {tagFamily} in folder') | |
# Upscale the apriltag without interpolation | |
im = im.resize((size_to_pixels,size_to_pixels),Image.NEAREST) # Need the Image.NEAREST to remove blur | |
# im.size (width, height) | |
# Background size | |
# a4im.size (width, height) | |
offset_tag = ((a4im.size[0] - im.size[0]) // 2, (a4im.size[1] - im.size[1]) // 2) # To center the tag | |
print(f'Apriltag resized to: {resize_mm} mm') | |
# Fill the empty a4 page with a apriltag | |
a4im.paste(im, offset_tag) # centered by offset | |
#a4im.paste(im, im.getbbox()) # Not centered, top-left corner | |
print(f'Apriltag added to A4 page') | |
# ======================================= LABELING PAGE ====================================# | |
draw = ImageDraw.Draw(a4im) | |
fnt = ImageFont.truetype('arial',100) # FontType and FontSize | |
text = f'ID: {tagID}\nFamily: {tagFamily}\nSize: {resize_mm} mm' | |
offset_text = (im.size[0]/4, ((a4im.size[1] - im.size[1]) / 2) + im.size[1] + 100 ) # Labeling location, need a better aproach | |
draw.text(offset_text, text, (0,0,0),font=fnt) | |
# ======================================= EXPORTING PDF ====================================# | |
outputfile = f'{tagFamily}_{tagID}.pdf' | |
a4im.save(outputfile, 'PDF', quality=100) | |
print(f'PDF sucessful saved -- {outputfile}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment