Use ImageMagick to Generate Image from Text?
Asked Answered
F

3

7

I'm trying to use ImageMagick to create thumbnail images that are 3840 x 2160.

I need the image to have a black background and white text. The text should be centered vertically and horizontally.

I want to be able to set a font size, but if the text would extend off the image, automatically decrease the font size so it fits with some amount of padding on the left and right.

I will be doing this in bulk with several hundred thousand images. From what I've been able to find, it looks like you always have to set a font size and there's no way to make it dynamic.

Can anyone confirm if this is possible to do?

Feme answered 22/7, 2020 at 20:32 Comment(0)
N
10

You can set a size that specifies how large a space is available for your text and ImageMagick will choose the largest pointsize text that will fit:

magick -gravity center -background black -fill white -size 400x300 -font /System/Library/Fonts/MarkerFelt.ttc    caption:"Short Text" short.png

enter image description here

magick -gravity center -background black -fill white -size 400x300 -font /System/Library/Fonts/MarkerFelt.ttc    caption:"Somewhat longer text that will get rendered in a smaller font" long.png

enter image description here


If you want a margin around the text, you can set a maximum size for the text, then increase the size of the canvas with -extent - I'll do that in red so you can see what the -extent added:

magick -gravity center -background black -fill white -size 400x300 -font /System/Library/Fonts/MarkerFelt.ttc    caption:"Somewhat longer text" -background red -extent 410x400 long.png

enter image description here


If you are reading lines from files to generate your hundreds of thousands of images, you can pipe the text in from another command like this:

echo -n "Text supplied by other command" | magick -gravity center -background black -fill white -size 400x300 -font /System/Library/Fonts/MarkerFelt.ttc caption:@- result.png 

enter image description here


If you want to know what pointsize ImageMagick chose, you can get it like this:

magick identify -format "%[caption:pointsize]\n" result.png
59
Narthex answered 22/7, 2020 at 21:29 Comment(4)
If you are on macOS or Linux, you can do several 100,000 images very fast with GNU Parallel. Ping me if you are interested in that. – Narthex
There's no way to specify a pointsize? – Nina
@Nina You can set a pointsize but this question was specifically about NOT setting a pointsize and instead specifying a canvas size that ImageMagick should fill with the biggest/best font it can fit. – Narthex
I found the "-pointsize" option. πŸ˜„ Thanks. πŸ™ – Nina
M
1

I use the following script to generate text based thumbnails, with all my favorite ttf font files, copied to the same folder before:

ls -1 *.ttf | while read line
do
magick -gravity center -background '#086cdf' -fill '#f1fffe' -size 490x400 -font "$line" caption:"Sample Text" -background red -extent 500x500 "$(echo "$line"|sed 's/.ttf/_icon.png/')"
done

The above script generates icon sized thumbnails with size of 500x500 pixels.

For social media platforms like youtube, thumbnails must be of size 1280x720 for which i use the following script, to generate it:

ls -1 *.ttf | while read line
do
magick -gravity center -background '#086cdf' -fill '#f1fffe' -size 1270x620 -font "$line" caption:"Sample Text" -background red -extent 1280x720 "$(echo "$line"|sed 's/.ttf/_Social_Media_Platforms.png/')"
done

Hope all these scripts help someone googling for a solution.

Matriarch answered 16/5, 2023 at 22:17 Comment(0)
B
1

Perhaps someone will find it useful to run image generation via a Python script.

The easiest way is to use the invoke library to pass the command that you enter in the terminal.

https://www.pyinvoke.org/

from invoke import run

run('convert -gravity center -background black -fill white -size 200x200 caption:"qwerty" -background black -extent 400x400 qwerty.png')

Or use Image from wand.

https://docs.wand-py.org/

from wand.image import Image

with Image(width=200, height=200, pseudo='caption:qwerty') as img:
    img.save(filename='qwerty.png')
Beep answered 22/9 at 15:31 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.