phi*_*mue 405
from PIL import Image
im = Image.open('whatever.png')
width, height = im.size
Run Code Online (Sandbox Code Playgroud)
根据文件.
Mar*_*oma 68
您可以使用Pillow(网站,文档,GitHub,PyPI).Pillow与PIL具有相同的界面,但与Python 3一起使用.
$ pip install Pillow
Run Code Online (Sandbox Code Playgroud)
如果您没有管理员权限(Debian上的sudo),您可以使用
$ pip install --user Pillow
Run Code Online (Sandbox Code Playgroud)
有关安装的其他说明在这里.
from PIL import Image
with Image.open(filepath) as img:
width, height = img.size
Run Code Online (Sandbox Code Playgroud)
30336张图像需要3.21秒(JPG从31x21到424x428,来自Kaggle 国家数据科学碗的训练数据)
这可能是使用Pillow而不是自编的最重要原因.你应该使用Pillow代替PIL(python-imaging),因为它适用于Python 3.
import scipy.ndimage
height, width, channels = scipy.ndimage.imread(filepath).shape
Run Code Online (Sandbox Code Playgroud)
import pygame
img = pygame.image.load(filepath)
width = img.get_width()
height = img.get_height()
Run Code Online (Sandbox Code Playgroud)
由于scipy'simread已被弃用,请使用imageio.imread.
pip install imageioheight, width, channels = imageio.imread(filepath).shape