Skip to content

Instantly share code, notes, and snippets.

@welliam
Last active August 17, 2023 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save welliam/f1df4594670b4b2165d0661a78b33a24 to your computer and use it in GitHub Desktop.
Save welliam/f1df4594670b4b2165d0661a78b33a24 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
from sys import argv
from PIL import Image, ImageFont, ImageDraw
import exiftool
focus_distance_data = {
("OM Digital Solutions", "OM-5", 'OLYMPUS M.60mm F2.8 Macro'): {
0.185: 304,
0.22: 238,
0.235: 201,
0.255: 171,
0.265: 145,
0.285: 120,
0.3: 97,
0.31: 88,
0.465: 72,
0.535: 65
}
}
def calculate_mm_size(filename):
with exiftool.ExifToolHelper() as et:
[metadata] = et.get_metadata([filename])
make = metadata['EXIF:Make']
model = metadata['EXIF:Model']
lens = metadata['EXIF:LensModel']
focus_distance = metadata['MakerNotes:FocusDistance']
fdd = focus_distance_data[(make, model, lens)]
if focus_distance in fdd:
return fdd[focus_distance]
if focus_distance > max(fdd):
raise ValueError('Focus distance for this photo is out of range', focus_distance)
fdd_items = sorted(fdd.items())
for prev, next in zip(fdd_items, fdd_items[1:]):
x1, y1 = prev
x2, y2 = next
if x1 < focus_distance < x2 :
m = (y2 - y1) / (x2 - x1)
b = y2 - m * x2
return focus_distance * m + b
raise ValueError('Focus distance for this photo is out of range', focus_distance)
filename = argv[1]
source_image = Image.open(argv[1]).convert("RGBA")
if len(argv) == 3:
mm_size = int(argv[2])
else:
mm_size = calculate_mm_size(filename)
if mm_size * 15 < source_image.width:
word = '1cm'
bar_width = mm_size * 10
else:
word = '1mm'
bar_width = mm_size
bar_height = source_image.height / 100
bar_margin = bar_height * 2
draw = ImageDraw.Draw(source_image)
draw.rectangle(
((source_image.width - bar_margin - bar_width,
source_image.height - bar_margin - bar_height),
(source_image.width - bar_margin,
source_image.height - bar_margin)),
fill="white"
)
font = ImageFont.truetype("Arial_Bold.ttf", int(source_image.height / 50))
word_width, word_height = font.getsize(word)
draw.text(
(source_image.width - bar_margin - word_width,
source_image.height - bar_margin - bar_height - word_height - source_image.height / 50 / 3),
word,
(255,255,255),
font=font,
)
exif = source_image.info['exif']
name, ext = filename.split('.', 1)
source_image.convert("RGB").save(f'{name}-scalebar.{ext}', "JPEG", exif=exif)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment