imageperlimage-processingimagemagickperlmagick

Scaling of image to fixed width but not preserve the aspect ratio


I want to scale an image that way that the resulting image got a fixed width, but the heigth of the image stays the same. How can this be done?


Solution

  • You can do this at the command line:

    convert input.jpg -resize 3x\! output.jpg
    

    The ! forces the resize, and leaving the height blank leaves it unaffected.

    # Create it 50x50 and check
    convert -size 50x50 xc:black  a.jpg
    identify a.jpg
    a.jpg JPEG 50x50 50x50+0+0 8-bit Gray 256c 173B 0.000u 0:00.009
    
    # Resize and check
    convert a.jpg -resize 3x\! out.jpg
    identify out.jpg
    out.jpg JPEG 3x50 3x50+0+0 8-bit Gray 256c 162B 0.000u 0:00.000
    

    And a Perl version of a similar thing:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Image::Magick;
    my $image;
    
    $image=Image::Magick->new(size=>'500x500');
    $image->Read('xc:white');
    $image->write("out1.jpg");
    $image->Resize(geometry => "3x!");
    $image->write("out2.jpg");