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
Draw/add a 3 pixel pure white frame around a picture?
Re: Draw/add a 3 pixel pure white frame around a picture?
Code: Select all
-bordercolor White -border 3x3
- 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?
I assume you mean you want a border around the non-background region.Keep in mind that the picture could not be rectangle but round/elliptic as well.
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.
- 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?
In unix, this would be something like either of the following:

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

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


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

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

- 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?
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
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

Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/