Rounded 3d borders with color transition

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
grouchysmurf
Posts: 8
Joined: 2011-06-13T01:17:23-07:00
Authentication code: 8675308

Rounded 3d borders with color transition

Post by grouchysmurf »

I am new to this site and ImageMagick too, so let me first say 'Hello everybody'.

I am long time GIMP user; for batch jobs I prefer Picture Window Pro. Currently, I am working on a virtual board for a wargame (you know, map of hexes, counters, dice, tables and so on). This requires a significant amount of slicing as well as creating borders for huge amount of small pictures.

What I want to accomplish is to create a 3d-like rounded border, something that looks like this:

Image.

I reckon ImageMagick may come in handy here but I cannot come up with a viable approach. I guess I will need to use some kind of gradient to create transitions between colors (top right and bottom left corners) and rounded borders (http://www.imagemagick.org/Usage/thumbn ... ded_border). But how do I combine those?

Can anybody provide me with some tips?
grouchysmurf
Posts: 8
Joined: 2011-06-13T01:17:23-07:00
Authentication code: 8675308

Re: Rounded 3d borders with color transition

Post by grouchysmurf »

This one below is not perfect and needs cleaning but so far it's best what I was able to come up with:

Code: Select all

convert -size 100x100 canvas:red canvas.png

convert canvas.png -border 3 -alpha transparent -background none -fill white -stroke none -strokewidth 0 -draw "roundrectangle 1,1 104,104 15,15" rounded_corner_mask.png

convert canvas.png -border 3 -alpha transparent -background none -fill none -stroke red -strokewidth 3 -draw "roundrectangle 1,1 104,104 15,15" rounded_corner_overlay.png

convert canvas.png -matte -bordercolor none -border 3 rounded_corner_mask.png -compose DstIn -composite rounded_corner_overlay.png -compose Over -composite rounded_border.png

convert -size 106x50 xc:"#aaaaaa" xc:"#333333" -size 106x6 gradient:"#aaaaaa"-"#333333" +swap -append -rotate 270 -distort SRT 45 overlap_mask.png

convert rounded_corner_overlay.png overlap_mask.png -compose hardlight -composite compose_hardlight.png

composite -compose DstIn rounded_corner_overlay.png compose_hardlight.png -alpha Set rounded_corner_overlay_dark.png

convert canvas.png -matte -bordercolor none -border 3  rounded_corner_mask.png -compose DstIn -composite rounded_corner_overlay_dark.png -compose Over -composite rounded_border.png

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

Re: Rounded 3d borders with color transition

Post by fmw42 »

see my script, bevel, at the link below.

Taking Anthony's first example rounded_corners.png from http://www.imagemagick.org/Usage/thumbnails/#rounded

Image

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 rounded_corners.png

Image

# extend the transparent border to make working room
convert rounded_corners.png -bordercolor none -border 20 rounded_corners_bord20.png

# now bevel it and remove the excess border that was added earlier
bevel rounded_corners_bord20.png miff:- | convert - -gravity center -shave 20x20 rounded_corners_beveled.png

Image
grouchysmurf
Posts: 8
Joined: 2011-06-13T01:17:23-07:00
Authentication code: 8675308

Re: Rounded 3d borders with color transition

Post by grouchysmurf »

Thank you.

It seems, however, that neither your script nor new version of IM work on my outdated Fedora 11 (I really need to upgrade it). So, I started to tinker with the script I posted earlier. Here is the result (it works!):

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION

@echo off

for %%a in (path_to_pngs\*.png) do (
	echo Processing file %%~dpnxa
	
	del color.txt
	C:\IM\convert %%~dpnxa -format "%%[pixel:s.p{5,5}]" info: >> color.txt
	

	C:\IM\convert %%~dpnxa -border 3 -alpha transparent -background none -fill white -stroke none -strokewidth 0 -draw "roundrectangle 1,1 64,64 5,5" rounded_corner_mask.png

	 FOR /F "delims=" %%B in ('TYPE color.txt') Do (
		C:\IM\convert %%~dpnxa -border 3 -alpha transparent -background none -fill none -stroke %%B -strokewidth 3 -draw "roundrectangle 1,1 64,64 5,5" rounded_corner_overlay.png	
  )
	
	C:\IM\convert -size 66x28 xc:"#aaaaaa" xc:"#222222" -size 66x10 gradient:"#aaaaaa"-"#222222" +swap -append -rotate 270 -distort SRT 45 overlap_mask.png
	
	C:\IM\convert rounded_corner_overlay.png overlap_mask.png -compose hardlight -composite compose_hardlight.png
	
	C:\IM\composite -compose DstIn rounded_corner_overlay.png compose_hardlight.png -alpha Set rounded_corner_overlay_dark.png
	
	C:\IM\convert %%~dpnxa -matte -bordercolor none -border 3  rounded_corner_mask.png -compose DstIn -composite rounded_corner_overlay_dark.png -compose Over -composite %%~na_frame.png

	move /y %%~na_frame.png wherever
)

del *.png
del *.txt

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

Re: Rounded 3d borders with color transition

Post by anthony »

Not suprizing. It relies on a Morphology function Distance, which was only added in the last few years.
Actually it was added early january 2010 while I was having a very long vacation in the China Hinterland :-)

