裁剪,调整大小和缩放图像有什么区别? [英] What is the difference between cropping, resizing and scaling an image?

查看:445
本文介绍了裁剪,调整大小和缩放图像有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Perl的
,老实说,什么都不懂。此 图像缩放



有人可以解释这三个想法之间的差异,如果可能的话,举例(最好用Perl)来取得更好的结果吗?或者至少描述当我想创建缩略图时我应该考虑什么?

解决方案

您使用的代码不保留方面-比。来自图片: :Imlib2 :: create_scaled_image


如果x或y为0,则保留另一个给出的宽高比。


因此更改行

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

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

并且新图像将是50像素宽,并且计算其高度以保持原始方面 - 比率。



由于这不是裁剪我也改变了变量名。



至于其他问题,以下是评论的基本讨论。请搜索有关图像处理的教程。此外,主要图书馆的文档通常有简短的解释。






这是从认为有帮助的评论中汇总而来的。另请参阅 Borodin 简短明了的答案。



想象一下,你想在下面自己画一幅图片(一张漂亮的照片)办法。您绘制一个120(水平)乘60(垂直)框的网格。所以120 x 60,720盒。这些是您的像素,每个只能填充一种颜色。如果您正在重新绘制的照片在某个位置大部分为蓝色,则将该像素设置为蓝色。等等。以忠实的重绘方式结束并不容易 - 比较粗糙的像素。



现在想象你想要绘制另一个这样的副本,只是小。如果你使它20x20将完全不同,因为它是一个正方形。让它看起来一样的最好机会是选择2:1的比例(如120x60),所以说40x20。这就是宽高比。但是仍然存在一个问题,因为现在你必须重新决定每个盒子要选择什么颜色,以便代表那个地方照片上的大部分。有算法(采样,见第二个链接)。这与调整大小有关。所获得的图纸的质量显然必须要差得多。



所以调整大小并不是那么简单。但是,对于我们的用户,我们大多需要大致了解所涉及的内容,并了解如何在库中使用这些功能。所以阅读文档。一些用途非常简单,有时你必须决定让它使用哪种算法,或者某些用法。同样,我所做的是仔细阅读手册



裁剪的基本版本很简单 - 你只是切断了部分图片。比如说,删除第一列和最后20列以及底部和前10行,从最初的120x60开始,您将获得80x40的图片。这通常在图像的外部部分仅具有白色区域(或者更糟,黑色!)时完成。因此,您希望从整个图像中剪切图片本身。许多图形工具可以通过分析图像并找出这些区域来自行完成。或者,我们选择并点击一个按钮。


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

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?

解决方案

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.

这篇关于裁剪,调整大小和缩放图像有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