Crop Image Using Outside 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
tugrul
Posts: 3
Joined: 2017-11-02T04:18:09-07:00
Authentication code: 1152

Crop Image Using Outside of the Image

Post 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?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Crop Image Using Outside of the Image

Post by Bonzo »

You need to:
Specify your IM version
Command you are using
Link to an image for testing
tugrul
Posts: 3
Joined: 2017-11-02T04:18:09-07:00
Authentication code: 1152

Re: Crop Image Using Outside of the Image

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

Re: Crop Image Using Outside of the Image

Post 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
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 Image Using Outside of the Image

Post 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.
tugrul
Posts: 3
Joined: 2017-11-02T04:18:09-07:00
Authentication code: 1152

Re: Crop Image Using Outside of the Image

Post 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');

?>
Post Reply