aboutsummaryrefslogtreecommitdiff
path: root/make_album_art.py
diff options
context:
space:
mode:
authorMatt Singleton <matt@xcolour.net>2024-02-03 14:02:03 -0600
committerMatt Singleton <matt@xcolour.net>2024-02-03 14:02:03 -0600
commitbb06503bc6e5e27c4bda3bb4359ec82cfb3b5ed5 (patch)
treec771393004197b698c20b3778829054a1e9b3896 /make_album_art.py
initial checkin
Diffstat (limited to 'make_album_art.py')
-rw-r--r--make_album_art.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/make_album_art.py b/make_album_art.py
new file mode 100644
index 0000000..ea821aa
--- /dev/null
+++ b/make_album_art.py
@@ -0,0 +1,58 @@
+import argparse
+
+from PIL import Image, ImageDraw
+
+from epdify import epdify, get_bounded_font
+
+parser = argparse.ArgumentParser()
+parser.add_argument("--font-face", default="fira")
+parser.add_argument("--text-padding", default=20, type=int)
+parser.add_argument("--text-padding-additional-right", default=60, type=int)
+parser.add_argument("--dither-palette", default="perceived")
+parser.add_argument("--final-palette", default="native")
+parser.add_argument("-o", "--output-filename")
+parser.add_argument("artist")
+parser.add_argument("album")
+parser.add_argument("cover_filename")
+args = parser.parse_args()
+
+# open cover image and resize to target size
+# don't crop, assume input image is reasonable close to being square
+cover_img = Image.open(args.cover_filename)
+cover_img = cover_img.resize((480, 480), resample=Image.Resampling.LANCZOS, reducing_gap=3)
+
+# find the optimal size for the album and artist names in the given font face
+text_width = 480 - (args.text_padding * 2) - args.text_padding_additional_right
+album_font = get_bounded_font(args.font_face, "bold", args.album, 60, 35, text_width)
+artist_font = get_bounded_font(args.font_face, "regular", args.artist, min(55, album_font.size - 5), 30, text_width)
+print(f"{album_font.size} {artist_font.size} : {args.artist} - {args.album}")
+
+# draw the album and artist names
+text_img = Image.new("RGB", (480, 480), color="white")
+draw = ImageDraw.Draw(text_img)
+draw.text(
+ (args.text_padding, 320 - (artist_font.size + args.text_padding)),
+ args.album,
+ font=album_font,
+ fill="black",
+ anchor="ld",
+)
+draw.text(
+ (args.text_padding, 320 - (artist_font.size + args.text_padding)),
+ args.artist,
+ font=artist_font,
+ fill="black",
+ anchor="la",
+)
+text_img = text_img.rotate(90)
+
+# compose the final image, dither, and save
+img = Image.new("RGB", (800, 480))
+img.paste(cover_img, (0,0))
+img.paste(text_img, (480, 0))
+img = epdify(img, args.dither_palette, args.final_palette)
+if args.output_filename is None:
+ output_filename=f"{args.artist} - {args.album}.cover.bmp"
+else:
+ output_filename = args.output_filename
+img.save(output_filename, 'bmp')