ImageMagick

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".

I’m interested in making a series of 800x800px images that read captions from txt files to form new .png images.

Each txt file is similar to the following and is stored in the directory: authors/text/ For example, mark_twain.txt looks like:

Mark Twain
The Adventures of Tom Sawyer
1876
Huckleberry Finn
USA

The background is: background_800x800.png

After creating the first image, I am then adding a Category with a gravity of Northwest (that is positioned above and to the Northwest corner) from the mark_twain.txt file information, which is centered in the image.

How do I change this to batch caption for 50 files? (Let’s assume that all are Fiction)

Is there a best practice for what I’m trying to accomplish that I am missing? I’m new to this and I don’t understand how to apply this to create images from multiple .txt files. Is there a variable for the input filenames and output files?

I’ve been looking at examples of other scripts, but haven’t quite figured it out.

You will have to write a bash script "for" loop over each input text file using your command if you know the placements you want. Those can be set in another text file. Alternately, you can loop over each input text file, create the caption image and then -append (or -smush) them vertically to form one file. The -smush command allows you to put spacing between the created files.

Post 3 sets of text or the text files (to some free hosting service and put the urls here) and I will try to show you how to loop and use -smush.

But how will 50 of these fit into one 800x800 px image? Do you know what dimensions you want to use for the captions and the spacing between text sections?

Thanks for your quick reply. I'm interested in creating 50 images in total, one image for each author. So there will be a Mark Twain image, a J.D. Salinger image etc. Each image will be 800x800. Sorry for the confusion!

Here I have duplicated your mark_twain text 3 times as files. And created a one line text file for your categories_fiction file. I then create an 800x800 white background image. You can replace that with any other file you want of the same size. I have concatenated your two commands into one command line to avoid writing an intermediate file. I made a list of the text files to process. It could be replaced with a file containing a list of files or potentially using a wild card, if the file names are consistent.

Try this with just one or two or three files first to see if it works.

Thanks for taking the time to help me with this, it’s much appreciated!

While trying to run the loop, I’ve been getting a syntax error with the $list in line 4.

File "<ipython-input-50-23f9ae78a950>", line 4
for file in $list; do
^
SyntaxError: invalid syntax

That could be from some beginner mistake on my part. I’m currently trying to determine why I’m getting that error in executing the loop. I don’t believe it’s related to policy.xml, as I’ve been able to read @files.txt and use them to create sample images.

Thanks! The script works well in bash shell, the two things I still have a question about are:

What should I change the ‘caption’ command to in order to read text from all three files? Currently I believe it’s only set to only read a single file:

caption:"@mark_twain1.txt" \

I’ve tried caption:”@mark_twain.txt @shelley.txt @sallinger.txt” but that didn’t work for me.

How do I pluralize this to read captions from the three files? @mark_twain.txt @shelley.txt @salinger.txt

One idea I had, and it might be totally wrong... the code below got me the desired output in Bash, but I don’t know how to direct this info as an input to the ‘caption’ command to create three separate images.

myvar=$( cd ./texts | cat mark_twain.txt salinger.txt shelley.txt )

I’ve been playing around with hundreds of different combinations today, but I still haven’t been able to solve it yet.

You also mentioned that I could create a file that specifies the .txt files to be read by the caption command? How does that work?

Caption: can only take 1 file at a time. But you can put 3 captions with corresponding composites in the same command with different placements. Or you can concatenate the 3 files with some Unix tool. But I thought you said before that you only want one file per image for 50 files. The script I provided allows you to created 3 or more files from the list of files.

If you want to read the list from a file, then look up "while read" unix command. It reads a line of text from a file. The file is just a simple text file with the names of your files listed one row at a time. In this case it is just: