perform imagemagick rounded corners operation on all images in a folder

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
rossdv8
Posts: 47
Joined: 2014-03-12T21:54:20-07:00
Authentication code: 6789

perform imagemagick rounded corners operation on all images in a folder

Post by rossdv8 »

Hi, I've been trying to find a way to do this and have searched here and in google generally.
I've pretty well given up, after trying a heap of loops and other things, but I wondered if someone here might have actually worked it out.

I have the following saved as s shell script and it works perfecly. But I need to run it on perhaps a dozen files each time (more or less). No matter what I do I cannot find anything to add to the script that doesn't result in a mess.

I need obviously, to have each file renamed as it is processed, which will involve variables, but it just ended up compositing all the png files into one image, or creating a whole lot of the same image with different file names.

I've been messing with it for a few hours each day for a week and I'm beginning to think it is impossible using imagemagick, and considering a gimp batch file or something.

I messed with mogrify as well, but couldn't find a way to make it do all the relevant bits, so I went back to convert.

Code: Select all

#!/bin/bash 
 convert *.jpg \
     \( +clone  -alpha extract \
        -draw 'fill black polygon 0,0 0,45 45,0 fill white circle 45,45 45,0' \
        \( +clone -flip \) -compose Multiply -composite \
        \( +clone -flop \) -compose Multiply -composite \
     \) -alpha off -compose CopyOpacity -composite  rounded-corners-45.png 
     
          convert -page +20+20 rounded-corners-45.png -alpha set \
          \( +clone -background black -shadow 60x20+20+20 \) +swap \
          -background none -mosaic     shadow-soft.png
        
        convert shadow-soft.png  -unsharp 10x4+1+0 shadow-soft-unsharp.png
          
          convert shadow-soft-unsharp.png -resize 600 shadow-soft-600.png
          
          mv shadow-soft-600.png output
          rm rounded-corners-45.png
          rm shadow-soft.png
          rm shadow-soft-unsharp.png
          mv *.jpg processed
          cd output
          geeqie shadow-soft-600.png
          
Thanks for looking. I hope someone has a solution. How it should work is the same as most of the other stuff I do. I just drop a heap of files of various sizes into a folder, click the script and go make a coffee.

Cheers,

RossD
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: perform imagemagick rounded corners operation on all images in a folder

Post by snibgo »

I'm confused by your first convert. It reads all the jpg files. It clones all the images, then extracts the alpha. But JPG files can't store alpha, so that image will be white. And so on, saving to a single png filename. But if there were multiple jpegs, there will be multiple pngs, with numbered names.

The following code assumes there is only one png, not numbered.

So I don't know what you intend.

I generally write convert commands using a shell variable for the input and output filenmes. The output filename can contain a directory. Each convert will process a single input file. If I want to process multiple files, I put it in a "for" loop.

In bash, like this:

Code: Select all

#!/bin/bash

for VAR in *.jpg
do
  convert $VAR {IM_stuff_here} output/$VAR
done
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: perform imagemagick rounded corners operation on all images in a folder

Post by GeeMack »

rossdv8 wrote:I hope someone has a solution. How it should work is the same as most of the other stuff I do. I just drop a heap of files of various sizes into a folder, click the script and go make a coffee.
I'm using ImageMagick 6.9.3-9 on Windows 10, and the command below produces the same results as your series of example commands. I started from scratch, so I approached the mask making and compositing quite differently.

This is in Windows syntax. I didn't have a chance to work it up in a *nix shell, so you'd have to translate it by escaping the parentheses with backslashes, changing the double quotes to single quotes, changing the continued line escapes from carets to backslashes, and possibly a couple other tweaks.

Code: Select all

convert *.jpg ^
   -set filename:f "%[t]" ^
   -background none ^
   -gravity northwest ^
   ( ^
      -size 45x45 ^
      xc:black ^
      -draw "fill white circle 45,45 45,0" ^
      -write mpr:corner ^
      -delete 0--1 ^
   ) ^
   ( ^
      -clone 0--1 ^
      -fill white ^
      -colorize 100% ^
      null: ^
      mpr:corner -layers composite -rotate 90 ^
      null: ^
      mpr:corner -layers composite -rotate 90 ^
      null: ^
      mpr:corner -layers composite -rotate 90 ^
      null: ^
      mpr:corner -layers composite -rotate 90 ^
      -write mpr:masks ^
      -delete 0--1 ^
   ) ^
   null: ^
   mpr:masks ^
   -compose copyopacity ^
   -layers composite ^
   -write mpr:images ^
   -shadow 60x20+20+20 ^
   null: ^
   mpr:images ^
   -compose over ^
   -layers composite ^
   -unsharp 10x4+1+0 ^
      "output/%[filename:f].png"
