Adding rounded corners of a specific colour to JPG

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
nickharambee
Posts: 5
Joined: 2010-12-06T18:33:20-07:00
Authentication code: 8675308

Adding rounded corners of a specific colour to JPG

Post by nickharambee »

Hi

I am using the following command line in an applescript to convert a folder of JPGs to a specific size:

-resize 395x275^ -gravity center -extent 395x275 -quality 95

I would like to add some rounded corners of 5px, specifying a colour for these corners (as I wan't to keep the files as JPGs not PNGs I can't use transparent rounded corners).

Is it possible to do this with ImageMagick?

Thanks

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

Re: Adding rounded corners of a specific colour to JPG

Post by fmw42 »

see http://www.imagemagick.org/Usage/thumbnails/#rounded

You can then just flatten the image against some color background to convert the transparent areas to whatever color you want.

convert image_with_transparent_corners -background somecolor -flatten image_with_color_corners

see

http://www.imagemagick.org/Usage/layers/#flatten


For example:

convert thumbnail.gif \
\( +clone -alpha extract \
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0' \
\( +clone -flip \) -compose Multiply -composite \
\( +clone -flop \) -compose Multiply -composite \
\) -alpha off -compose CopyOpacity -composite -compose over -background red -flatten thumbnail_red_corners.gif


Though I am not real sure this is what you really want.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Adding rounded corners of a specific colour to JPG

Post by anthony »

It is posible to generate a rounded corner overlay of the right color and overlay that in each corner.

Hmmm just set background color to what ever you like...

Code: Select all

convert thumbnail.gif \( +clone -crop 16x16+0+0 \
                -draw 'fill white color 30,20 reset fill black circle 15,15 15,0' \
                -background red  -alpha shape \
                \( +clone -flip \) \( +clone -flop \) \( +clone -flip \) \
             \) -flatten  thumbnail_red_corner.png
Actually if you add a -compose DstOut before the flatten you will get transparent colors!

BUG found... -flip and -flop are NOT correctly handling virtual canvas information!
The resulting offset is not as if you flipped the virtual canvas, but just the image itself!
This bug is a show stopper. Sorry.

ASIDE: -rotate 180 does handle virtual canvas right, but that only gets one other corner, It is no help unless the image is square :-(

UPDATE: This bug has been fixed in IM so the above does work from version 6.6.6-5
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
nickharambee
Posts: 5
Joined: 2010-12-06T18:33:20-07:00
Authentication code: 8675308

Re: Adding rounded corners of a specific colour to JPG

Post by nickharambee »

Thanks for these prompt responses.

I am left a little confused though by the last message. Are you saying Anthony that what I'd like to do can't be achieved by ImageMagick?

Just to confirm, as the first poster seemed unsure, what I am trying to do is to add a rounded corner to JPGs, but instead of making the cut out area transparent, I'd like to use a solid colour instead (as JPGs don't support transparency). This colour would match the background colour of a website, so, effectively it would appear as if transparent over the overall background. The reason for doing it this way is that JPGs tend to be smaller file sizes than PNG files, and I was wanting to keep file size down as much as possible.

If it isn't possible to do the JPG method though, and the PNG method (i.e. transparent rounded corners) would work with ImageMagick then I would consider this.

Nick
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Adding rounded corners of a specific colour to JPG

Post by anthony »

nickharambee wrote:I am left a little confused though by the last message. Are you saying Anthony that what I'd like to do can't be achieved by ImageMagick?
No. What I gave should be working. But it seems that flip and flop does not handle virtual canvases. My fault really, I should have checked flip and flop when I was checking the other operations for virtual canvas handling 5 years ago, That is: -crop, -resize, -splice, -rotate; all handle virtual canvas offsets as you would expect, but I neglected to include -flip and -flop at that time. So now when I wrote the above example to use it I did not have it :-(

Basically I am kicking myself for my neglect!

The above would be a lot faster, as it does not compose large images, just small corners. The only fix for that is to DIY the calculations of those offsets, and that makes what should be a simple, 'just do it' example, much more complex.

I have added the fix of flip and flop virtual offsets to my ToDo list, unless Cristy steps in and does it first. I have little to no time at the moment.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding rounded corners of a specific colour to JPG

Post by fmw42 »

My flatten method above does work. But I am not sure you want a rounded corner image with red background to fill it out to the rectangular bounds. If that is what you want, my method does work, but may not be as optimal as Anthony's. Try my example, taking the thumbnail image from Anthony's examples page on Thumbnails and rounded corners as linked above. Let us know if that is what you want.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Adding rounded corners of a specific colour to JPG

Post by anthony »

Just an update as of IM 6.6.6-5 the virtual canvas bug in -flip and -flop, as well as -transpose and -transverse was fixed.
Allowing the example I thought would work, to now work as expected.

Actually it was fixed in IM v6.6.6-4 but the offset modification was not correct.

However due to an interaction with the 'background canvas' internally generated by -flatten you can only use it to add color to the corners of an image. You can not use it to erase corners to transparency.

I have added the example (with the appropriate warnings) to the Rounded and Shaped Corners examples set.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
nickharambee
Posts: 5
Joined: 2010-12-06T18:33:20-07:00
Authentication code: 8675308

Re: Adding rounded corners of a specific colour to JPG

Post by nickharambee »

Sorry it's taken a while to respond. I have now had time to try both methods for achieving the coloured background corners, and this method works fine:

convert thumbnail.gif \
\( +clone -alpha extract \
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0' \
\( +clone -flip \) -compose Multiply -composite \
\( +clone -flop \) -compose Multiply -composite \
\) -alpha off -compose CopyOpacity -composite -compose over -background red -flatten thumbnail_red_corners.gif

this method however produced all four coloured corners in the top left of the image joined together, rather than in each corner of the image:

convert thumbnail.gif \
\( +clone -crop 16x16+0+0 -fill white -colorize 100% \
-draw 'fill black circle 15,15 15,0' \
-background Red -alpha shape \
\( +clone -flip \) \( +clone -flop \) \( +clone -flip \) \
\) -flatten rounded_corners_red.png

I am converting jpg to jpg. I am not sure if this makes a difference.

Also I am wondering how I would incorporate these commands into an applescript. I have a couple of basic commands working in another applescript:

e.g. set convertCommand to quoted form of POSIX path of convertPath & " -define jpeg:size=840x500 -thumbnail '420x250>' -background '#24221F' -gravity center -extent 420x250 -quality 95 "

but when I try to enter the command for rounded corners above I get an error message when I try to save the script suggesting that the command needs to be entered differently for the script.

Thanks,

Nick
nickharambee
Posts: 5
Joined: 2010-12-06T18:33:20-07:00
Authentication code: 8675308

Re: Adding rounded corners of a specific colour to JPG

Post by nickharambee »

Hi,

I'm still wondering how to integrate this ImageMagick command into an applescript, and would be grateful for any help.

Thanks

Nick
Last edited by nickharambee on 2011-02-19T10:59:21-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding rounded corners of a specific colour to JPG

Post by fmw42 »

You need to use do shell script

Search google with "imagemagick in applescript"

e.g

http://macscripter.net/viewtopic.php?id=18375


see also
http://studio.imagemagick.org/pipermail ... 23198.html
nickharambee
Posts: 5
Joined: 2010-12-06T18:33:20-07:00
Authentication code: 8675308

Re: Adding rounded corners of a specific colour to JPG

Post by nickharambee »

Hi,

Thanks. I'm aware I need to do a shell script.

I have previously got an ImageMagick command working in an applescript, by doing the following:

set convertPath to "Macintosh HD:opt:local:bin:convert"

-- setup the image magick command
set convertCommand to quoted form of POSIX path of convertPath & " -define jpeg:size=840x500 -thumbnail '420x250>' -background '#24221F' -gravity center -extent 420x250 -quality 95 "

-- convert the images

repeat with i from 1 to count of theImages
set thisImage to (item i of theImages) as text
if thisImage is not in convertedFileList then
set end of convertedFileList to thisImage
set fileCounter to fileCounter + 1
set outPath to outputFolder & fileCounter & ".jpg"

do shell script convertCommand & quoted form of POSIX path of thisImage & space & quoted form of POSIX path of outPath
end if
end repeat

But I am not sure how to incorporate the command for rounded corners into the script, i.e.:

"convert thumbnail.gif \\
\\( +clone -alpha extract \\
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0' \\
\\( +clone -flip \\) -compose Multiply -composite \\
\\( +clone -flop \\) -compose Multiply -composite \\
\\) -alpha off -compose CopyOpacity -composite -compose over -background red -flatten thumbnail_red_corners.gif"

Thanks

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

Re: Adding rounded corners of a specific colour to JPG

Post by fmw42 »

Sorry Nick, my Applescripting goes back to OS 9 and I have not really worked with IM in Applescript since then. At that time, there was a very simple IM Applescript dictionary for a Mac app version of IM called ImageMagick_AE authored by Leonard Rosenthol, if I recall. Both IM and Applescript have evolved and I have not kept up with the use of IM in Applescript as now I do mostly bash shell scripts.

Hopefully, someone else on the forum will have had more experience with current Applescript and IM, than I.

Fred
Post Reply