For more detail see Compound Font Effects, Conical...
http://www.imagemagick.org/Usage/fonts/#conical
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: Rounded 3d borders with color transition

Post by fmw42 »

anthony wrote:Not suprizing. It relies on a Morphology function Distance, which was only added in the last few years.
Actually it was added early january 2010 while I was having a very long vacation in the China Hinterland :-)

For more detail see Compound Font Effects, Conical...
http://www.imagemagick.org/Usage/fonts/#conical

Anthony is talking about my bevel script using morphology distance functions and his reference shows how that works.
grouchysmurf
Posts: 8
Joined: 2011-06-13T01:17:23-07:00
Authentication code: 8675308

Re: Rounded 3d borders with color transition

Post by grouchysmurf »

Yeah, I know that. I commented the lines that invoked the morphology but it still wouldn't work.

Anyways, I am happy with the result as it is and even more for I made it by myself (well, almost, I recycled and compiled scripts I found in the internet).
grouchysmurf
Posts: 8
Joined: 2011-06-13T01:17:23-07:00
Authentication code: 8675308

Re: Rounded 3d borders with color transition

Post by grouchysmurf »

One minor correction.

The roundrectangle produces some semi-transparent pixels which themselves are not visible but when projected onto a opaque counter they create ugly lines that interfere with clean 3d look. A -threshold helps:

Instead of

Code: Select all

C:\IM\convert %%~dpnxa -border 3 -alpha transparent -background none -fill none -stroke %%B -strokewidth 3 -draw "roundrectangle 1,1 64,64 5,5" rounded_corner_overlay.png  
I now use

Code: Select all

C:\IM\convert%%~dpnxa -border 3 -alpha transparent -background none -fill none -stroke %%B -strokewidth 3 -draw "roundrectangle 1,1 64,64 5,5" -channel matte -threshold 50% rounded_corner_overlay.png
-channel matte -threshold 50% clips off those unwanted pixels.

Old:

Image

New:

Image

The difference is subtle but it is there. The latter image is much 'cleaner' with no strange artifacts.
Last edited by grouchysmurf on 2011-06-16T01:08:51-07:00, edited 1 time in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Rounded 3d borders with color transition

Post by anthony »

To me the former is cleaner. I don't see the lines you are talking about.

The semi-transparent pixels are meant to make the overall view smoother without 'stair-case' edges.

However if you like it like this add +antialias before the -draw operation. That will remove the semi-transparent pixel draw creates, in the rounded corner mask.

Another way of creating a rounded corner are listed in IM Examples, Thumbnailing
as a side effect of Soft Edges, just before the 'anti-aliased' Rounded Corner examples.
http://www.imagemagick.org/Usage/thumbnails/#soft_edges
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
grouchysmurf
Posts: 8
Joined: 2011-06-13T01:17:23-07:00
Authentication code: 8675308

