Help: Image::Magick::Thumbnail

Ask a Question related to PERL Modules, Design and Development.

  1. #1

    Default Help: Image::Magick::Thumbnail

    Hi,

    The following example (from cpan.org) take an .jpg image and creates a
    thumbnail.
    It creates an image where the biggest size is 50 pixels.

    Can this code be easily modified to make all sides 50 pixels (ie square
    image)?

    I've tried the following, but it doesn't create any image.
    my ($thumb,$x,$y) = Image::Magick::Thumbnail::create(50,50);
    my ($thumb,50,50) = Image::Magick::Thumbnail::create($src,50);

    As a newbie I've tried without success.

    Thanks


    use Image::Magick::Thumbnail;
    # Load your source image
    my $src = new Image::Magick;
    $src->Read('source.jpg');

    # Create the thumbnail from it, where the biggest side is 50 px
    my ($thumb,$x,$y) = Image::Magick::Thumbnail::create($src,50);

    # Save your thumbnail
    $thumb->Write('source_thumb.jpg');


    cb Guest

  2. Similar Questions and Discussions

    1. Image Magick thumbnail batch resizing
      Spartacus <spam@mailinator.com> writes: I think you want to scale instead of sample do convert -quality 85 -scale x75 $i ../thumbs/`basename $i...
    2. Image::Magick::Thumbnail::create problem
      I am using a script based on the example from www.cpan.org for Image::Magick::Thumbnail:- #!/usr/bin/perl -w use CGI qw(:all); print header;...
    3. Thumbnail image in Flash to larger image in new window?
      I have a series of thumbnail images in Flash and I want the user to be able to click on the thumbnail and get a larger photo. I know how to make the...
    4. Image Magick
      Mike At Spy wrote: the easiest way to remove them is to use RPM, if that was what was used to install them rpm -e ImageMagick if there are no...
    5. [PHP] Image Magick
      Mike -- ...and then Mike At Spy said... % % Anyone here familiar with the installation of Image Magick and what filesit % installs on your...
  3. #2

    Default Re: Image::Magick::Thumbnail

    "cb" <cb2005uk@yahoo.co.uk> wrote in message
    news:402bd388$0$3942$cc9e4d1f@news.dial.pipex.com. ..
    > Hi,
    >
    > The following example (from cpan.org) take an .jpg image and creates a
    > thumbnail.
    > It creates an image where the biggest size is 50 pixels.
    >
    > Can this code be easily modified to make all sides 50 pixels (ie square
    > image)?
    >
    > I've tried the following, but it doesn't create any image.
    > my ($thumb,$x,$y) = Image::Magick::Thumbnail::create(50,50);
    this fails because the first argument should be a Image::Magick object
    > my ($thumb,50,50) = Image::Magick::Thumbnail::create($src,50);
    this fails because you are trying to assing the returned values to
    the constant 50.
    >
    > As a newbie I've tried without success.
    >
    > Thanks
    >
    >
    > use Image::Magick::Thumbnail;
    > # Load your source image
    > my $src = new Image::Magick;
    > $src->Read('source.jpg');
    >
    > # Create the thumbnail from it, where the biggest side is 50 px
    > my ($thumb,$x,$y) = Image::Magick::Thumbnail::create($src,50);
    just read the Image::Magick docs (see
    [url]http://www.imagemagick.org/www/perl.html[/url])
    and add an appropriate operation here. for example
    $thumb->Resize(width=>50,height=>50);
    >
    > # Save your thumbnail
    > $thumb->Write('source_thumb.jpg');

    looks like Image::Magick::Thumbnail is pretty simple (and useless).
    you probably should drop it altogether.

    use Image::Magick;
    my $src = new Image::Magick;
    $src->Read('source.jpg');
    $src->Resize(width=>50,height=>50);
    $src->Write('source_thumb.jpg');


    of course if you resize the image to 50x50 and it was not
    square to begin with, it will stretch.

    gnari




    gnari Guest

  4. #3

    Default Re: Image::Magick::Thumbnail

    use Image::Magick ;

    my $input_img = ... ;
    my $output_img = ... ;

    my $max_dimension = 50 ;
    my $fill_color = '#ffffff' ;

    my $image = Image::Magick->new ;
    $image->Read($input_img) ;
    my $width = $image->Get('columns') ;
    my $height = $image->Get('height') ;

    $image->Scale(geometry=>"$max_dimension x $max_dimension") if $width > $max_dimension ;
    $image->Scale(geometry=>"$max_dimension x $max_dimension") if $height > $max_dimension ;

    my $border_width = int(0.5 + ($max_dimension - $image->Get('columns'))/2) ;
    my $border_height = int(0.5 + ($max_dimension - $image->Get('height'))/2) ;
    $image->Border(geometry=>"$max_dimension x $max_dimension",
    width=>$border_width, height=>$border_height, fill=>$fill_color);
    $image->Crop(geometry=>"$max_dimension x $max_dimension+0+0");

    $image->Write($output_img);


    thinkfirst Guest

  5. #4

    Default Re: Help: Image::Magick::Thumbnail

    On Thu, 12 Feb 2004 19:22:37 -0000,
    cb <cb2005uk@yahoo.co.uk> wrote:
    > Hi,
    >
    > The following example (from cpan.org) take an .jpg image and creates a
    > thumbnail.
    > It creates an image where the biggest size is 50 pixels.
    >
    > Can this code be easily modified to make all sides 50 pixels (ie square
    > image)?
    >
    > I've tried the following, but it doesn't create any image.
    > my ($thumb,$x,$y) = Image::Magick::Thumbnail::create(50,50);
    > my ($thumb,50,50) = Image::Magick::Thumbnail::create($src,50);
    use Image::Magick;

    my $im = Image::Magick->new();
    $im->Read($f);
    $im->Scale(geometry => "50x50!");

    You can read about the geometry values in the man page ImageMagick(1),
    or in your locally installed documentation in HTML format, or at
    [url]http://www.imagemagick.org/www/utilities.html[/url]

    This will distort the image if it isn't square already. If you don't
    want that, you will have to be more clear about what you want to
    happen: Do you want the smallest dimension of the image to be scaled
    to 50 pixels, and the rest cropped off? Do you want the largest
    dimension to be scaled to 50, and the rest filled up?

    Martien
    --
    |
    Martien Verbruggen | That's not a lie, it's a terminological
    Trading Post Australia | inexactitude.
    |
    Martien Verbruggen Guest

  6. #5

    Default Re: Help: Image::Magick::Thumbnail

    On 16 Feb 2004 05:38:29 GMT,
    Martien Verbruggen <mgjv@tradingpost.com.au> wrote:
    > On Thu, 12 Feb 2004 19:22:37 -0000,
    > cb <cb2005uk@yahoo.co.uk> wrote:
    [snip request for thumbnails of exactly 50x50, and one possible
    solution, distorting aspect ratio]
    > This will distort the image if it isn't square already. If you don't
    > want that, you will have to be more clear about what you want to
    > happen: Do you want the smallest dimension of the image to be scaled
    > to 50 pixels, and the rest cropped off? Do you want the largest
    > dimension to be scaled to 50, and the rest filled up?
    Here's two others that don't distort aspect ratio:

    # resize smallest dimension, and crop
    my $im = Image::Magick->new();
    $im->Read($f);
    my ($w, $h) = $im->Get("width", "height");
    my $geometry = $w > $h ? "x50" : "50";
    $im->Scale(geometry => $geometry);
    ($w, $h) = $im->Get("width", "height");
    $im->Crop(width => 50, height => 50, x => ($w - 50)/2, y => ($h - 50)/2);

    # resize largest dimension, and fill
    my $im = Image::Magick->new();
    $im->Read($f);
    $im->Scale(geometry => "50x50");
    my $im2 = Image::Magick->new(size => "50x50");
    $im2->Read("xc:black");
    $im2->Composite(image => $im, compose => "Over", gravity => "Center");

    You should probably put some error checking in.

    Martien
    --
    |
    Martien Verbruggen | Useful Statistic: 75% of the people make up
    Trading Post Australia | 3/4 of the population.
    |
    Martien Verbruggen Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139