How to create a batch script for resize & darken?

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
DGF
Posts: 8
Joined: 2012-02-23T18:59:35-07:00
Authentication code: 8675308

How to create a batch script for resize & darken?

Post by DGF »

I was directed here from the gimpforums.com. I typically use GIMP to edit pictures. Now I am wondering if I can create a script (which I think is what I need) to perform a meticulous task on 40 separate photographs.

The web site I'm working on is http://scenicdesktops.com/

As you can see, the individual images have all been re-sized into 3 different versions, a thumbnail, a darkened thumbnail (seen when hovering the curser over) and the actual lager (but still shrunken) downloadable wallpaper image.

It's time to update the entire site. I have a whole lot of new photos, and have edited (and thus need to re-load) all the earlier ones.

Last time, I used GIMP to resize every single photo 2 separate times. Then, had I to create a black overlay and set the opacity for 20%, to create the darkened thumbnail. Every. Time. This time around, I would like a way to just batch it all into one process, so I can generate all of these results from every one of the 40 photos I want to use.

I've never made a script in my life. Nor am I a command line guru. Can anyone help me figure out what to do?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to create a batch script for resize & darken?

Post by fmw42 »

Do you have IM installed? What version and what platform? If Linux or Mac OSX, then this works for any one image. But a loop can be written to process all images from some directory. If on windows, the syntax would be slightly different, but the script would have to change a lot and I am not a Windows user.

Where do you put all the images? All images in the same directory? Are you starting with the larger jpg that you have already or from some other image and/or size. If the start image is not your current large image, then what are you using, since going from JPG to JPG will give some loss of quality, though for thumbnails it probably does not matter.

So starting with your current large image, the following seems to work.

http://www.fmwconcepts.com/misc_tests/r ... owLake.jpg

infile="BowLake.jpg"
inname=`convert $infile -format "%t" info:`
convert $infile -thumbnail 143x107! -write ${inname}1.jpg \
-fill black -colorize 20% ${inname}2.jpg

Image

Image


It creates two images: BowLake1.jpg and BowLake2.jpg. The first is just resized to 143x107 and the second is a copy that is darkened by 20% with black. If you need to create the larger image from some other image, then the above can easily be modified to create 3 output images.

If you can clarify how you want the script to work --- what start image and where they are found and written, then I can probably write a simple loop for you.
DGF
Posts: 8
Joined: 2012-02-23T18:59:35-07:00
Authentication code: 8675308

Re: How to create a batch script for resize & darken?

Post by DGF »

I'm using Linux Mint 10, and IM appears to already have been installed. The version comes up as 7:6.6.2.6-1ubuntu1.1

Right now I have all 40 images on my desktop, with nothing else. They are 10 megabit files each, and the ones I start with, and resize each file from.

One more thing I need to mention. I plan on putting a small, semi transparent text overlay across the 1920 X 1440 wallpapers. This is a custom process for each photo, as the text cannot go to the same place in every instance, so it won't be automated. That's ok, I will do that part myself.

That being the case, perhaps I should have the automated process scale it down and save it as a 1920 X 1440 PNG (or any other lossless format), which I can then apply the text to and save again as a jpeg (as opposed to scaling it down, saving as a jpeg, and saving it again as a jpeg after editing).

So here's the logic tree (I think)

Original 3648 × 2736 JPG -> 1920 x 1440 PNG
Original 3648 × 2736 JPG -> 143 x 107 JPG
Original 3648 × 2736 JPG -> 143 x 107 JPG w/ 20% darkening applied

In the case of the thumbnails, 95% or so quality preservation is probably fine. The process can be applied to whatever is on my desktop. For simplicity's sake I'll say the destination folder is in ~/sites. For naming, just preserve the name for the bigger picture, while adding a 1 to the end of 1st thumbnail's name and 2 to the darkened thumbnail's.

I hope I laid that out OK. Thanks for any help, it's greatly appreciated.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to create a batch script for resize & darken?

Post by fmw42 »

I need more information. What are the really large filenames called. To automate this, one has to assume that the output names are derived from the input names without its suffix. Thus if the original large file is BowLake.jpg and you want to scale that down to 3648 × 2736, then I am not sure what to call the 1920 X 1440 size version. For example


infile="BowLake.jpg"
inname=`convert $infile -format "%t" info:`
convert $infile -resize 1920x1440! -write ${inname}0.jpg \
-thumbnail 143x107! -write ${inname}1.jpg \
-fill black -colorize 20% ${inname}2.jpg

So the above will have:
1920x1440 as the BowLake0.jpg
143x107 as BowLake1.jpg
143x107 20% darkened as BowLake2.jpg

To remove the 0 from Bowlake0.jpg, one would either have to have some consistent input files such as BowLake_orig.jpg or something that is always the same for each image that can be stripped off or the output images need to be written to a different directory.

The latter would be the:


infile="BowLake.jpg"
inname=`convert $infile -format "%t" info:`
convert $infile -resize 1920x1440! -write /path2/outdir/${inname}.jpg \
-thumbnail 143x107! -write /path2/outdir/${inname}1.jpg \
-fill black -colorize 20% /path2/outdir/${inname}2.jpg

