gpt4 book ai didi

python - 错误模块'对象没有属性'LANCZOS

转载 作者:太空宇宙 更新时间:2023-11-03 15:53:07 25 4
gpt4 key购买 nike

我想将图像大小调整为 28*28 像素 .jpeg。我使用名为 PIL 的模块来调整这张图片的大小:我创建了这个类:

from PIL import Image

#import PIL
import numpy
from resizeimage import resizeimage

import scipy.misc

''' This class is to resize input image to MNIST size (28x28 px) '''


class Resize_img:
def __init__(self, imageName):
print 'Image -- ', imageName
self.resized_image = ''
# resize img to mnist size [28x28]
#with open(imageName,'r+b') as f:
# with Image.open(f) as image:
image = Image.open(imageName)
cover = resizeimage.resize_cover(image, [28, 28])
self.resized_image = 'new ' + imageName
cover.save(self.resized_image, image.format)
# transform img to MNIST form
# image to ndarray
PILimg = PIL.Image.open(self.resized_image)

self.mnist_image_input = scipy.misc.fromimage(PILimg,
True) # True => space gray! ----------------------------------------------------
self.mnist_image_input = (numpy.multiply(self.mnist_image_input,
1.0 / 255.0) - 1.0) * -1.0 # inverse the image :D ( white -> dark )


def main(): # To test this Class.
imageTest = '2.jpeg' # The name of the image to resize

imageTest = Resize_img(imageTest)
scipy.misc.imshow(imageTest.mnist_image_input)


if __name__ == "__main__":
main()


# sudo apt-get install libtiff4-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.5-dev tk8.5-dev python-tk

错误是:

Image --  2.jpeg
Traceback (most recent call last):
File "Resize_img.py", line 40, in <module>
main()
File "Resize_img.py", line 35, in main
imageTest = Resize_img(imageTest)
File "Resize_img.py", line 18, in __init__
with Image.open(f) as image:
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 528, in __getattr__
raise AttributeError(name)
AttributeError: __exit__

如何解决这个问题?

模块 PIL.Image 中方法调整大小的帮助:

resize(self, size, resample=0) unbound PIL.Image.Image method
Returns a resized copy of this image.

:param size: The requested size in pixels, as a 2-tuple:
(width, height).
:param filter: An optional resampling filter. This can be
one of :py:attr:`PIL.Image.NEAREST` (use nearest neighbour),
:py:attr:`PIL.Image.BILINEAR` (linear interpolation in a 2x2
environment), :py:attr:`PIL.Image.BICUBIC` (cubic spline
interpolation in a 4x4 environment), or
:py:attr:`PIL.Image.ANTIALIAS` (a high-quality downsampling filter).
If omitted, or if the image has mode "1" or "P", it is
set :py:attr:`PIL.Image.NEAREST`.
:returns: An :py:class:`~PIL.Image.Image` object.

最佳答案

正如其他人所提到的,这可能是由于您的系统上未安装 LANCZOS 缺少某些依赖项。您可以尝试使用 BILINEAR、BICUBIC、NEAREST 或 ANTIALIAS。为了解决这个问题,我们需要深入研究 resizeimage 包。假设该包位于: /usr/local/lib/python3.4/dist-packages/resizeimage/

我们可以使用以下命令编辑 resizeimage 文件:

sudo nano/usr/local/lib/python3.4/dist-packages/resizeimage/resizeimage.py

进入该文件后,我们需要将 LANCZOS 替换为 ANTIALIAS。例如,在resize_cover函数中:

img = img.resize((new_size[0], new_size[1]), Image.LANCZOS)

变成:

img = img.resize((new_size[0], new_size[1]), Image.ANTIALIAS)

文件中的其余函数依此类推。这应该可以解决您的问题。

关于python - 错误模块'对象没有属性'LANCZOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41078229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com