How to batch composite / round corners?

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?".
imagefox87
Posts: 20
Joined: 2016-08-26T13:37:18-07:00
Authentication code: 1151

Re: How to batch composite?

Post by imagefox87 » 2016-08-26T17:41:22-07:00

Also do you guys know how to use -chop in order to cut just 12x12 from all 4 corners?

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

Re: How to batch composite?

Post by fmw42 » 2016-08-26T17:49:18-07:00

As far as I know, -chop will only chop full rows or columns. You will have to use -crop with -gravity 4 times to get all 4 corners.

imagefox87
Posts: 20
Joined: 2016-08-26T13:37:18-07:00
Authentication code: 1151

Re: How to batch composite?

Post by imagefox87 » 2016-08-26T17:52:52-07:00

fmw42 wrote:As far as I know, -chop will only chop full rows or columns. You will have to use -crop with -gravity 4 times to get all 4 corners.
How about -region geometry? Because the northeast,southeast, northwest, and southwest are not specific enough for gravity.

User avatar
GeeMack
Posts: 722
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How to batch composite?

Post by GeeMack » 2016-08-26T18:37:29-07:00

imagefox87 wrote:Also do you guys know how to use -chop in order to cut just 12x12 from all 4 corners?
We came up with one like this last week to answer a different question. I modified it a bit to chop a 12x12 square from every corner of every PNG in the directory and save them all to another directory with their original flienames...

Code: Select all

