crop - Cut out one or more rectangular regions of the image

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
mleo
Posts: 11
Joined: 2016-07-05T06:07:14-07:00
Authentication code: 1151

crop - Cut out one or more rectangular regions of the image

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: crop - Cut out one or more rectangular regions of the image

Post 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.
snibgo's IM pages: im.snibgo.com
mleo
Posts: 11
Joined: 2016-07-05T06:07:14-07:00
Authentication code: 1151

Re: crop - Cut out one or more rectangular regions of the image

Post 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!!!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: crop - Cut out one or more rectangular regions of the image

Post 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
snibgo's IM pages: im.snibgo.com
mleo
Posts: 11
Joined: 2016-07-05T06:07:14-07:00
Authentication code: 1151

Re: crop - Cut out one or more rectangular regions of the image

Post 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
mleo
Posts: 11
Joined: 2016-07-05T06:07:14-07:00
Authentication code: 1151

Re: crop - Cut out one or more rectangular regions of the image

Post 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).
mleo
Posts: 11
Joined: 2016-07-05T06:07:14-07:00
Authentication code: 1151

Re: crop - Cut out one or more rectangular regions of the image

Post 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 ...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: crop - Cut out one or more rectangular regions of the image

Post by fmw42 »

PNG supports a virtual canvas that stores the size and offset of each crop. BMP does not support this.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: crop - Cut out one or more rectangular regions of the image

Post 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).
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: crop - Cut out one or more rectangular regions of the image

Post 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.
mleo
Posts: 11
Joined: 2016-07-05T06:07:14-07:00
Authentication code: 1151

Re: crop - Cut out one or more rectangular regions of the image

Post 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?
mleo
Posts: 11
Joined: 2016-07-05T06:07:14-07:00
Authentication code: 1151

Re: crop - Cut out one or more rectangular regions of the image

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: crop - Cut out one or more rectangular regions of the image

Post 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.
snibgo's IM pages: im.snibgo.com
mleo
Posts: 11
Joined: 2016-07-05T06:07:14-07:00
Authentication code: 1151

Re: crop - Cut out one or more rectangular regions of the image

Post 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 ...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: crop - Cut out one or more rectangular regions of the image

Post by snibgo »

If you have many rectangles to include or exclude, drawing them as black or white rectangles is a more obvious method.
snibgo's IM pages: im.snibgo.com
Post Reply