It processes all the JPGs in the directory. It sets the filenames without the extension at the top of the command. Then at the end of the command it adds the "output/" directory to the beginning of the filename and a ".png" extension to the end. That "output" directory probably has to already exist.
rossdv8
Posts: 47
Joined: 2014-03-12T21:54:20-07:00
Authentication code: 6789

Re: perform imagemagick rounded corners operation on all images in a folder

Post by rossdv8 »

Thank you to snibgo and GeeMac.

Since my brain lesions I have had quite a few years of 'losing' stuff I used to be able to do in my sleep. So obviously this is not 'my' code'. I was trying to modify the Thumbnail code from the Imagemagick website to make achieve the same things on jpegs as on gifs.

If you look at the top section it is the rounded corners code for gif thumbnails and the shadow code is from the same place.

So running this bit on a jpg gives me a png with rounded corners and transparency and a drop shadow - Works perfectly and quickly and is exactly what I wanted. But for only one image at a time.

Code: Select all

 convert *.jpg \
     \( +clone  -alpha extract \
        -draw 'fill black polygon 0,0 0,45 45,0 fill white circle 45,45 45,0' \
        \( +clone -flip \) -compose Multiply -composite \
        \( +clone -flop \) -compose Multiply -composite \
     \) -alpha off -compose CopyOpacity -composite  rounded-corners-45.png 
     
          convert -page +20+20 rounded-corners-45.png -alpha set \
          \( +clone -background black -shadow 60x20+20+20 \) +swap \
          -background none -mosaic     shadow-soft.png
Stealing code from the Imagemagick site and modifying it only works up to a point though.
The big dramas came when I tried to use various loops that I have used in the past. Absolutely nothing made sense.

So if snibgo and GeeMac don;t mind me using their suggestions I will see that works.
At the moment I have resorted to Phatch. But almost everything else I do is in Imagemagick with lots of very messy shell scripting.

Resulting in this sort of thing. I think I got advice on bash loops from snibgo and Fred when I was trying to do this a few years ago. But Fred's diagonal collage tilted the horizon, something I wanted to avoid.

http://rossdevitt.blogspot.com.au/2016/ ... lages.html

So thanks for being patient with an old bloke. I'll go through and modify some bits as both of oyu suggested then try to work out where I went wrong.

Thanks GeeMac for the rewrite and snibgo for maybe showing me how to do the loop.
rossdv8
Posts: 47
Joined: 2014-03-12T21:54:20-07:00
Authentication code: 6789

Re: perform imagemagick rounded corners operation on all images in a folder

Post by rossdv8 »

Hmmm I can add yet another loop to the collection of loops that didn;t work on that piece of code. Every time I run it on a single jpeg, I get a perfect result.

I know it is simpler to just use Phatch. But I know from searching on Google that a lot of other people want to do this. And the only thing missing is the ability to run it on multiple jpegs.

I'll have a play with GeeMac's code.
rossdv8
Posts: 47
Joined: 2014-03-12T21:54:20-07:00
Authentication code: 6789

Re: perform imagemagick rounded corners operation on all images in a folder

Post by rossdv8 »

Just an update. None of the suggestions worked for me, so I tore apart the script I used for the diagonal collages.

It is clumsy and not very professional but it works perfectly for me at the moment. Well, almost perfectly. I'm getting the multiple extensions that I used to get on my other stuff and I need to remember how I stopped it. So the final output might be prettyimage,jpg,png.png

Other than that, and the fact that at the moment I have to make an output folder - and I haven;t told the script to clean up after itself. But that's deliberate because I might want images without drop shadows.

I also told it to resize because I want all the images to fit in a particular web page (blogs) and I want all the corners the same radius.

Anyway, if anyone can make it better - go for it. Until then, at least it works. Well for me it does.

Round-with-Shadows

Code: Select all

#!/bin/bash 
    for f in *.jpg
   do convert $f -resize 600 \
     \( +clone  -alpha extract \
        -draw 'fill black polygon 0,0 0,45 45,0 fill white circle 45,45 45,0' \
        \( +clone -flip \) -compose Multiply -composite \
        \( +clone -flop \) -compose Multiply -composite \
     \) -alpha off -compose CopyOpacity -composite  $f.png 
    done
    mkdir output
     for f in *.png
   do convert $f -page +20+20 -alpha set \
          \( +clone -background black -shadow 60x20+20+20 \) +swap \
          -background none -mosaic   output/$f.png
    done
    
