0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonで写真にファイル名を記載するコード

Posted at

印刷する際に写真とID(=ファイル名)の紐付けができるように、写真内にファイル名を記載したい。
Adhocにinteractive shellで実行する想定

font_pathはPCによって異なるので各自で確認のこと。

import glob
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

files = glob.glob("./*")

def add_text_to_image(img, text, font_size, font_color):
  position = (50, 50)
  fontpath = "/System/Library/Fonts/Helvetica.ttc"
  font = ImageFont.truetype(fontpath, font_size)
  draw = ImageDraw.Draw(img)
  draw.rectangle((20, 20, 700, 100), fill=(255, 255, 255), outline=(0, 0, 0))
  draw.text(position, text, font_color, font=font)
  return img

quality=90 #deafulat: 75, max: 95
for file in files:
  print(file)
  target = Image.open(file).copy()
  file_name = file.replace('./', '')
  font_size = 30
  font_color = (0, 0, 0)
  img = add_text_to_image(target, file_name, font_size, font_color)
  img.save(file, quality=quality)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?