Page 1 of 1

Resizing with Extent

Posted: 2009-06-08T14:13:50-07:00
by Bigus
Hi. I want to take a series of images and resize them all to 540 x 405 pixels wide. The images may not always be in the same proportion but I want to preserve the aspect ratio and fill any gaps with a background colour. So I have this code:

Code: Select all

my $dir = 'g:/development/';
use Image::Magick;
my $image = Image::Magick->new;
$image->read($dir.'DSC_4233a.jpg');
my($owidth,$oheight) = (540,405);
my $width = 540;
my $height = 405;
($origw, $origh) = $image->Get('width', 'height');
if($origw >= $origh){
	$height = $width * $origh / $origw;
}
else{
	$width = $height * $origw / $origh;
}
$image->Resize(width=>$width, height=>$height);
$image->Extent(gravity=>'center', width=>$owidth, height=>$oheight, background=>'black');
$image->Write( $dir.'resized.jpg' );
The image used for test actually came out at about 540x355 resized, so needed 50 pixels of padding. However the padding all appeared at the bottom of the image, in other words it wasn't vertically centered.

Any ideas what I've done wrong in the code?

Thanks

Re: Resizing with Extent

Posted: 2009-06-08T16:13:14-07:00
by magick
You found a bug. We'll have a patch in ImageMagick Subversion trunk by sometime tomorrow. Thanks.

Re: Resizing with Extent

Posted: 2009-06-09T00:18:16-07:00
by Bigus
OK awesome :-)

I've not used the subversion trunk before and am running Perl on Windows XP. Is it easy to update Perl Magick on WIN32 with sucha patch, or do I need to compile things?

Thanks
Bigus

Re: Resizing with Extent

Posted: 2009-06-09T05:51:13-07:00
by magick
The patch will be available in the next point release of ImageMagick within a week. If you can't wait, download ImageMagick 6.5.3-4 Beta and compile PerlMagick yourself:
  • cd PerlMagick
    perl Makefile.PL
    nmake
    nmake install

Re: Resizing with Extent

Posted: 2009-06-10T05:46:04-07:00
by Bigus
OK thanks. Looks like I'll have to wait for the point release as when I first tried to use nmake (v1.5) I got an error about the -W needing a parameter in CCFLAGS in the makefile. I changed it to -w which is allowed on it's own and turns off warnings but compiling failed again - this time with:

Code: Select all

        cl -c  -I../ -I.. -I/usr/include/freetype2 -I/usr/include/libxml2  -nologo -GF -W3 -MD -Zi -DNDEBUG -O1 -DWIN32 -D_CONSOLE -DNO_STRI
CT -DHAVE_DES_FCRYPT -DNO_HASH_SEED -DUSE_SITECUSTOMIZE -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -DPERL_MSVCRT_READFIX -fope
nmp -g -O2 -Wall -w -pthread -MD -Zi -DNDEBUG -O1    -DVERSION=\"6.5.3\"  -DXS_VERSION=\"6.5.3\"  "-IC:\Perl\lib\CORE"  -D_LARGE_FILES=1 -DH
AVE_CONFIG_H Magick.c
cl : Command line warning D9025 : overriding '/O1' with '/O2'
cl : Command line warning D9025 : overriding '/W3' with '/w'
cl : Command line warning D9025 : overriding '/O2' with '/O1'
cl : Command line warning D9002 : ignoring unknown option '-fopenmp'
cl : Command line warning D9002 : ignoring unknown option '-g'
cl : Command line warning D9002 : ignoring unknown option '-pthread'
Magick.c
G:\extract\ImageMagick\magick/monitor.h(33) : error C2054: expected '(' to follow 'inline'
G:\extract\ImageMagick\magick/monitor.h(35) : error C2085: 'QuantumTick' : not in formal parameter list
G:\extract\ImageMagick\magick/monitor.h(35) : error C2143: syntax error : missing ';' before '{'
G:\extract\ImageMagick\magick/quantum.h(78) : error C2054: expected '(' to follow 'inline'
G:\extract\ImageMagick\magick/quantum.h(79) : error C2085: 'RoundToQuantum' : not in formal parameter list
G:\extract\ImageMagick\magick/quantum.h(79) : error C2143: syntax error : missing ';' before '{'
G:\extract\ImageMagick\magick/quantum.h(105) : error C2054: expected '(' to follow 'inline'
G:\extract\ImageMagick\magick/quantum.h(106) : error C2085: 'ScaleQuantumToChar' : not in formal parameter list
G:\extract\ImageMagick\magick/quantum.h(106) : error C2143: syntax error : missing ';' before '{'
Magick.xs(536) : error C2065: '__func__' : undeclared identifier
Magick.xs(5234) : error C2065: 'ssize_t' : undeclared identifier
Magick.xs(5234) : error C2146: syntax error : missing ';' before identifier 'count'
Magick.xs(5234) : error C2065: 'count' : undeclared identifier
Magick.xs(5237) : error C2143: syntax error : missing ';' before 'type'
Magick.xs(5240) : error C2275: 'SV' : illegal use of this type as an expression
        C:\Perl\lib\CORE\perl.h(2088) : see declaration of 'SV'
Magick.xs(5240) : error C2065: 'av_reference' : undeclared identifier
Magick.xs(5241) : error C2065: 'perl_exception' : undeclared identifier
Magick.xs(5241) : error C2100: illegal indirection
Magick.xs(5242) : error C2065: 'reference' : undeclared identifier
Magick.xs(5242) : error C2100: illegal indirection
Magick.xs(5245) : error C2143: syntax error : missing ';' before 'type'
Magick.xs(5257) : error C2223: left of '->sv_any' must point to struct/union
Magick.xs(5261) : error C2065: 'info' : undeclared identifier
Magick.xs(5272) : error C2065: 'number_colors' : undeclared identifier
Magick.xs(5305) : error C2223: left of '->sv_any' must point to struct/union
Magick.xs(12337) : error C2146: syntax error : missing ';' before identifier 'count'
Magick.xs(12340) : error C2143: syntax error : missing ';' before 'type'
Magick.xs(12343) : error C2275: 'SV' : illegal use of this type as an expression
        C:\Perl\lib\CORE\perl.h(2088) : see declaration of 'SV'
Magick.xs(12344) : error C2100: illegal indirection
Magick.xs(12345) : error C2100: illegal indirection
Magick.xs(12357) : error C2223: left of '->sv_any' must point to struct/union
Magick.xs(12494) : error C2223: left of '->sv_any' must point to struct/union
NMAKE : fatal error U1077: 'E:\productivity\VisualStudio2005\VC\BIN\cl.EXE' : return code '0x2'
Stop.
Not being a C programmer, I haven't much of an idea what's going on there the compiler obviously isn't happy :-(