PIL:放大图像

问题描述:

我无法让PIL放大图像。大图像缩小就好,但小图像不会变大。PIL:放大图像

# get the ratio of the change in height of this image using the 
# by dividing the height of the first image 
s = h/float(image.size[1]) 
# calculate the change in dimension of the new image 
new_size = tuple([int(x*s) for x in image.size]) 
# if this image height is larger than the image we are sizing to 
if image.size[1] > h: 
    # make a thumbnail of the image using the new image size 
    image.thumbnail(new_size) 
    by = "thumbnailed" 
    # add the image to the images list 
    new_images.append(image) 
else: 
    # otherwise try to blow up the image - doesn't work 
    new_image = image.resize(new_size) 
    new_images.append(new_image) 
    by = "resized" 
logging.debug("image %s from: %s to %s" % (by, str(image.size), str(new_size))) 
+0

你能否也请写下你是如何读取图像文件的? – doniyor 2014-10-21 09:32:37

对于任何人读这篇文章,有同样的问题 - 尝试另一台机器上。我有两个

im = im.resize(size_tuple) 

im = im.transform(size_tuple, Image.EXTENT, (x1,y1,x2,y2) 

适当调整的文件。我的服务器上安装python肯定有问题。在我的本地机器上工作良好。

我认为是因为这条线。您需要将调整大小的图像存储在新对象中。

image.thumbnail(new_size) 

应该

newimage = image.thumbnail(new_size) 
+2

缩略图不是问题,而是调整大小。缩略图编辑图像对象,调整大小返回副本。虽然谢谢! – 2011-03-09 20:30:06

这里是一个工作示例如何与OpenCV的和numpy的每一个方向调整图像大小:

import cv2, numpy 

original_image = cv2.imread('original_image.jpg',0) 
original_height, original_width = original_image.shape[:2] 
factor = 2 
resized_image = cv2.resize(originalImage, (int(original_height*factor), int(original_width*factor)), interpolation=cv2.INTER_CUBIC) 

cv2.imwrite('resized_image.jpg',resized_image) 

这么简单。您想使用“cv2.INTER_CUBIC”放大(因子> 1)和“cv2.INTER_AREA”以使图像变小(因子< 1)。

增加图像比减小尺寸更重要。缩小图像涉及获取现有像素并消除其中的一些像素。照片放大要求使现有像素代表其自身不存在的额外像素,以使图像看起来更大。我发现很少有可以做到这一点的软件产品。据报道真正的Fractiles能够,我发现了一种名为Imagener的产品。

+4

您能否提供一些代码来说明如何在python程序中使用这些软件产品来增加图像的大小?否则,关于改变图像大小的基本正确但非常普遍的观察并不能帮助回答问题。 – 2015-02-28 09:21:31