Page 1 of 1
convert: extract an horizontal band
Posted: 2014-06-25T07:12:23-07:00
by Cequiel
Hi Everybody,
I have the following command line:
Code: Select all
convert \( some drawing, scaling, rotation and translation operations... \) output.png
The above command is the result of apply several image operations. The result of these operations is an arbitrary image and the (0, 0) point is not necessarily at the top left corner. I would like to extract an horizontal band and I tried this command:
Code: Select all
convert \( some pictures, drawing, scaling, rotation and translation operations... \) -extent 0x125 output.png
But the above command doesn't work properly because the 'extent' directive assumes that the (0, 0) point is at the top left corner (which is not always true). How could I extract an horizontal band of an arbitrary image with arbitrary geometry starting from the (0, 0) point?
Here's an illustration showing what I would like to do:
Thanks and sorry for my English

Re: convert: extract an horizontal band
Posted: 2014-06-25T07:48:57-07:00
by snibgo
The usual operation for extracting a portion of an image is "-crop". This takes any canvas into account. Generally, follow "-crop" with "+repage" to remove canvas information. For example, on Windows, I make an image g.png where (0,0) isn't top-left. (0,0) is close to bottom-right. Then I crop, and the result g1.png comes from the bottom-right of the input. I asked for 100 pixels width, but the result is only 20 pixels wide, because that is all the pixels to the right of (0,0).
Code: Select all
F:\web\im>%IM%convert -size 100x100 gradient: -repage -80-80 g.png
F:\web\im>%IM%identify g.png
g.png PNG 100x100 100x100-80-80 16-bit sRGB 647B 0.000u 0:00.000
F:\web\im>%IM%convert g.png -crop 100x10+0+0 +repage g1.png
F:\web\im>%IM%identify g1.png
g1.png PNG 20x10 20x10+0+0 16-bit sRGB 286B 0.000u 0:00.000
If this doesn't help, please post an input image, and say what portion you want to extract.
Re: convert: extract an horizontal band
Posted: 2014-06-25T12:49:36-07:00
by snibgo
I can use your image as an example. I move (0,0) to where you want it:
Code: Select all
convert example2.png -repage -187-93 e.png
I check this has worked:
Code: Select all
identify e.png
e.png PNG 563x317 563x317-187-93 8-bit sRGB 128KB 0.000u 0:00.000
The steps above would be done by your rotation, translation, etc.
I crop it to 125 pixels high:
Code: Select all
convert e.png -crop x125+0+0 +repage cropped.png
cropped.png is:

Re: convert: extract an horizontal band
Posted: 2014-06-25T14:38:36-07:00
by Cequiel
Thanks snibgo. The problem is that I don't know where is exactly that point. Actually, the image is generated automatically by a mysterious algorithm
snibgo wrote:I can use your image as an example. I move (0,0) to where you want it...
Re: convert: extract an horizontal band
Posted: 2014-06-25T14:43:22-07:00
by Bonzo
So you have a point that you do not know where it is and you do not know how to find it and you want to use it as the reference
I would think you need to post a real life image for people to try.
I think snibgo is saying that the point you want may be in you image file but not in the image file you posted here so he had to manually create it. Anyway as I say best to post a real life example.
Re: convert: extract an horizontal band
Posted: 2014-06-25T14:49:17-07:00
by Cequiel
I didn't know the 'identify' command. It was very useful. I did the following:
Code: Select all
// creates the image
convert \( mysterious algorithm ... \) output.png
Code: Select all
// gets the offset Y
identify -format '%Y' output.png
// the above command returns
+4294967207
Actually, the offset "4294967207" is an unsigned value. If you want to get the signed value, you have to do the following:
Code: Select all
<?php
$base = pow(2, 31);
if ($offsetY > $base) $offsetY -= 2 * $base;
In this case the unsigned value "4294967207" is equal to the signed value "-89". Now, you can mount the command to extract the horizontal band:
Code: Select all
// change '-89' by '+89'
convert output.png -background transparent -extent 0x125+0+89 +repage output.png
The above command extracts a band starting from the point (0,0). It is very complicated and very hard to implement, but it works.
snibgo wrote:The usual operation for extracting a portion of an image is "-crop". This takes any canvas into account. Generally, follow "-crop" with "+repage" to remove canvas information. For example, on Windows, I make an image g.png where (0,0) isn't top-left. (0,0) is close to bottom-right. Then I crop, and the result g1.png comes from the bottom-right of the input. I asked for 100 pixels width, but the result is only 20 pixels wide, because that is all the pixels to the right of (0,0).
Code: Select all
F:\web\im>%IM%convert -size 100x100 gradient: -repage -80-80 g.png
F:\web\im>%IM%identify g.png
g.png PNG 100x100 100x100-80-80 16-bit sRGB 647B 0.000u 0:00.000
F:\web\im>%IM%convert g.png -crop 100x10+0+0 +repage g1.png
F:\web\im>%IM%identify g1.png
g1.png PNG 20x10 20x10+0+0 16-bit sRGB 286B 0.000u 0:00.000
If this doesn't help, please post an input image, and say what portion you want to extract.
Re: convert: extract an horizontal band
Posted: 2014-06-25T15:42:38-07:00
by Cequiel
I think there should be an argument similar to 'extent', but respecting the geometry. It would be very useful.
Re: convert: extract an horizontal band
Posted: 2014-06-25T16:38:51-07:00
by fmw42
Cequiel wrote:I think there should be an argument similar to 'extent', but respecting the geometry. It would be very useful.
More properly, you should be using -crop and not -extent. However, -extent does crop as well as extend.
With regard to cropping relative the virtual-canvas, I think IM already does that. see
http://www.imagemagick.org/Usage/crop/#crop_page. Note that if the offsets are positive, this means the origin is left and above the top left corner. For origins inside the image, the offsets will be negative. (I just tested this to be sure)
But if I read your post above, your origin is not specified as a proper offset in the virtual-canvas. Or did I misunderstand why you had to do all the calculations.
see
http://www.imagemagick.org/Usage/crop/#crop
http://www.imagemagick.org/Usage/crop/#extent