【问题标题】:PIL: enlarge an imagePIL:放大图像
【发布时间】:2011-07-12 04:22:39
【问题描述】:

我无法让 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)))

【问题讨论】:

  • 能否请您写下您是如何读取图像文件的?

标签: python resize transform python-imaging-library


【解决方案1】:

这是一个如何使用 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(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”缩小图像(因子

【讨论】:

  • OP 询问了关于在 PIL 框架中调整大小的问题
【解决方案2】:

对于阅读本文的任何人,遇到同样的问题 - 在另一台机器上尝试。我两个都有

im = im.resize(size_tuple)

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

正确调整文件大小。我的服务器上的python安装一定有问题。在我的本地机器上运行良好。

【讨论】:

    猜你喜欢
    • 2011-10-31
    • 2011-06-21
    • 2012-12-01
    • 1970-01-01
    • 2018-05-13
    • 2017-04-01
    • 1970-01-01
    • 2021-08-01
    • 2021-06-27
    相关资源
    最近更新 更多