Draw/add a 3 pixel pure white frame around a picture?

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
bensto
Posts: 31
Joined: 2012-07-02T00:32:10-07:00
Authentication code: 13

Draw/add a 3 pixel pure white frame around a picture?

Post by bensto »

How can I draw/add a 3 pixel wide pure white frame around an existing *.jpg or *.png picture

D:\mypicture\holidayjune\pic123.jpg

or

D:\mypicture\holidayjune\pic123.png

Keep in mind that the picture could not be rectangle but round/elliptic as well.

Ben
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Draw/add a 3 pixel pure white frame around a picture?

Post by Bonzo »

Code: Select all

 -bordercolor White -border 3x3
Note the image will grow by 6px in the width and height.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Draw/add a 3 pixel pure white frame around a picture?

Post by fmw42 »

Keep in mind that the picture could not be rectangle but round/elliptic as well.
I assume you mean you want a border around the non-background region.

Convert the image into a binary mask, which you can do, since the outside of the region is a known color, via -fuzz XX% -threshold or -fuzzXX% -floodfill (or use -draw to do the floodfill). The either blur and threshold the mask or use -morphology dilate/erode (depending upon polarity). Get the difference between the mask before and after thresholding. Make a copy and color it. Then composite the images together.

If you post a link to example image, we can see what you really want and perhaps provide the code.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Draw/add a 3 pixel pure white frame around a picture?

Post by fmw42 »

In unix, this would be something like either of the following:

Image

convert rounded_corners.png -bordercolor none -border 10 \
\( -clone 0 -alpha extract -morphology edgeout octagon:3 \) \
\( -clone 1 -fuzz 20% -fill red -opaque white -fill red -opaque black \) \
+swap -compose over -composite -trim +repage rounded_corners_border1.png

Image


convert rounded_corners.png -bordercolor none -border 10 \
\( +clone -alpha extract \) \
\( +clone -alpha extract -blur 3x65000 -level 0x50% -threshold 0 \) \
\( +clone -clone 1 -compose difference -composite \) \
\( +clone -fuzz 20% -fill red -opaque white \) \
-delete 1,2 +swap -compose over -composite -trim +repage rounded_corners_border2.png

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

Re: Draw/add a 3 pixel pure white frame around a picture?

Post by anthony »

Note that the gaps are pixels which are not quite 50% transparent! (more opaque than transparent), so was throught to be 'foreground'. Remember morphology is typically a binary image operator.

The solution for anti-aliased edges is not so simple!
See Image Transformations, Edge Detection, Edge Outlines from Anti-Aliased Shapes
http://www.imagemagick.org/Usage/transform/#edge_jitter

It includes techniques to try and anti-alias binary images.

THE best technique I have found so far is to extract the shape, convert to a vector image, then draw that shape appropriately.
Edging using a Raster to Vector Converter
http://www.imagemagick.org/Usage/transform/#edge_vector
Once you have the vector stroke it with a width of 6, then overlay the original image (cutting it to 3).
It is of course the more computationally intensive too :lol:
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply