Crop problem with 'convert'

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
chris2011
Posts: 5
Joined: 2011-06-20T00:47:22-07:00
Authentication code: 8675308

Crop problem with 'convert'

Post by chris2011 »

Hi there,

I have image with the dimension 170x100 (width and height).

Now I want to call:

convert -crop 500x100+0+0 mypic.jpg -gravity Center newmypic.jpg

The new image has the dimension 170x100 (width and height).

How to force the convert tool to add white space that the new image has the correct dimensons (500x100) without resizing etc.?

Thanks
Chris
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Crop problem with 'convert'

Post by anthony »

First read the image before applying any operation.

Second use the setting BEFORE the operation(s) it applies to.

Essentially IM will do operations in the order they are seen. Do not thing of the arguments as 'options' but as operations and settings (reading a image is a operation). The only reason it worked as you had it is due to 'legacy handling' left over from IMv5. In IMv7 performing operations on images that has not been read in yet will produce an error.

This is explained in IM Examples, Basics
http://www.imagemagick.org/Usage/basics/

Now to do what you want first -crop the image to remove excess, then use the same dimensions with -extent to 'pad out' the image with the current background color setting.

Code: Select all

convert mypic.jpg -gravity Center -crop 500x100+0+0 -background white -extent 500x100+0+0  newmypic.jpg
See IM Examples, Thumbnails, Padding Images
http://www.imagemagick.org/Usage/thumbnails/#pad

Warning, reading and then saving the image again as JPEG will cause some slight degrading of image quality. This is caused by the JPG compression, and it not something IM can do anything about. It is recommended that if quality is important to save to PNG or some other non-lossy file format. It will take more disk space though, that is the trade off.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Crop problem with 'convert'

Post by anthony »

Apologies, after a little extra thought I realised that -extent can not only be used to pad, but it can crop too. It was after all one of its original tasks. As such you can think of it as a 'padded crop'.

Try just replacing the -crop with the -extent (But add the background color setting).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
chris2011
Posts: 5
Joined: 2011-06-20T00:47:22-07:00
Authentication code: 8675308

Re: Crop problem with 'convert'

Post by chris2011 »

Thanks Anthony,

I tried: http://shp.f68.de/cmpic.php

But there is only a black border on the right. I inlcuded in the link a picture what the result should be - even the difference between the -crop and the -extent

Thanks so much. I'm now trying & reading a couple of hours but I can't find a solution.

Chris
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop problem with 'convert'

Post by fmw42 »

First, -extent does not allow offsets nor is it -geometry sensitive (but is gravity sensitive). Second you need to put -background before -extent

So try

convert p454a.jpg -gravity center -background white -extent 500x200 p454a_tmp1.jpg

see http://www.imagemagick.org/Usage/crop/#extent

If you need an unequal border, then you will need to crop the image first, then pad it using -splice (see http://www.imagemagick.org/Usage/crop/#splice)

convert p454a.jpg -gravity center -crop 450x200+0+0 -gravity west -splice 20x0 -gravity east -splice 30x0 p454a_tmp2.jpg
chris2011
Posts: 5
Joined: 2011-06-20T00:47:22-07:00
Authentication code: 8675308

Re: Crop problem with 'convert'

Post by chris2011 »

Thanks for your help.

But please have a look at the result: http://shp.f68.de/cmpic.php

On the right is an black area. Nothing white and only on the right. Thats all. Please have a look at the second picture.
I want an image which is inside the borders with the white areas. But the 1000 tries won't bring be near to an acceptable result .... :-?

Thanks
Chris
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop problem with 'convert'

Post by fmw42 »

My commands above work perfectly on IM 6.7.0.8 Q16 Mac OSX tiger.

convert p454a.jpg -gravity center -background white -extent 500x200 p454a_tmp1.jpg

Image

convert p454a.jpg -gravity center -crop 450x200+0+0 -background white -gravity west -splice 20x0 -gravity east -splice 30x0 p454a_tmp2.jpg

Image

just to be sure I added -background white before the splice so that it splices white on the left and right.

Do you want a black border around it? If so, then

convert p454a.jpg -gravity center -crop 450x200+0+0 -background white -gravity west -splice 20x0 -gravity east -splice 30x0 -bordercolor black -border 2 p454a_tmp3.jpg

Image

You never said what version of IM you are using nor what platform. If old, then that could explain the problem and you may have to upgrade or find another approach, such as

convert -size 20x200 xc:white \( p454a.jpg -gravity center -crop 450x200+0+0 \) -size 30x200 xc:white +append p454a_tmp4.jpg

Image
chris2011
Posts: 5
Joined: 2011-06-20T00:47:22-07:00
Authentication code: 8675308

Re: Crop problem with 'convert'

Post by chris2011 »

thats astonishing! you see the results with the weblink http://shp.f68.de/cmpic.php ?

thats convert version

Version: ImageMagick 6.2.4 07/28/09 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2005 ImageMagick Studio LLC

is this version to old?

thanks
chris
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop problem with 'convert'

Post by fmw42 »

It is over 400 versions old. I would recommend you upgrade. That will solve lots of problems and give you many new features.

see
http://www.imagemagick.org/script/binary-releases.php
and
http://www.imagemagick.org/download/www ... .html#unix

What platform are your using?


However, try my last method. That should work for your old system, so long as crop works. I am assuming +append was around in that old system.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Crop problem with 'convert'

Post by anthony »

-extent does work with argument offsets. But will assume offsets are zero if not given.

However when using offsets with center and side padding, you loose that offset on one side and gain it again on the otherside.


I also recommend you still use a +repage if input images can posibilby contain a virtual offset (which -extent ignores), as I am seeing a little bit of screwy behaviour in the output image offset (though no different in the actual image result, as expected. I don't think it was tested against images with virtual offsets. -- I have made a note to check this out later.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply