Batch script (windows) for converting, resizing and layer ..

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
x3Ray

Batch script (windows) for converting, resizing and layer ..

Post by x3Ray »

Hello!

I am a newbie to ImageMagick, but did my first steps and I think (well, thought) I could get along with the basic functions of IM, but of course I am stuck now and hope someone can help me.

The situation:
I have several large TIF files (1200 dpi), which I want to
1. resize to 150dpi and 20% of the original size
2. put another image as a watermark layer over that image

First I tried these commands which work pretty fine with just one image:
1. convert -resize 20% -density 150x150 source.tif target.tif
2. composite -compose atop -gravity center -dissolve 25,100 -quality 85 watermark.tif target.tif wm_target.jpg

But when trying to put all commands in one batch file I fail. :(
I started using the mogrify command for the first part, but it is so weird:
mogrify -path mypath -resize 20% -density 150x150 *.tif
in a batch file results in TIF files that are way too small (about 30 k) compared with when directly executing the same (!) command line in the dos box (about 1.000 k). So the command line gives the correct result, but not when executing the batch file with exactly the same command. ??

And for the second part it's getting more complicated; of course I saw the examples with "for %%f in ... do (convert ...)" but I don't get it to work. :(

So any help or hint will be appreciated! Oh, btw I use the command line version fopr windows (version 6.5.8.10, windows xp sp3).

Thanks in advance
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch script (windows) for converting, resizing and layer ..

Post by fmw42 »

post some links to your original images and what you want for the result.

otherwise look at using convert rather than composite. Then you can use parenthesis processing to combine into one command.

see

http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/compose/#watermark
http://www.imagemagick.org/Usage/compos ... d_dissolve

sorry I am a Mac user and know little about DOS scripting, but can help with command line equivalent for one command if you provide the images.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Batch script (windows) for converting, resizing and layer ..

Post by Bonzo »

I would say you want to avoid tempory images in the batch file as that will confuse things.
This command will do everything excelpt dissolve the image as it will not work in convert:

Code: Select all

convert input.tiff -resize 50% -density 150x150 watermark.png -gravity center -composite -compose over -quality 85 output.jpg
Can you make the watermark image opaque ?

This code is similar to one I use and have it saved as resize.bat on my desktop. I just drag images and drop them onto the icon; a resized image is created in the original folder.

Code: Select all

convert.exe %1 -resize 50% -density 150x150 watermark.png -gravity center -composite -compose over -quality 85 "th_%~n1.jpg"
If you wanted to do a whole folder try saving this batch file and run by using batch_file_name path/to/originals path/to/save

Code: Select all

::Turn of displaying the code on the screen
@echo off

:: Read all the jpg images from the directory, resize them, add some text and save as a png in a different directory
for %%f in (%1\*.jpg) do ( convert "%%f" -resize 200x200 -pointsize 18 -fill black ^
-gravity northwest -annotate +0+0 "Some text" "%2\%%~nf.png" )
There was a similar batch file question to yours last week I think. Try looking theough the users and developers section.

I have some example batch files here: http://www.rubblewebs.co.uk/imagemagick/batch.php
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Batch script (windows) for converting, resizing and layer ..

Post by el_supremo »

Code: Select all

mogrify -path mypath -resize 20% -density 150x150 *.tif
If you execute *exactly* this line in a batch file it will not work the same as on the command line.
You have to double the percent sign when it is in a batch file otherwise it will be stripped off and instead of doing "-resize 20%" it will actually do "-resize 20" which will produce an extremely small file.
So try this in a batch file:

Code: Select all

mogrify -path mypath -resize 20%% -density 150x150 *.tif
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
x3Ray

Re: Batch script (windows) for converting, resizing and laye

Post by x3Ray »

Great! :) Thank you all so much for examples and helpful links, I finally got it to work. (Although I am a bit frustrated for forgetting the second % in the batch file command... actually I didn't know that but it's such a silly fault) But I am so relieved now (with 7.500 images to be resized you can imagine noone would voluntarily do that file by file). :)

As I have to convert the original image into two different sizes (both with watermarks) I added some more lines; there is one folder containing the batch script and the watermark image file and some folders containing each some hundred images to be converted. The script places the new images into subfolders with name "high" and "low".
To be more flexible, the batch file has to be called with the folder name as parameter, e. g. "convert-images.bat myfolder1"

The batch script now is (%1 is the folder name as given by param):
==============
echo step 1: create folders if necessary
if not exist %1\high\nul md %1\high
if not exist %1\low\nul md %1\low

echo step 2: convert all TIF images in folder %1 to "high" resolution/size in subfolder high
mogrify -path %1\high -resize 20%% -density 150x150 %1\*.tif

echo step 3: center watermark image file over the TIF file, use 25% opacity and save that file as JPG in subfolder "high"
for %%f in (%1\high\*.tif) do (
composite -compose atop -gravity center -dissolve 25,100 -quality 85 watermark.tif "%%f" "%1\high\%%~nf.jpg"
)

echo step 4: covert images to low resolution/size (half of the resized file in "high") too (using the already resized files with watermark) and save all in subfolder "low"
mogrify -path %1\low -resize 50%% -density 72x72 %1\high\*.jpg

echo step 5: delete TIF files in subfolder "high" as they are not used any more
del /Q %1\high\*.tif
================

Again, thank you for your fast help :)
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Batch script (windows) for converting, resizing and laye

Post by Bonzo »

I wonder how long this will take to do 7,500 tiff files :shock:

A nice batch script but will only work on folders that do not contain a space in their name - I found out !

With convert you can read the image in and save as two different file sizes etc. I wonder if that would be worth looking into as you have so many files. Although it would mean your low image would not have a watermark.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Batch script (windows) for converting, resizing and laye

Post by el_supremo »

Hi Bonzo,
Put double quotes around all filenames, e.g. "%1\high\*.jpg". That should handle the name even if it has spaces in it.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Batch script (windows) for converting, resizing and laye

Post by Bonzo »

Thanks for the tip Pete. You did tell me this before and I did try that but still had problems and am not sure why so in the end I renamed the folder !
Post Reply