How can I make labels from filenames in batch?

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?".
Post Reply
Robin55
Posts: 22
Joined: 2018-03-19T15:29:46-07:00
Authentication code: 1152

How can I make labels from filenames in batch?

Post by Robin55 »

Hi All!
Need a bit more help here...
I am using Windows 10 and I am using ImageMagick 7.0.7-31 Q16 x64

I want to make a spine label in batch for many different images using the images file name.

below is the bat file I am currently using to add images to the spine:

Code: Select all

 for %%F in (Spine\*.png) do magick "Front Covers\%%~nF.png" ( "Spine\%%~nF.png" -rotate 90 -resize "44x400>" ) -gravity SouthWest -geometry +26+80 -composite "Covers\%%~nF.png"
I want to keep the size of the label as in my code and keep it in batch as in my code but just use a filename instead of an image on the label. I also want the label to resize the text to fit the label but not break up words, so what I mean is keep all letters in a word together and not split the word on a new line. I haven't decided on a font yet, but any font will do for now.

As always if anyone can help me out here I would be very grateful.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I make labels from filenames in batch?

Post by snibgo »

IM has many ways of rendering text. One is "-size WxH label:blah" which automatically chooses a font size, so you won't need to resize it:

Code: Select all

for %%F in (Spine\*.png) do magick "Front Covers\%%~nF.png" ( -size 400x44 label:"%%~nF" -rotate 90 ) -gravity SouthWest -geometry +26+80 -composite "Covers\%%~nF.png"
snibgo's IM pages: im.snibgo.com
Robin55
Posts: 22
Joined: 2018-03-19T15:29:46-07:00
Authentication code: 1152

Re: How can I make labels from filenames in batch?

Post by Robin55 »

Hi again snibgo!

OK so far so good! but I want just the pictures in "Front Covers" to get a label and then output to "Covers" I don't need anything to do with the spine folder at all.
Also I like that the text is resized but I would like the text to form a second line instead at a certain point and THEN get smaller
As always your help is very appreciated!
Robin55
Posts: 22
Joined: 2018-03-19T15:29:46-07:00
Authentication code: 1152

Re: How can I make labels from filenames in batch?

Post by Robin55 »

ok this is what I have got so far with your help:

for %%F in (Front Covers\*.png) do magick "Front Covers\%%~nF.png" ( -size 250x44 label:"%%~nF" -rotate 90 ) -gravity SouthWest -geometry +26+80 -composite "Covers\%%~nF.png"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How can I make labels from filenames in batch?

Post by fmw42 »

You could use -annotate to add text to an image. You would use it twice in the same command line to create text of different point sizes and/or colors and/or fonts. See http://www.imagemagick.org/Usage/text/ and http://www.imagemagick.org/Usage/fonts/.

If you text has to be rotated to a vertical arrangement, then using label: is probably the best way to go.
Robin55
Posts: 22
Joined: 2018-03-19T15:29:46-07:00
Authentication code: 1152

Re: How can I make labels from filenames in batch?

Post by Robin55 »

Hi fmw42,
This is kind of what I'm looking for:

convert -background lightblue -fill blue -font Candice -pointsize 40 \
-size 320x -gravity Center caption:'ImageMagick Rules OK!' \
caption_centered.gif

But can this be used to add the file name to the image and in batch? I wouldn't know where to begin modifying it.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How can I make labels from filenames in batch?

Post by fmw42 »

Caption: is like label, but it allows multiple line wrapping. But you cannot different sizes for each line. I was suggesting using -annotate. In fact it does allow rotation using the +X+Y arguments. The filename can be extracted from the image automatically since -annotate can use % escapes. For example:

Image

Code: Select all

convert lena.jpg -fill white -pointsize 24 -gravity center -annotate +0+0 "This is my %f" output3.jpg
Image


See
http://www.imagemagick.org/script/escape.php

and other options are -set and -filename at
http://www.imagemagick.org/Usage/basics/#set

-filename is if you want the input name as part of the output name.

I am not sure how you want things name from input to output.

