Imagemagick create thumbnail

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
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Imagemagick create thumbnail

Post by chaoscarnage »

I have an image that is 200x200 I want to trim the white space, and make it into an icon that is 32x32.

Code: Select all

convert img.png -trim -resize 32x32 imgi.png
This line of code does not work, it will not make the canvas a uniform 32x32. I have tried taking it and adding -canvas 32x32 or -extend 32x32 but then the script does not work at all.

I am doing this from PHP with exec so I do not get an error back.

Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Imagemagick create thumbnail

Post by Bonzo »

Have you tried +repage ?

Code: Select all

convert img.png -trim -resize 32x32 +repage  imgi.png

chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Imagemagick create thumbnail

Post by chaoscarnage »

I tested repage with both resize and thumbnail. In both cases the image canvas is coming out 9x32. Same was without repage. I read though the examples to come up with what I have now. Trim takes out all of the transparent space, but then the canvas does not reset to 32x32 with the resize.

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

Re: Imagemagick create thumbnail

Post by fmw42 »

If the white space is not trimmed evenly, then the image will not be square after the trim and your resize will not end up square. You can force it to be 32x32 exactly if you are willing to allow for some distortion from its actual aspect ratio by adding !. Depending upon your OS, you may need to escape the ! or put the argument into double quotes.

convert img.png -trim -resize 32x32! imgi.png

chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Imagemagick create thumbnail

Post by chaoscarnage »

I tried that as well and it stretches the result to fit the size. I just want to have the same image but on a 32x32 canvas so all my thumbnails are 32x32.

snibgo
Posts: 12901
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Imagemagick create thumbnail

Post by snibgo »

Offering advice is far easier if we can see the image.

If the trimmed image isn't square, you must decide what you want to do: trim some of the image, or add some blank to make it square, or stretch the image.
snibgo's IM pages: im.snibgo.com

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

Re: Imagemagick create thumbnail

Post by fmw42 »

you can fill it with transparency. Your trim is removing a lot of width so that is why it is 9x32.

convert image.png -trim +repage -resize 32x32 -gravity center -background none -extent 32x32 result.png
Last edited by fmw42 on 2013-06-15T14:38:58-07:00, edited 1 time in total.

chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Imagemagick create thumbnail

Post by chaoscarnage »

snibgo wrote:Offering advice is far easier if we can see the image.

If the trimmed image isn't square, you must decide what you want to do: trim some of the image, or add some blank to make it square, or stretch the image.
We want to add some transparency to make it square.

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

Re: Imagemagick create thumbnail

Post by fmw42 »

We want to add some transparency to make it square.
See my post just above yours. We must have posted at about the same time.

chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Imagemagick create thumbnail

Post by chaoscarnage »

fmw42 wrote:
We want to add some transparency to make it square.
See my post just above yours. We must have posted at about the same time.
That appears to have worked perfectly. Thank you.

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

Re: Imagemagick create thumbnail

Post by fmw42 »

if you don't like the centering, you can change the gravity setting so that it might end up more aligned as it was originally

chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Imagemagick create thumbnail

Post by chaoscarnage »

fmw42 wrote:you can fill it with transparency. Your trim is removing a lot of width so that is why it is 9x32.

convert image.png -trim +repage -resize 32x32 -gravity center -background none -extent 32x32 result.png
POST trim, If the icon is SMALLER than 32x32, I just want to extend and not resize, is there a way to tell it that

User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Imagemagick create thumbnail

Post by GreenKoopa »

-resize and its geometry argument is very flexible. Try adding the shrink only flag

convert image.png -trim +repage -resize 32x32> -gravity center -background none -extent 32x32 result.png

The > character may need to be quoted or escaped on your platform. For other flags see:
http://www.imagemagick.org/script/comma ... p#geometry

chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Imagemagick create thumbnail

Post by chaoscarnage »

GreenKoopa wrote:-resize and its geometry argument is very flexible. Try adding the shrink only flag

convert image.png -trim +repage -resize 32x32> -gravity center -background none -extent 32x32 result.png

The > character may need to be quoted or escaped on your platform. For other flags see:
http://www.imagemagick.org/script/comma ... p#geometry
That's exactly what I needed thank you very much.

Post Reply