Convert image to a given size, BUT dont crop

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
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Convert image to a given size, BUT dont crop

Post by ultranerds »

Hi,

I'm trying to get an image to shrink to a given size (300x300). The command I normally use is:

Code: Select all

convert "/var/home/steveraf/steampunkjunkiesdev.net/www/new/img/blank300.gif" \\\( -define jpeg:size=600x600 "$path_orig" -thumbnail 600x600 \\\) -gravity center -composite "$path_orig"
However, that doesn't seem to work. I'm on IM 6.8.6-8 currently. The outcome I want, is a 600x600 image, which ISNT cropped, but can be resized + placed on a fixed background of the given dimensions

TIA!

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

Re: Convert image to a given size, BUT dont crop

Post by fmw42 »

I am confused. You say 300x300 in one place and then 600x600 in another place. What size are the two images and what size output do you want? Is blank300.gif a constant color or transparent?
convert "/var/home/steveraf/steampunkjunkiesdev.net/www/new/img/blank300.gif" \\\( -define jpeg:size=600x600 "$path_orig" -thumbnail 600x600 \\\) -gravity center -composite "$path_orig"
Why three \\\. You only need one before the parens on unix such as \( ... \)
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Convert image to a given size, BUT dont crop

Post by ultranerds »

Hi,

Sorry, that was out of my perl script. You have to double escape a \, otherwise it would be trying to escape the ( (and not be run as part of the command)

For the 300x300 but.. that was a typo =) I just need a 600x600, and then I'll be able to shrink it down to whatever I need (proportionatly)

This is the equiv command that gets run:

Code: Select all

convert "/var/home/steveraf/steampunkjunkiesdev.net/www/new/img/blank300.gif" \( -define jpeg:size=600x600 "$path_orig" -thumbnail 600x600 \) -gravity center -composite "$path_orig"
I don't get any errors - it just doesn't seem to do what its meant to :(

TIA

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

Re: Convert image to a given size, BUT dont crop

Post by fmw42 »

What size are the two images and what size output do you want? Is blank300.gif a constant color or transparent?

Try without the size hint or make the hint size larger than the thumbnail size.
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Convert image to a given size, BUT dont crop

Post by ultranerds »

Hi,

The example image can be found here:

http://steampunkjunkies.net/uploads/ori ... Adolv5.jpg

the blank300.gif is just a transparent gif, thats 600px wide (bit mis-leading, but it was from when we were doing it as a 300 :))

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

Re: Convert image to a given size, BUT dont crop

Post by fmw42 »

What do you mean by is not cropped? The aspect ratio does not support 600x600 without distortion. So you either resize so that the larger size is 600 and the other is less than 600 and pad with transparency or some other color or your resize so that the smaller size is 600 and the other is larger than 600 and crop to 600x600. There is no need to create a transparent background image.

The first one is this

Code: Select all

convert image.jpg -thumbnail 600x600 -background none -gravity center -extent 600x600 tmp1.png
The second is this

Code: Select all

convert image.jpg -thumbnail 600x600^ -gravity center -extent 600x600 tmp2.png
or

Code: Select all

convert image.jpg -thumbnail 600x600^ -gravity center -crop 600x600+0+0 +repage tmp2.png
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Convert image to a given size, BUT dont crop

Post by ultranerds »

You beauty - the first one worked like a charm :)

Thanks!

Andy
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Convert image to a given size, BUT dont crop

Post by ultranerds »

Me again :)

After doing conversions to PNG, I've decided that actually JPG's would be better for my site, as PNG's are stupidly large (and for just photos, jpg would be more suited)

I've got it all going now - but I'm having the normal issue with the PNG showing black where the transparency. I know why its doing this, but I'm a little confused as to why its doing it with this command:

convert "$read_path" -thumbnail ${height}x${width} -background "#ffffff" -gravity center -extent ${height}x${width} "$write_path"

I've tried it with this too:

convert "$read_path" -thumbnail ${height}x${width} -background white -gravity center -extent ${height}x${width} "$write_path"

I'm a bit baffled why its doing this? You can see an example here:

http://steampunkjunkies.net/475-BFujdLg ... hNoazD.jpg

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

Re: Convert image to a given size, BUT dont crop

Post by fmw42 »

I am confused by the question. Are you getting black in the output to PNG or for output to JPG. Note that JPG does not support transparency. If you are getting black where your input was transparent and the output is PNG, then please post a link to your input and output image and the exact command line using real image names.

If you want a transparent background for your code, then use

Code: Select all

convert "$read_path" -thumbnail ${height}x${width} -background none -gravity center -extent ${height}x${width} "$write_path"

or

convert "$read_path" -thumbnail ${height}x${width} -background "#00000000" -gravity center -extent ${height}x${width} "$write_path"
If you want a white background and have transparency in the input image, then try

Code: Select all

convert "$read_path" -thumbnail ${height}x${width} -background "FFFFFFFF" -gravity center -extent ${height}x${width} "$write_path"
Note I have specified the hex color with 8 values, the last two are for tranparency and in this case fully opaque

Note, also that you can write to PNG8:outputimage.png and get 8-bit color with binary transparency as a smaller file size, though not as small as jpg. But again jpg does not support transparency. If you need 8-bit transparency, then you need to write to 32-but PNG as PNG32:outputimage.png if it does not default when just using outputimage.png
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Convert image to a given size, BUT dont crop

Post by ultranerds »

Yeah, I'm aware that PNG doesnt support transparency - thus the black part

I ended up getting this working for me:

convert "$path/$filename" -resize 800x800 -size 800x800 xc:white +swap -compose over -composite "$path/$filename_new"

8)

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

Re: Convert image to a given size, BUT dont crop

Post by fmw42 »

Yeah, I'm aware that PNG doesnt support transparency - thus the black part
PNG support transparency. It is JPG that does not support transparency
Post Reply