That way the very large input image name could be used for the 1920x1440 images as well.

So I either need to know your naming convention on your very large images or you have to write all the output files to a new directory.
DGF
Posts: 8
Joined: 2012-02-23T18:59:35-07:00
Authentication code: 8675308

Re: How to create a batch script for resize & darken?

Post by DGF »

Just name the 1920 x 1440 ones the same as the source JPG, the PNG extension should differentiate them, correct?

I've already renamed the bigger originals (to bowlake.jpg, etc). The resized 1920 x 1440 wallpapers should just be the same name with the PNG extension (unless you think there is a better lossless way to go).

EDIT, just to be thorough:
original.jpg -> original.png (1920 x1440)
original.jpg -> original1.jpg (143 x 107 thumbnail)
original.jpg -> original2.jpg (143 x 107 darkened thumbnail)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to create a batch script for resize & darken?

Post by fmw42 »

DGF wrote:Just name the 1920 x 1440 ones the same as the source JPG, the PNG extension should differentiate them, correct?

I've already renamed the bigger originals (to bowlake.jpg, etc). The resized 1920 x 1440 wallpapers should just be the same name with the PNG extension (unless you think there is a better lossless way to go).

EDIT, just to be thorough:
original.jpg -> original.png (1920 x1440)
original.jpg -> original1.jpg (143 x 107 thumbnail)
original.jpg -> original2.jpg (143 x 107 darkened thumbnail)
So you want to make your 1920x1440 a png, which will have a larger file size than jpg? Is that correct?

As I said, the other way is to write the outputs to a different directory from the inputs. That way all your files could be jpg if that is what you desire.

Please make sure I know what you want to do. Also give me your input and desired output directories

Are all the images in your input directory jpg or do you allow for other input formats. I can accept all formats or filter for only jpgs. Are there any other files such as text files or non-image files in that directory. If so, then I have to filter on only the image formats you want for input files.

P.S. Do you have any files with spaces in the names?
DGF
Posts: 8
Joined: 2012-02-23T18:59:35-07:00
Authentication code: 8675308

Re: How to create a batch script for resize & darken?

Post by DGF »

One more thing I need to mention. I plan on putting a small, semi transparent text overlay across the 1920 X 1440 wallpapers. This is a custom process for each photo, as the text cannot go to the same place in every instance, so it won't be automated. That's ok, I will do that part myself.

That being the case, perhaps I should have the automated process scale it down and save it as a 1920 X 1440 PNG (or any other lossless format), which I can then apply the text to and save again as a jpeg (as opposed to scaling it down, saving as a jpeg, and saving it again as a jpeg after editing).
PNG so I can do the above mentioned process before losing any quality. They'll end up as jpeg, but I have to perform that process for each manually before I do that.

All the input images will be jpg. They are actually the only files on my desktop. Doesn't matter much where they get outputed to. If its easier, just output them into the same directory as the source files. The actual input directory from the command line is: /media/Shared/Desktop.

PS. No spaces in any names. Thank goodness :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to create a batch script for resize & darken?

Post by fmw42 »

Any images with spaces in the names?

For safety of your input image, perhaps I should put all the output images in their own subdirectory. Is that OK?
DGF
Posts: 8
Joined: 2012-02-23T18:59:35-07:00
Authentication code: 8675308

Re: How to create a batch script for resize & darken?

Post by DGF »

Nope, no spaces in any names. And sure, a sub-directory is fine. Thanks again.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to create a batch script for resize & darken?

Post by fmw42 »

Here is the basic script, but untested.

Define inputdir where all your input files exist.
Define a new outputdir.
Test if inputdir exists
Create outputdir
Get list of images from inputdir
Loop over each input image and create 3 output images in the outputdir.

(There may be more efficient ways to do this, but I think it should work)

Code: Select all

inputdir="/media/Shared/Desktop"
outputdir="${inputdir}/processed"
[ -d $inputdir ] || { 
echo  "ERROR: --- Directory $inputdir DOES NOT EXIST ---"
exit 1 
}
[ -d $outputdir ] || mkdir $outputdir
imgArr=(`ls "$inputdir"`)
for infile in ${imgArr[*]}; do
inname=`convert $infile -format "%t" info:`
convert $infile -resize 1920x1440! -write $outputdir/${inname}.png \
-thumbnail 143x107! -write $outputdir/${inname}1.jpg \
-fill black -colorize 20% $outputdir/${inname}2.jpg
done
You can edit the inputdir and outputdir as desired. Then cut and paste into your bash terminal. The outputdir will be created if it does not exist.

HOWEVER, I would suggest you test just one image first to be sure it works before looping over all images. So try

Code: Select all

