Page 1 of 1

Crop Image Using Outside of the Image

Posted: 2017-11-02T04:24:15-07:00
by tugrul
Crop command is working well using negative offset parameters but transparent areas are trimming.

How can i keep transparent areas outside of canvas on crop?

Re: Crop Image Using Outside of the Image

Posted: 2017-11-02T05:31:31-07:00
by Bonzo
You need to:
Specify your IM version
Command you are using
Link to an image for testing

Re: Crop Image Using Outside of the Image

Posted: 2017-11-02T05:39:23-07:00
by tugrul

Code: Select all

convert rose: -crop 70x45+0-10 crop.png
I wanted to:
canvas size have to 70x45 with 10 pixels transparent area top of the canvas.

result:
10 pixel transparent area is trimmed and final canvas size is 70x35

Re: Crop Image Using Outside of the Image

Posted: 2017-11-02T06:15:51-07:00
by snibgo
"-crop" won't give you pixels that are outside the image. IM calls these "virtual pixel", as opposed to "authentic pixels" that are those inside the image. You can get virtual pixels with a viewport and a null "-distort", but if you want transparency, that is overkill.

If you simply want to append 10 rows to the top:

Code: Select all

convert -size 1x10 xc:None rose: -background None -append out.png
And you can crop that, if you want:

Code: Select all

convert -size 1x10 xc:None rose: -background None -append -crop 70x35+0+0 +repage out.png

Re: Crop Image Using Outside of the Image

Posted: 2017-11-02T09:58:41-07:00
by fmw42
try this

Code: Select all

convert rose: -background none -gravity north -extent 70x55+0-10 crop.png
Make the height 10 pixels more than the input and use the yoffset of -10. Set the background color to whatever you want.

Re: Crop Image Using Outside of the Image

Posted: 2017-11-02T11:19:00-07:00
by tugrul
Thank you all.

My real reason is cropping image using PHP Imagick.

I just augmented some cropping strategies to use in PHP Imagick. I have to use -extent method to fit crop origin to 0,0.

Following code made my request.

Code: Select all

<?php

$imagick = new \Imagick();
$imagick->setBackgroundColor('white');
$imagick->readImage('in.png');

$width = $imagick->getImageWidth();
$height = $imagick->getImageHeight();

$width -= min(0, $width - ($crop['width'] + $crop['x']));
$width -= min(0, $crop['x']);

$height -= min(0, $height - ($crop['height'] + $crop['y']));
$height -= min(0, $crop['y']);

$imagick->extentImage($width, $height, $crop['x'], $crop['y']);
$imagick->cropImage($crop['width'], $crop['height'], 0, 0);
$imagick->setImagePage($crop['width'], $crop['height'], 0, 0);
$imagick->writeImage('out.png');

?>