Search code examples
imageperlimage-resizingimlib2

What is the difference between cropping, resizing and scaling an image?


I am using Perl's Image::Imlib2 package to generate thumbnails from larger images.

I've done such tasks before with several ImageMagick interfaces (PHP, Ruby, Python) and it was relatively easy. I have no prior experience with Imlib2 and it is a long time since I wrote something in Perl, so I am sorry if this seems naive!

This is what I've tried so far. It is simple, and assumes that scaling an image will keep the aspect ratio, and the generated thumbnail will be an exact miniature copy of the original image.

use strict;
use warnings;

use Image::Imlib2;

my $dir = 'imgs/*';

my @files = glob ($dir);

foreach my $img ( @files ) {
    my $image = Image::Imlib2->load($img);
    my $cropped_image = $image->create_scaled_image(50, 50);
    $cropped_image->save($img);
}

Original image

Original image

Generated image

Generated image

My first look at the image tells me that something is wrong. It may be my ignorance on cropping, resizing and scaling, but the generated image is displaying wrongly on small screens.

I've read What's the difference between cropping and resizing?, and honestly didn't understand anything. Also this one Image scaling.

Could someone explain the differences between those three ideas, and if possible give examples (preferably with Perl) to achieve better results? Or at least describe what I should consider when I want to create thumbnails?


Solution

  • The code you use isn't preserving the aspect-ratio. From Image::Imlib2::create_scaled_image

    If x or y are 0, then retain the aspect ratio given in the other.

    So change the line

    my $cropped_image = $image->create_scaled_image(50, 50);
    

    to

    my $scaled_image  = $image->create_scaled_image(50, 0);
    

    and the new image will be 50 pixels wide, and its height computed so to keep the original aspect-ratio.

    Since this is not cropping I've changed the variable name as well.

    As for other questions, below is a basic discussion from comments. Please search for tutorials on image processing. Also, documentation of major libraries often have short and good explanations.


    This is aggregated from comments deemed helpful. Also see Borodin's short and clear answer.

    Imagine that you want to draw a picture (of some nice photograph) yourself in the following way. You draw a grid of, say, 120 (horizontally) by 60 (vertically) boxes. So 120 x 60, 720 boxes. These are your "pixels," and each you may fill with only one color. If the photo you are re-drawing is "mostly" blue at some spot, you color that pixel blue. Etc. It is not easy to end up with a faithful redrawing -- the denser the pixels the beter.

    Now imagine that you want to draw another copy of this, just smaller. If you make it 20x20 that will be completely different, since it's a square. The best chance of getting it to "look the same" is to pick 2-to-1 ratio (like 120x60), so say 40x20. That's "aspect-ratio." But there is still a problem, since now you have to decide all over again what color to pick for each box, so to represent what is "mostly" on the photo at that spot. There are algorithms for that ("sampling," see your second link). That's involved with "resizing." The "quality" of the obtained drawing clearly must be much worse.

    So "resizing" isn't all that simple. But, for us users, we mostly need to roughly know what is involved, and to find out how to use these features in a library. So read documentation. Some uses are very simple, while sometimes you'll have to decide which "algorithm" to let it use, or some such. Again, what I do is read manuals carefully.

    The basic version of "cropping" is simple -- you just cut off a part of the picture. Say, remove the first and last 20 columns and the bottom and top 10 rows, and from the initial 120x60 you get a picture of 80x40. This is normally done when outer parts of an image have just white areas (or, worse, black!). So you want to "cut out" the picture itself from the whole image. Many graphics tools can do that on their own, by analyzing the image and figuring out those areas. Or, we select and hit a button.