inputdir="/media/Shared/Desktop"
outputdir="${inputdir}/processed"
[ -d $inputdir ] || { 
echo  "ERROR: --- Directory $inputdir DOES NOT EXIST ---"
exit 1 
}
[ -d $outputdir ] || mkdir $outputdir
imgArr=(`ls "$inputdir"`)
infile="SOMEIMAGE.jpg"
inname=`convert $infile -format "%t" info:`
convert $infile -resize 1920x1440! -write $outputdir/${inname}.png \
-thumbnail 143x107! -write $outputdir/${inname}1.jpg \
-fill black -colorize 20% $outputdir/${inname}2.jpg
Just replace SOMEIMAGE.jpg with the name of an actual image in the inputdir. See if you get the 3 images created in the outputdir and examine them for what you want for the quality and darkness of the last image. If more quality is needed at the expense of the filesizes, then we can add -quality XX to the command to compress the jpgs more or less as desired. see http://www.imagemagick.org/script/comma ... hp#quality

Let me know how the one image tests goes before you do the other. We may want to create the imgArr, but edit it so that you try only 2 or 3 images at a time next.

As you can see, I always take small steps to test my concepts before running the whole thing.
Last edited by fmw42 on 2012-02-24T17:59:31-07:00, edited 1 time in total.
DGF
Posts: 8
Joined: 2012-02-23T18:59:35-07:00
Authentication code: 8675308

Re: How to create a batch script for resize & darken?

Post by DGF »

Cool. I will test this out in a few hours when I can get back. I'll let you know.
DGF
Posts: 8
Joined: 2012-02-23T18:59:35-07:00
Authentication code: 8675308

Re: How to create a batch script for resize & darken?

Post by DGF »

I tried doing the one image script, with Flattop.jpg replacing the SOMEIMAGE.jpg. No images were generatied; this is what I got:
dan@Linuxmint10 ~ $ inputdir="/media/Shared/Desktop"
dan@Linuxmint10 ~ $ outputdir="${inputdir}/processed"
dan@Linuxmint10 ~ $ [ -d $inputdir ] || {
> echo  "ERROR: --- Directory $inputdir DOES NOT EXIST ---"
> exit 1
> }
dan@Linuxmint10 ~ $ [ -d $outputdir ] || mkdir $outputdir
dan@Linuxmint10 ~ $ imgArr=(`ls "$inputdir"`)
dan@Linuxmint10 ~ $ infile="Flattop.jpg"
dan@Linuxmint10 ~ $ inname=`convert $infile -format "%t" info:`
convert: unable to open image `Flattop.jpg': @ error/blob.c/OpenBlob/2498.
convert: missing an image filename `info:' @ error/convert.c/ConvertImageCommand/2970.
dan@Linuxmint10 ~ $ convert $infile -resize 1920x1440! -write $outputdir/${inname}.png \
> -thumbnail 143x107! -write $outputdir/${inname}1.jpg \
> -fill black -colorize 20% $outputdir/${inname}2.jpg
convert: unable to open image `Flattop.jpg': @ error/blob.c/OpenBlob/2498.
convert: missing an image filename `/media/Shared/Desktop/processed/2.jpg' @ error/convert.c/ConvertImageCommand/2970.
dan@Linuxmint10 ~ $
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to create a batch script for resize & darken?

Post by fmw42 »

dan@Linuxmint10 ~ $ infile="Flattop.jpg"
dan@Linuxmint10 ~ $ inname=`convert $infile -format "%t" info:`
What do you get from

inputdir="/media/Shared/Desktop"
cd $inputdir
convert Flattop.jpg Flattop2.jpg

Looks like I may have left the cd out of my script above, though I thought at one time it was there. Sorry my fault.

If the above works, then try

inputdir="/media/Shared/Desktop"
outputdir="${inputdir}/processed"
[ -d $inputdir ] || {
echo "ERROR: --- Directory $inputdir DOES NOT EXIST ---"
exit 1
}
[ -d $outputdir ] || mkdir $outputdir
cd $inputdir
infile="Someimage"

inname=`convert $infile -format "%t" info:`
convert $infile -resize 1920x1440! -write $outputdir/${inname}.png \
-thumbnail 143x107! -write $outputdir/${inname}1.jpg \
-fill black -colorize 20% $outputdir/${inname}2.jpg


Then eventually,

inputdir="/media/Shared/Desktop"
outputdir="${inputdir}/processed"
[ -d $inputdir ] || {
echo "ERROR: --- Directory $inputdir DOES NOT EXIST ---"
exit 1
}
[ -d $outputdir ] || mkdir $outputdir
cd $inputdir
imgArr=(`ls "$inputdir"`)
for infile in ${imgArr[*]}; do
inname=`convert $infile -format "%t" info:`
convert $infile -resize 1920x1440! -write $outputdir/${inname}.png \
-thumbnail 143x107! -write $outputdir/${inname}1.jpg \
-fill black -colorize 20% $outputdir/${inname}2.jpg
done
DGF
Posts: 8
Joined: 2012-02-23T18:59:35-07:00
Authentication code: 8675308

Re: How to create a batch script for resize & darken?

Post by DGF »

All 3 processes did exactly what they needed to do. Worked like a charm! I now have 120 files from the 40 originals.

I really appreciate the help. Cup of coffee? :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to create a batch script for resize & darken?

Post by fmw42 »

Glad it worked out well for you.

Fred
Post Reply