Re: Rounded 3d borders with color transition

Post by grouchysmurf »

I like the latter better ;-)

The artifacts are caused - of which I am perfectly aware - by antialiasing.I don't want to turn it off for, when it's turned off, the results are really odd-looking.

Below are the masks are use for clipping off the counter corners. The left one is the older version.

Image

You can clearly see - all right, not so clearly, but it's clear enough - that the borders of the left rectangle are blurry. The blurriness is what yields the weird artifacts on the final image.
grouchysmurf
Posts: 8
Joined: 2011-06-13T01:17:23-07:00
Authentication code: 8675308

Re: Rounded 3d borders with color transition

Post by grouchysmurf »

This is the FINAL version of counters I decided to use:

Image

Let me just say ImageMagick ROCKS. I would never be able to add the fancy frames if it wasn't for IM. Thank you!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rounded 3d borders with color transition

Post by fmw42 »

Perhaps you could post a command line or set of command lines (not in a Batch File) for this example.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Rounded 3d borders with color transition

Post by anthony »

I am especially interested in the technique you used to add a direct gradient to border.

You seemed to generate gradient which you rotated and use to color the corners, something I have not explored.
I may be able to find faster ways to do this.

For example: One idea I had was to overlay (DstIn) a blurred -frame image to generate the gradient
Another is to use -sparse-color gradient (no need to rotate!)

Also rather than extracting the color from the image. Expanding the image using a Virtual-pixel methods may work better.


Also if you are converting a whole stack of these images, generating a template 'frame' whcih you can use as an overlay image or as a lighting effect, may work better than processing each image completely, but that may not be a problem as the images seem quite small.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
grouchysmurf
Posts: 8
Joined: 2011-06-13T01:17:23-07:00
Authentication code: 8675308

Re: Rounded 3d borders with color transition

Post by grouchysmurf »

Sure thing, here's the script:

Input: there are >200 images, each 96 pixels wide and 96 pixel high. They come in different colors; different colors represent different nationalities. In the game I design a board for there are, let me count... say, six nationalities.

Below is a sample counter:

Image

It's the German 61st Infantry Division.

Each counter is resized and slightly sharpened:

Code: Select all

convert zeton.png -resize 66x66 zeton_66x66.png
convert zeton_66x66.png -sharpen 0x0.4 zeton_ostry.png
It results with:

Image

zeton_ostry.png is the input file for further modifications.

Since counters come in different shades, first I have to determine what color needs to be used for borders. The method I use is very lousy but Windows batch script would just refuse to work with set color='convert zeton_ostry.png -format "%%[pixel:s.p{5,5}]" info:'. Instead, the color is written to the file from which I subsequently read its RGB value. Well, whatever, but it works! In case of uniformly colored images, this step is not mandatory. In fact, it's better to omit it.

Step A

Code: Select all

convert zeton_ostry.png -format "%%[pixel:s.p{5,5}]" info: >> color.txt
Then I generate an round corner mask which will be used to clip off the counter corners. This example originates from IM documentation:

Step B

Code: Select all

convert zeton_ostry.png -border 5 -alpha transparent -background none -fill white -stroke none -strokewidth 0 -draw "roundrectangle 2,2 73,73 10,10" rounded_corner_mask.png
Image

Step C

Then, using color that was determined in step A, I generate a rounded border. Mind the 'threshold' which is used to eliminate semitransparent pixels.

Code: Select all

FOR /F "delims=" %%B in ('TYPE color.txt') Do (
               convert zeton_ostry.png -border 5 -alpha transparent -background none -fill none -stroke %%B -strokewidth 5 -draw "roundrectangle 2,2 73,73 10,10" -channel matte -threshold 20% rounded_corner_overlay.png   
 )
Image

(it looks much worse when viewed on this forum; it looks best displayed against a black backround)

Step D

The gradient is generated with following code:

Code: Select all