convert *.png ^
   -set filename:f "%[t]" ^
   -background none ^
   -gravity northwest ^
   ( ^
      -size 12x12 ^
      xc:black ^
      -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 ^
      "output/%[filename:f].png"
Change this part of the above command...

Code: Select all

   ( ^
      -size 12x12 ^
      xc:black ^
      -write mpr:corner ^
      -delete 0--1 ^
   ) ^
... to this...

Code: Select all

   ( ^
      -size 12x12 ^
      xc:black ^
      -draw "fill white circle 12,12 12,0" ^
      -write mpr:corner ^
      -delete 0--1 ^
   ) ^
... and it will actually make 12x12 rounded transparent corners on all the PNGs in the directory.

Make sure the directory named "output" already exists. To run this from a Windows BAT script you'd have to change all the single percent signs "%" to doubles "%%".

imagefox87
Posts: 20
Joined: 2016-08-26T13:37:18-07:00
Authentication code: 1151

Re: How to batch composite?

Post by imagefox87 » 2016-08-26T18:56:02-07:00

@GeeMack: Thank you so much man! You're a life saver. Glad to know that people provide code here for free. :)

User avatar
GeeMack
Posts: 722
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How to batch composite?

Post by GeeMack » 2016-08-26T18:58:28-07:00

imagefox87 wrote:@GeeMack: Thank you so much man! You're a life saver. Glad to know that people provide code here for free. :)
If you ever show up in the area of Peoria, Illinois, you're buying dinner. ;)

imagefox87
Posts: 20
Joined: 2016-08-26T13:37:18-07:00
Authentication code: 1151

Re: How to batch composite?

Post by imagefox87 » 2016-08-26T19:10:25-07:00

GeeMack wrote: If you ever show up in the area of Peoria, Illinois, you're buying dinner. ;)
Sure, np man haha. I was going to say a beer, but dinner is cool with me too. Btw, if you ever play the card game, Yu-Gi-Oh, let me know. I can give you a free version of a MSE master template that is currently the best highest quality template available on the net.

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

Re: How to batch composite?

Post by fmw42 » 2016-08-26T19:31:53-07:00

My mistake above about using -crop. It should have been -composite. Here is my approach (unix syntax) for one image. You make a white background the size of the image and composite as 12x12 black image at each corner, then put the result into the alpha channel of the image.

Input:
Image

Code: Select all

convert lena.jpg \
\( -size 12x12 xc:black -write mpr:corner +delete \) \
\( -clone 0 -fill white -colorize 100% \
	mpr:corner -gravity northwest -compose over -composite \
	mpr:corner -gravity northeast -compose over -composite \
	mpr:corner -gravity southeast -compose over -composite \
	mpr:corner -gravity southwest -compose over -composite \) \
-alpha off -compose copy_opacity -composite lena_corners12.png
Image

Other shapes could be created using -draw to replace the simple black square.

Code: Select all

convert lena.jpg \
\( -size 25x25 xc:black -fill white \
	-draw "circle 25,25 25,0" -alpha off \
	-write mpr:corner +delete \) \
\( -clone 0 -fill white -colorize 100% \
	\( mpr:corner \) -gravity northwest -compose over -composite \
	\( mpr:corner -rotate 90 \) -gravity northeast -compose over -composite \
	\( mpr:corner -rotate 180 \) -gravity southeast -compose over -composite \
	\( mpr:corner -rotate 270 \) -gravity southwest -compose over -composite \) \
-alpha off -compose copy_opacity -composite lena_corners25c.png
Image

And add a shadow:

Code: Select all

convert lena.jpg \
\( -size 25x25 xc:black -fill white \
	-draw "circle 25,25 25,0" -alpha off \
	-write mpr:corner +delete \) \
\( -clone 0 -fill white -colorize 100% \
	\( mpr:corner \) -gravity northwest -compose over -composite \
	\( mpr:corner -rotate 90 \) -gravity northeast -compose over -composite \
	\( mpr:corner -rotate 180 \) -gravity southeast -compose over -composite \
	\( mpr:corner -rotate 270 \) -gravity southwest -compose over -composite \) \
-alpha off -compose copy_opacity -composite -compose over \
\( +clone -background black -shadow 80x3+5+5 \) \
+swap -background none -layers merge +repage lena_corners25cs.png
Image

Ref: http://www.fmwconcepts.com/imagemagick/ ... und_shadow

imagefox87
Posts: 20
Joined: 2016-08-26T13:37:18-07:00
Authentication code: 1151

Re: How to batch composite?

Post by imagefox87 » 2016-08-26T21:20:44-07:00

@fmw42: This is also a great method thank you! I'm assuming that the radius of the circle you draw controls the curvature of the rounded corners?

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

Re: How to batch composite?

Post by fmw42 » 2016-08-26T21:30:58-07:00

imagefox87 wrote:@fmw42: This is also a great method thank you! I'm assuming that the radius of the circle you draw controls the curvature of the rounded corners?
Yes, but you generally want to draw a circle and the radius of the circle should match the square black background size. So you want to draw the center of the circle in the bottom right corner for the orientation I used.

See my addition of a shadow in the post above that has been edited to include that.

P.S. It is just a variation on what GeeMack did. There are lots of ways to do things in IM.

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

Re: How to batch composite?

Post by fmw42 » 2016-08-26T22:44:15-07:00

For windows users, for a single image, this would be my rounded corners command. (Windows users, please correct me if this is wrong).

Remove \ from ( and from ) and change line ending \ to ^. For bat syntax double % to %%

Note the output directory must exist already

Code: Select all

convert path2inputdir/lena.jpg ^
-set filename:f "%[t]" ^
( -size 25x25 xc:black -fill white ^
   -draw "circle 25,25 25,0" -alpha off ^
   -write mpr:corner +delete ) ^
( -clone 0 -fill white -colorize 100% ^
   ( mpr:corner ) -gravity northwest -compose over -composite ^
   ( mpr:corner -rotate 90 ) -gravity northeast -compose over -composite ^
   ( mpr:corner -rotate 180 ) -gravity southeast -compose over -composite ^
   ( mpr:corner -rotate 270 ) -gravity southwest -compose over -composite ) ^
-alpha off -compose copy_opacity -composite -compose over ^
( +clone -background black -shadow 80x3+5+5 ) ^
+swap -background none -layers merge +repage  "path2outputdir/%[filename:f].png"
You probably need to follow Geemack's approach to be able to handle wild card inputs for multiply input and output files.

But you could write a loop over all your images and process them one at a time in the loop with the above command.

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

Re: How to batch composite?

Post by fmw42 » 2016-08-27T10:41:22-07:00

If all your input images are the same size, then you can do wild cards with my command as follows (similar to what GeeMack does using null: and layers composite). see http://www.imagemagick.org/Usage/anim_mods/#composite

Windows format:

Code: Select all

convert path2inputdir/* -set filename:f "%[t]" ^
null: ^
( -size 25x25 xc:black -fill white ^
	-draw "circle 25,25 25,0" -alpha off ^
	-write mpr:corner +delete ) ^
( -clone 0 -fill white -colorize 100% ^
	( mpr:corner ) -gravity northwest -compose over -composite ^
	( mpr:corner -rotate 90 ) -gravity northeast -compose over -composite ^
	( mpr:corner -rotate 180 ) -gravity southeast -compose over -composite ^
	( mpr:corner -rotate 270 ) -gravity southwest -compose over -composite ) ^
-alpha off -compose copy_opacity -layers composite "path2outputdir/%[filename:f].png"

imagefox87
Posts: 20
Joined: 2016-08-26T13:37:18-07:00
Authentication code: 1151

Re: How to batch composite?

Post by imagefox87 » 2016-08-27T12:57:22-07:00

All of your codes work but the processing time is very large and it uses the RAM. Hence, I want to use a for loop that saves an image as soon as that image is processed rather than storing it in RAM. I'm using this code to process up to 12,000 images. Other programs like XnConvert usually take 3-4 hours to do this but it it saves the images as they are being done so you know how long and it doesn't slow the computer

I've been reading this: http://www.imagemagick.org/Usage/windows/#for_loops

I'm not sure if I need "^" based on the syntax. This is GeeMack's original code with some modifications. Here is my code:

Code: Select all

@ECHO OFF
for %%g in (*.png) do (
	convert %%g ^
	   -background none ^
	   -gravity northwest ^
	   ( ^
		  -size 12x12 ^
		  xc:black ^
		  -draw "fill white circle 12,12 12,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 ^
		  "batch/%%g"
	)
Any help would be appreciated.

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

Re: How to batch composite?

Post by fmw42 » 2016-08-27T13:07:15-07:00

His code above that you reference loads all the images in memory. You should take my single image solution above (at viewtopic.php?f=1&t=30365&start=15#p137074) and put a for loop around it. Sorry, I do not code Windows bat scripts, so cannot be of much help.

imagefox87
Posts: 20
Joined: 2016-08-26T13:37:18-07:00
Authentication code: 1151

Re: How to batch composite?

Post by imagefox87 » 2016-08-27T13:10:18-07:00

fmw42 wrote:His code above that you reference loads all the images in memory. You should take my single image solution above (at viewtopic.php?f=1&t=30365&start=15#p137074) and put a for loop around it. Sorry, I do not code Windows bat scripts, so cannot be of much help.
I've converted yours too but it doesn't start:

Code: Select all

@ECHO OFF
for %%g in (*.png) do (
	convert %%g
	( -size 25x25 xc:black -fill white ^
	   -draw "circle 25,25 25,0" -alpha off ^
	   -write mpr:corner +delete ) ^
	( -clone 0 -fill white -colorize 100%% ^
	   ( mpr:corner ) -gravity northwest -compose over -composite ^
	   ( mpr:corner -rotate 90 ) -gravity northeast -compose over -composite ^
	   ( mpr:corner -rotate 180 ) -gravity southeast -compose over -composite ^
	   ( mpr:corner -rotate 270 ) -gravity southwest -compose over -composite ) ^
	-alpha off -compose copy_opacity -composite -compose over ^
	( +clone -background black -shadow 80x3+5+5 ) ^
	+swap -background none -layers merge +repage  "batch/%%g"
)

Post Reply