我想将图片大小调整为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.
答案 0 :(得分:1)
正如其他人所提到的,这可能是由于在系统上未安装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)
等等,用于文件中的其余功能。这应该可以解决您的问题。
答案 1 :(得分:0)
这可能取决于您的PIL版本。在python shell中试试这个
>>> import PIL.Image
>>> help(PIL.Image.Image.resize)
答案 2 :(得分:0)
我实际上没有在您的代码中看到LANCZOS,但修复方法是将Image.LANCZOS
替换为Image.ANTIALIAS
。您可能安装的Python版本与使用LANCZOS的版本不同。 ANTIALIAS应该在缩小图像尺寸并使其看起来清晰时表现平等。
答案 3 :(得分:0)
您的依赖项旨在与更高版本的Pillow一起使用。只需安装最新的Pillow即可。