convert -size 76x35 xc:"#aaaaaa" xc:"#444444" -size 76x6 gradient:"#aaaaaa"-"#444444" +swap -append -rotate 270 -distort SRT 45 overlap_mask.png
Gradient spanning through the whole picture width would be to soft. Through experiments I found out a 6-pixel wide gradient works best with the 10-pixel radii I use for rounded corners.

Image

Color values #aaaaaa and #444444 have also been picked up through tinkering with different shades of grey. Pure white and black didn't work too well in this case,

Step E

Gradient is used to lighten/darken the border that was generated in step C:

Code: Select all

convert rounded_corner_overlay.png overlap_mask.png -compose hardlight -composite compose_hardlight.png
Image

Step F

The border is cut from the image generated in step E:

Code: Select all

composite -compose DstIn rounded_corner_overlay.png compose_hardlight.png -alpha Set rounded_corner_overlay_dark.png
Image

Step G

And finally this border is pasted over the counter itself:

Code: Select all

convert zeton_ostry.png -matte -bordercolor none -border 5  rounded_corner_mask.png -compose DstIn -composite rounded_corner_overlay_dark.png -compose Over -composite zeton_ostry_frame.png
yielding final image

Image

I do know it all can be squeezed into fewer lines but, first of all, it's the third day I've been using IM, secondly, I am happy it works, if I start to tweak it, I will surely break it.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Rounded 3d borders with color transition

Post by anthony »

It's the German 61st Infantry Division.
I thought as much. War game counters...

But I like the different way you generate the gradient, though it is rather complex.
An alternative method may be to make use of shaded bevels, such as
http://www.imagemagick.org/Usage/fonts/#bevel

That bevel can be expanded by using linear blurs, which was pioneered by Fred.


The most difficult part in your method seems to be the extraction and use of the background color. Well there is an alternative. You could just grab the pixel, and resize it to the size wanted (for the composition.

Code: Select all

 convert zetonostry.png -crop 1x1+0+0 +repage -resize 76x76 background.png
You can also combine commands to remove the need for intermediate images. For example

Code: Select all

 convert zeton_ostry.png -crop 1x1+0+0 +repage -resize 76x76 \
      overlap_mask.png -compose hardlight -composite \
      rounded_corner_overlay.png SrcIn -alpha Set rounded_corner_overlay_dark.png
Now you can replace the various image reads with the processes that generated those parts (in parenthesis)
And eventually get it all in one command, without any intermediate images at all.

Code: Select all

convert zeton.png -resize 66x66 -sharpen 0x0.4 -border 5 \
  \( +clone -crop 1x1+5+5 +repage -resize 76x76 \) \
  \( -size 76x76 xc:none -draw "roundrectangle 2,2 73,73 10,10" \
        -clone 0 -compose SrcIn -composite \) \
  \( -size 76x76 xc:none -fill none -stroke white -strokewidth 5 \
        -draw "roundrectangle 2,2 73,73 10,10" \
        -channel A -threshold 20% +channel  \) \
  \( -size 6x6 gradient:"#aaaaaa"-"#444444" \
     -define distort:viewport=76x76-35-35 -distort SRT -45 \
     -clone 1 +swap -compose Hardlight -composite \
     -clone 3 -compose DstIn -composite \
     -clone 2 +swap -compose Over -composite \) \
  -delete 0--2  zeton_ostry_frame.png
Each Parenthesis, basically represents one command, with it using -clone to read in the results of previous commands. But the final line there are five images in memory (inital operations, + 4 parenthesis.
The -delete 0--2 removes all but the last image (the final result).

For debugging replace the final line with +append debug.png. OR you can insert -write debug.png operations to save intermediate images for viewing.

Do not forget to convert the command to windows (DOS) format using the rules in
http://www.imagemagick.org/Usage/window ... conversion

The technique and various options is show in IM examples, Basics,
Complex Image Processing and Debugging
http://www.imagemagick.org/Usage/basics/#complex



In IMv7 there is plans for a new technique that will let you do the above, but allow the wrapping script to make control decisions as needed, based on information about images being processed. This will include better use of colors.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply