如何获得PIL的图片大小?
如何用PIL或任何其他Python库获取图片的大小?
width, height = im.size 
根据文件 。
您可以使用Pillow( 网站 , 文档 , GitHub , PyPI )。 枕头与PIL具有相同的界面,但与Python 3一起使用。
安装
 $ pip install Pillow 
如果你没有pipe理员权限(Debian上的sudo),你可以使用
 $ pip install --user Pillow 
关于安装的其他说明在这里 。
码
 from PIL import Image with Image.open(filepath) as img: width, height = img.size 
速度
这需要3.21秒30336图像(JPG从31×21到424×428,来自国家数据科学碗在Kaggle的训练数据)
这可能是使用枕头而不是自写的最重要的原因。 你应该使用枕头而不是PIL(python-imaging),因为它适用于Python 3。
select#1:Numpy
 import scipy.ndimage height, width, channels = scipy.ndimage.imread(filepath).shape 
select#2:Pygame
 import pygame img = pygame.image.load(filepath) width = img.get_width() height = img.get_height()