Convert image to fit fixed size, but black?

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 fit fixed size, but black?

Post by ultranerds »

Hi,

I've got a bit of a weird one here

I'm running the following command:

`mogrify -resize 100x100 -background white -gravity center -extent 100x100 -format jpg -quality 75 "$WriteImage"`;

This is the image before: http://designerstaticfiles.com.nmsrv.com/before.jpg
And this is it after: http://designerstaticfiles.com.nmsrv.com/test.jpg

For some reason the suypposidly "white" background comes out as blak ... any ideas? (oh, and it doesn't seem to centralize the image either, so not sure whats going on :/)

TIA

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

Re: Convert image to fit fixed size, but black?

Post by Bonzo »

What version of Imagemagick are you using?
Worked OK for me with convert - does it work for you using convert?
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Convert image to fit fixed size, but black?

Post by ultranerds »

Hi,

Thanks for your reply. I've emailed the guy to ask him to send me SSH access. I can't check version numbers without :(

He told me that he installed this lot, but didn't say what versions:

imagemagick
imagemagick-C++
imagemagick-C++-devel
imagemagick-devel
imagemagick-perl

Should my command above also work with "convert" instead of "mogrify"? I was just using some examples I found online <G>

Cheers

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

Re: Convert image to fit fixed size, but black?

Post by Bonzo »

You can try hard coding everything to see if it works:

Code: Select all

convert input.jpg -resize 100x100 -background white -gravity center -extent 100x100 -format jpg -quality 75 $WriteImage
If you are using php check out my site for some examples - link below.

Using php you can find the version using:

Code: Select all

<?php
echo "<pre>";
system("convert -version");  
echo "</pre>";
?>
 
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Convert image to fit fixed size, but black?

Post by ultranerds »

Hi,

Thanks for the reply. Its coming up with:

Version: ImageMagick 6.2.8 02/22/12 Q16 file:/usr/share/ImageMagick-6.2.8/doc/index.html
Copyright: Copyright (C) 1999-2006 ImageMagick Studio LLC

I've also tried using that syntax with "convert", and that also has the black stuff showing (which is why I'm getting confused as to whats going on :/)

TIA

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

Re: Convert image to fit fixed size, but black?

Post by Bonzo »

I would guess your version is to old; try:

Code: Select all

convert -size 100x100 xc:red before.jpg -resize 100x100 -unsharp 1.5x1+0.7+0.02 -gravity center -composite -quality 75 output1.jpg
Added some sharpening as well and for some reason -background would not work so had to use xc:
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert image to fit fixed size, but black?

Post by fmw42 »

According to http://www.imagemagick.org/Usage/crop/#extent

"Before IM version v6.3.2, "-extent" just cleared the memory of any new areas to zero, or straight black. It did not fill the areas with "-background" color. ... Also after IM v6.3.2, "-extent" will use the "-gravity" to define where the areas added/removed are positioned relative to the original image."

Your version 6.2.8 is too old to use extent and get the background and centering you need.

try

convert -size 100x100 xc:white <yourimage> -resize 100x100 -gravity center -compose over -composite -quality 75 <resultimage>
Last edited by fmw42 on 2012-04-02T10:22:46-07:00, edited 1 time in total.
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Convert image to fit fixed size, but black?

Post by ultranerds »

YOU LEGEND! I was literally pulling my hair out over this one (whats left of it ;)) ... your solution works like a charm. Thanks for taking the time to help me out 8)

Cheers

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

Re: Convert image to fit fixed size, but black?

Post by anthony »

ultranerds wrote:Hi,

Thanks for your reply. I've emailed the guy to ask him to send me SSH access. I can't check version numbers without :(

He told me that he installed this lot, but didn't say what versions:

imagemagick
imagemagick-C++
imagemagick-C++-devel
imagemagick-devel
imagemagick-perl

Should my command above also work with "convert" instead of "mogrify"? I was just using some examples I found online <G>

Cheers

Andy
That looks like 'package' installations for a linux system (like fedora, redhat, centos) these system are often out of date, especiually the larger servers whcih don't update packages, as they prefer 'stablity' as in 'old'.

Very old versions of extent did not provide background color, or gravity usage.

The equivelent is to generate a background image yourself and center compose the image over it. Just such a method was shown by Fred (see previous post).

However as typically you want to preverse the images meta-data (labels, comments, camera profiles) other, less direct, methods are typically used. These are exampled in IM Examples, Thumbnails, Padding.
http://www.imagemagick.org/Usage/thumbnails/#pad
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: Convert image to fit fixed size, but black?

Post by ultranerds »

Hi,

Thanks for the reply. Yeah the person isn't very server savvy, and therefore just uses Plesk/CPanel to install this kinda stuff. All working now, so all good 8)

Cheers

Andy
Post Reply