首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PIL:放大图像

PIL:放大图像
EN

Stack Overflow用户
提问于 2011-03-10 03:19:49
回答 2查看 23.2K关注 0票数 8

我很难让PIL放大图像。大图像可以很好地缩小,但小图像不会变大。

代码语言:javascript
复制
# 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)))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-10 21:13:44

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

代码语言:javascript
复制
im = im.resize(size_tuple)

代码语言:javascript
复制
im = im.transform(size_tuple, Image.EXTENT, (x1,y1,x2,y2)

正确调整文件大小。我的服务器上的python安装肯定有什么问题。在我的本地机器上工作正常。

票数 19
EN

Stack Overflow用户

发布于 2014-04-22 06:30:10

下面是一个使用openCV和numpy在每个方向上调整图像大小的工作示例:

代码语言:javascript
复制
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(original_image, (int(original_height*factor), int(original_width*factor)), interpolation=cv2.INTER_CUBIC )

cv2.imwrite('resized_image.jpg',resized_image)
#fixed var name

就这么简单。你想使用"cv2.INTER_CUBIC“来放大(因子> 1),使用"cv2.INTER_AREA”来缩小图像(因子< 1)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5251010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档