Using this I can dump a heap of jpg files into a folder then click the executable once and it just does its thing.

Thanks for setting me on the right track :-)
rossdv8
Posts: 47
Joined: 2014-03-12T21:54:20-07:00
Authentication code: 6789

Re: perform imagemagick rounded corners operation on all images in a folder

Post by rossdv8 »

My final (for now) working version of the Rounded Corners and Drop Shadow script.

Why I wrote it:
I have a few blogs to maintain and people tend to send me FaceBook images that are notmuch more than Thumbnails - sometimes as low as 20KB, usually around 100KB. And they expect them to look great on the blog.
I also take a lot of photos at about 4-5MB size. Often these go on a blog and need to be scaled down.
In both cases running an Unsharp Mask generally makes them bearable at the adjusted size.\
And obviously, I have been applying the round corners and drop shadow in GIMP or batching them in Phatch - neither of which gave me the desired result.

On my Celeron 1.6GHz Gigabyte Brix, 13 jpegs about 4.5MB each were resized to 1024px, unsharped and processed into png files with rounded corners at an average size of 1.5MB each in about 30 seconds with this script.

The size can be changed, the unsharp line can be removed and I also have a copy that applies a gemeric S-Curve to cruddy images.

USAGE (in Linux):

Copy the script into a directory.
Mark the script executable.
Copy some jpg or JPG inages into the same directory.
Click the script.

If the folder is open, and as long as you have 'Show Previews' turned on, you should see a small green 'waiting' sign appear in the folder. If previews are off, it doesn't matter.
After a while the green sign is replaced by a blue Done sign.

Regardless of whether your previews are turned on or off, once the job is complete a large sign should pop up on the desktop when the job is finished.

There's a heap of room for improvement, my BASH skills are pretty primitive and some of the script was simply left over from study tutorials. Some was copied from 'how to' examples on the ImageMagick site. I have several copies set to make different size images.

This is for Linux. Windows or Mac users will need to mess with it.

Code: Select all

#!/bin/bash 
# Ross Devitt - An ImageMagick script to add Rounded Corners
# and Drop Shadows to all jpeg images in a folder
#I'm using ImageMagick 6.7.7-10  on Mint 17.3 KDE
#
# Remove previous done message
 rm 0done.png
# make WORKING message
 convert -background white -fill green -font Arial -pointsize 156 -size 800x800  \
          -gravity center    label:'WORKING\n \nPlease\nWAIT 2-3 mins..' \
          0working.png 
# RENAME any JPG to jpg
      #!/bin/sh
      # lowerit
      # convert all file names in the current directory to lower case
      # only operates on plain files--does not change the name of directories
      # will ask for verification before overwriting an existing file
      for x in `ls`
        do
        if [ ! -f $x ]; then
          continue
          fi
        lc=`echo $x  | tr '[A-Z]' '[a-z]'`
        if [ $lc != $x ]; then
          mv  $x $lc
        fi
        done
 
    mkdir roundcorners
    for f in *.jpg
   do convert $f -resize 1024 \
     \( +clone  -alpha extract \
        -draw 'fill black polygon 0,0 0,45 45,0 fill white circle 45,45 45,0' \
        \( +clone -flip \) -compose Multiply -composite \
        \( +clone -flop \) -compose Multiply -composite \
     \) -alpha off -compose CopyOpacity -composite -unsharp 10x4+1+0 roundcorners/$f.png 
# To add contrast and brightness to dull images insert the following
# between -composite and -unsharp (above)   -composite -sigmoidal-contrast 5,35% 
     done
    cd roundcorners
    mkdir ../corners-shadows
     #Get rid of double extension
  for i in *.png
do
    mv "$i" "`echo $i | sed 's/.jpg//'`"
done
     for f in *.png
   do convert $f -page +20+20 -alpha set \
          \( +clone -background black -shadow 60x20+20+20 \) +swap \
          -background none -mosaic   ../corners-shadows/$f.png
              done
      cd ../corners-shadows
  #Get rid of double extension
  for i in *.png
do
    mv "$i" "`echo $i | sed 's/.png//'`"
done
      cd ..

  #Clean Up and Remove WORKING message
rm 0working.png
# Message when job is done
 convert -background white -fill blue -font Arial -pointsize 88 -size 800x800  \
          -gravity center    label:'DONE!\nJob is in\nRoundCorners\nand\ncorners-shadows\' \
          0done.png
          display 0done.png
# End of Program         
I'm sharing this as a thank you to the people who have helped me with suggestions over a few years and because I love what ImageMagick has allowed me to do with my graphics...

RossD.
Post Reply