Page 1 of 1
crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-05T06:16:42-07:00
by mleo
Hi,
I have a set of (overlapping) rectangles and I want to crop a region of ONE image that is coverd by one or more of these rectangles. Example: If first rectangle would be a horizontal bar and second would be a vertical bar both overlapping then I would like to get a cross as THE result image. How do I have to call convert then?
Thank you
Re: crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-05T06:36:14-07:00
by snibgo
There are many ways. For example:
Arrange to have each rectangle white in an image the size of the original, with the rest of the image black.
Composite these two rectangles, "compose Lighten", so now you have an image that is white there was at least one rectangle and black elsewhere.
Use that "compose CopyOpacity" to paint out any pixels that were not covered by either rectangle.
If you had supplied images, and told us your version of IM and your platform, I could have shown you the commands.
Re: crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-05T07:02:41-07:00
by mleo
Version: ImageMagick 6.8.9-9 Q16 i686 2016-06-01
http://www.imagemagick.org
System: Debian testing
convert rose.png -crop 70x10+0+26 -crop 10x46+30+0 rose_cross.png
(Leads to be a dot at the moment)
Thank you for yout help!!!
Re: crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-05T07:33:36-07:00
by snibgo
You are making a crop, then making a crop of that, which doesn't leave much.
Perhaps this is what you want (bash script):
Code: Select all
convert \
rose: \
\( -clone 0 -fill Black -colorize 100 +write mpr:BLCK +delete \) \
\( \
\( mpr:BLCK -region 70x10+0+26 -fill White -colorize 100 \) \
\( mpr:BLCK -region 10x46+30+0 -fill White -colorize 100 \) \
-compose Lighten -composite \
\) \
-compose CopyOpacity -composite \
c.png
Re: crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-05T07:45:21-07:00
by mleo
I adapted it like this especially setting mpr:block instead of mpr:BLCK which did not work. Now all fine: Thank you very much!!!
convert ~/Downloads/rose.png \( -clone 0 -fill Black -colorize 100 +write mpr:block +delete \) \( \( mpr:block -region 70x10+0+26 -fill White -colorize 100 \) \( mpr:block -region 10x46+30+0 -fill White -colorize 100 \) -compose Lighten -composite \) -compose CopyOpacity -composite rose_cross.png
Re: crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-06T08:14:56-07:00
by mleo
The example worked but in real there are problems: I used this image:
http://www.directupload.net/file/d/4408 ... n3_png.htm
Calling:
convert "/tmp/import_crop.png" \( -clone 0 -fill Black -colorize 100 +write mpr:block +delete \) \( \( mpr:block -region 338x49+187+0 -fill White -colorize 100 \) \( mpr:block -region 409x156+116+49 -fill White -colorize 100 \) -compose Lighten -composite \) -compose CopyOpacity -composite /tmp/export_crop.bmp
It is exactly the same as above except different numbers. Even file types are identical:
/home/orther/Downloads/rose.png: PNG image data, 70 x 46, 8-bit/color RGB, non-interlaced
/tmp/import_crop.png: PNG image data, 525 x 700, 8-bit/color RGB, non-interlaced
But the result image looks exactly the same like import image. So no change between org and dest picture. What could be the reason? (I tried different image type like bmp, jpg, png. Always output looked like input).
Re: crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-06T08:18:01-07:00
by mleo
Solution found: Export image type must be png and not bmp. If bmp is set there is no change on image. png is cropped. Why ever ...
Re: crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-06T09:33:37-07:00
by fmw42
PNG supports a virtual canvas that stores the size and offset of each crop. BMP does not support this.
Re: crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-06T10:16:51-07:00
by snibgo
But the commands shown don't rely on offsets being stored in files.
The commands do make pixels transparent, so that won't work when the output doesn't support transparency (eg JPG).
Re: crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-06T11:54:24-07:00
by fmw42
snibgo wrote:But the commands shown don't rely on offsets being stored in files.
Sorry, I did not read the post carefully and thought it was another post where virtual canvas was I believe the issue.
Re: crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-07T01:33:28-07:00
by mleo
I am sorry asking again, I catched the example for one case but could not repeat it.
Using this (very large) image:
http://www.file-upload.net/download-117 ... 9.bmp.html saved as page569.bmp
I then called:
convert -crop 525x700+516+1110 page569c.bmp /tmp/figure.png
which should have extracted the left figure. I get the error: convert: invalid chromaticities `/tmp/figure.png' @ warning/png.c/MagickPNGWarningHandler/1671.
So I used:
convert -crop 525x700+516+1110 page569c.bmp /tmp/figure.bmp
which extracted the figure perfectly. Now I want to remove the text around the figure using the frames. I call then:
convert /tmp/figure.bmp \( -clone 0 -fill Black -colorize 100 +write mpr:block +delete \) \( \( mpr:block -region 338x49+187+0 -fill White -colorize 100 \) \( mpr:block -region 409x156+116+49 -fill White -colorize 100 \) \( mpr:block -region 525x280+0+205 -fill White -colorize 100 \) \( mpr:block -region 483x215+42+484 -fill White -colorize 100 \) -compose Lighten -composite \) -compose CopyOpacity -composite /tmp/figure2.bmp
which relates to the new coordinates of the cropped figure. It does not any effect. /tmp/figure2.bmp looks exactly the same like /tmp/figure.bmp.
What could be the reason?
Re: crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-07T01:37:54-07:00
by mleo
As I understand I should make png of it because bmp does not support transparency. Event I get this error I get the png. When calling the frame term on png instead on bmp:
convert /tmp/figure.png \( -clone 0 -fill Bla... ompose CopyOpacity -composite /tmp/figure2.png
it is the same result: /tmp/figure.png and /tmp/figure2.png are identical.
Re: crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-07T03:00:35-07:00
by snibgo
mleo wrote:convert -crop 525x700+516+1110 page569c.bmp /tmp/figure.bmp
Your syntax is wrong. You should read the image, then crop, then write it.
mleo wrote:convert /tmp/figure.bmp
\( -clone 0 -fill Black -colorize 100 +write mpr:block +delete \)
\(
\( mpr:block -region 338x49+187+0 -fill White -colorize 100 \)
\( mpr:block -region 409x156+116+49 -fill White -colorize 100 \)
\( mpr:block -region 525x280+0+205 -fill White -colorize 100 \)
\( mpr:block -region 483x215+42+484 -fill White -colorize 100 \)
-compose Lighten -composite \)
-compose CopyOpacity -composite /tmp/figure2.bmp
"-composite" operates on two images, or two images plus a mask. It won't operate on four images.
Re: crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-07T04:30:03-07:00
by mleo
Is there any way to solve this problem cropping an area consisting of two OR MORE rectangles using imagemagick? Otherwise I would calculate outstanding rectangles and draw them as white rectangles onto the picture ...
Re: crop - Cut out one or more rectangular regions of the image
Posted: 2016-07-07T05:17:28-07:00
by snibgo
If you have many rectangles to include or exclude, drawing them as black or white rectangles is a more obvious method.