But if you want to keep the same input and output names, you can use mogrify to process a whole folder of files. See http://www.imagemagick.org/Usage/basics/#mogrify
Robin55
Posts: 22
Joined: 2018-03-19T15:29:46-07:00
Authentication code: 1152

Re: How can I make labels from filenames in batch?

Post by Robin55 »

Sorry I'm only getting back now.

ok this is getting a bit more confusing, but this is what I have now:

for %%F in (Front Covers\*.png) do magick convert "Front Covers\%%~nF.png" ( -size 250x44 caption:"%%~nF" ) -font Candice -pointsize 40 -rotate 90 -gravity SouthWest -geometry +26+80 -annotate "Covers\%%~nF.png"

It produces these (does not matter that the script is at the wrong angle for now I'll fix that in Photoshop)

Image

Image

The problem now is the script is not centered and the font command is being ignored and also in the second picture the script has become too small without creating a second line instead.

As always your help is much appreciated!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I make labels from filenames in batch?

Post by snibgo »

Your command renders the text with "caption:", and then specifies which font the rendering should use. You need to specify the font before rendering.

The same problem with "-pointsize".
snibgo's IM pages: im.snibgo.com
Robin55
Posts: 22
Joined: 2018-03-19T15:29:46-07:00
Authentication code: 1152

Re: How can I make labels from filenames in batch?

Post by Robin55 »

I'm having no more success with this at all.
I really need some help here

This is as far as I have got and It just seems to be doing the same thing. I really need to get this done.

for %%F in (Front Covers\*.png) do magick convert "Front Covers\%%~nF.png" -fill white -pointsize 24 -font Ariel -rotate 90 -gravity SouthWest -geometry +26+80 -caption "Covers\%%~nF.png"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How can I make labels from filenames in batch?

Post by fmw42 »

I do not know Windows, but in IM 7, just use magick, not magick convert. As best I can see, all you are doing is creating the text image. You have not composited it over the background image. You are also using -caption. That writes to meta data. You probably want caption: or label:.

Just a guess, but for one image try

Code: Select all

magick input.png ( -background white -pointsize 24 -font Ariel caption:"your caption" -rotate 90 +repage ) -gravity SouthWest -geometry +26+80 -compose over -composite output.png
I will leave the looping and file naming to you or snibgo to add in.

It is best always to start simple for one image and then once that works, you can move on the the looping.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I make labels from filenames in batch?

Post by snibgo »

As Fred says. Walk first (with a single input) before running (in a loop).

Yes, I suggest "magick", never "magick convert" unless you need that for a good (v6) reason.
snibgo's IM pages: im.snibgo.com
Robin55
Posts: 22
Joined: 2018-03-19T15:29:46-07:00
Authentication code: 1152

Re: How can I make labels from filenames in batch?

Post by Robin55 »

OK with everyones help I have got this far:

for %%F in (Front Covers\*.png) do magick convert "Front Covers\%%~nF.png" ( -font bebas-neue -fill red -gravity NorthWest -geometry +25+550 -size 300x30 -rotate 90 label:"%%~nF" ) -composite "Covers\%%~nF.png"
Which gives me this:

Image

The last thing I think I need to know is: is it possible to set the text position start point to the end of the sentence rather than the beginning? I could search forever to find an answer to this. Thanks for the help so far guys.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How can I make labels from filenames in batch?

Post by fmw42 »

Use -gravity southwest rather than -gravity northwest. Your syntax is in the wrong order, though IM 6 is rather forgiving. But IM 7 is not. Also as I said before for IM 7, do not use magick convert. Use magick, only, unless you really want IM 7 to call and use IM 6.

Try

Code: Select all

for %%F in (Front Covers\*.png) do magick "Front Covers\%%~nF.png" ( -background white -size 300x30 -font bebas-neue -fill red  label:"%%~nF" -rotate 90 ) -gravity SouthWest -geometry +X+Y  -composite "Covers\%%~nF.png" 
Robin55
Posts: 22
Joined: 2018-03-19T15:29:46-07:00
Authentication code: 1152

Re: How can I make labels from filenames in batch?

Post by Robin55 »

Finally got it just how I want it now!! This has been a learning experience :) Thanks everyone for helping out, this bit of code is going to come in very handy!!!
Post Reply