How to erase everything inside a border?

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?".
ooker
Posts: 22
Joined: 2014-07-24T10:32:57-07:00
Authentication code: 6789

Re: How to erase everything inside a border?

Post by ooker »

If I want to customize the width of the output border in pixel, what should I do? I can change the argument in the octagon kernel, but I would like to control exactly how wide it is.

Because this is the border, I would like when it is enlarges, it is aligned it in the middle, not in left or right.
Image

Also, sometimes I would like the output to be a dotted line or a stroked line. Is there any way to manipulate this?

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

Re: How to erase everything inside a border?

Post by fmw42 »

Sorry this is not very clear to me. Can you show examples of what you want to happen and explain what you are trying to do for each example with an example output. You have too many cases. Break this into parts for each case with examples.
ooker
Posts: 22
Joined: 2014-07-24T10:32:57-07:00
Authentication code: 6789

Re: How to erase everything inside a border?

Post by ooker »

Sorry for making you confused. I'll try to explain better.
  1. These are three images of the output when the argument of octagon is 1, 3 and 5:
    ImageImageImage

    The point is, when you enlarge the border, it expand inside. I would like it to expand equally inside and outside, which means the heart of the border is unmoved.
  • Sometimes I would like the output to be a dotted line or a dashed line. Is there a way to change the code to achieve that?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to erase everything inside a border?

Post by fmw42 »

The point is, when you enlarge the border, it expand inside. I would like it to expand equally inside and outside, which means the heart of the border is unmoved.
Yes, there is. Replace edgein with edge and half the octagon:size
Sometimes I would like the output to be a dotted line or a dashed line. Is there a way to change the code to achieve that?
Yes there is. See -draw about types of line strokes at http://www.imagemagick.org/Usage/draw/#mvg_settings

I will have to get back to you later with updates on the script. I will be out most of the day.
ooker
Posts: 22
Joined: 2014-07-24T10:32:57-07:00
Authentication code: 6789

Re: How to erase everything inside a border?

Post by ooker »

I have tried to do that myself, and haven't succeeded. I ask you in a seperate thread viewtopic.php?f=1&t=26191&p=114987#p114987 and you say there is no way. So can you help me again? Thank you.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to erase everything inside a border?

Post by fmw42 »

fmw42 wrote:
Sometimes I would like the output to be a dotted line or a dashed line. Is there a way to change the code to achieve that?
Yes there is. See -draw about types of line strokes at http://www.imagemagick.org/Usage/draw/#mvg_settings

I will have to get back to you later with updates on the script. I will be out most of the day.
Sorry I misunderstood your question. If you have a raster image with lines, I do not think you can easily change solid lines to dashed lines. You can only do that if you have a vector image such as SVG. The only way I can think to do that is to use the image as a mask and composite it with some other image that has a tiny checkerboard pattern.

Here is an example:

Code: Select all

convert -respect-parenthesis 1.png \
\( -size 3x3 xc:white xc:black +append \( +clone -flop \) -append \
-write mpr:checks +delete -size 640x480 tile:mpr:checks \) \
\( -clone 0 -alpha extract \) \
\( -clone 1 -clone 2 -compose multiply -composite \) \
-delete 1,2 -alpha off -compose over -compose copy_opacity -composite 1_proc.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to erase everything inside a border?

Post by fmw42 »

fmw42 wrote:
The point is, when you enlarge the border, it expand inside. I would like it to expand equally inside and outside, which means the heart of the border is unmoved.

This works for me by changing edgein to edge and making -bordercolor none and -border = octagon value or larger if needed and then shave by octagon value. So here I use octagon:2 which will add about 2 pixels to each side of the edge.

Code: Select all

infile="1.png"
color="black"
w2=`convert $infile -format "%[fx:w-2]" info:`
h2=`convert $infile -format "%[fx:h-2]" info:`
convert $infile -background white -flatten -fill black +opaque white -bordercolor none -border 2 \
-fill none -draw "matte 2,2 floodfill matte $w2,2 floodfill matte $w2,$h2 floodfill matte 2,$h2 floodfill" \
-fill white +opaque none -fill black -opaque none \
-alpha off -morphology edge octagon:2 \
-channel rgba -fill none +opaque white -fill $color -opaque white -shave 2x2 result.png

ooker
Posts: 22
Joined: 2014-07-24T10:32:57-07:00
Authentication code: 6789

Re: How to erase everything inside a border?

Post by ooker »

This one is cleaner. Thank you so much :D

Can I adjust the width of the line in pixel?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to erase everything inside a border?

Post by fmw42 »

ooker wrote: Can I adjust the width of the line in pixel?
change the octagon value. It will put approximately one pixel on each side of the line. Or use a different shape, such as square. See
http://www.imagemagick.org/Usage/morphology/#kernel
ooker
Posts: 22
Joined: 2014-07-24T10:32:57-07:00
Authentication code: 6789

Re: How to erase everything inside a border?

Post by ooker »

Thanks.

When I try your code, it runs well. However when I try to make an output as a variable:

Code: Select all

infile="overlay.png"
outfile="overlay_cleaned.png"
width=3
w2=`convert $infile -format "%[fx:w-2]" info:`
h2=`convert $infile -format "%[fx:h-2]" info:`
 
convert $infile -background white -flatten -fill black +opaque white -bordercolor none -border 2 \
-fill none -draw "matte 2,2 floodfill matte $w2,2 floodfill matte $w2,$h2 floodfill matte 2,$h2 floodfill" \
-fill white +opaque none -fill black -opaque none \
-alpha off -morphology edge octagon:$width \
-channel rgba -fill none +opaque white -fill $color -opaque white -shave 2x2 $outfile
It gives this error:

Code: Select all

convert.im6: unable to open image `-opaque': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `-opaque' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `white': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `white' @ error/constitute.c/ReadImage/544.
I don't know why it's wrong.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to erase everything inside a border?

Post by snibgo »

What is the value of $color? That's the message you'll get if you haven't set it.
snibgo's IM pages: im.snibgo.com
ooker
Posts: 22
Joined: 2014-07-24T10:32:57-07:00
Authentication code: 6789

Re: How to erase everything inside a border?

Post by ooker »

Stupid me. Thank you so much :D
Post Reply