这里有一个例子:
from PIL import Image
def get_num_pixels(filepath):
width, height = Image.open(filepath).size
return width*height
print(get_num_pixels("/path/to/my/file.jpg"))
open(filepath)
并不必要——Image.open()
只需要文件名即可。 - mhawke这是您要求的示例:
from PIL import Image
import os.path
filename = os.path.join('path', 'to', 'image', 'file')
img = Image.open(filename)
width, height = img.size
print "Dimensions:", img.size, "Total pixels:", width * height
Python Imaging Library(PIL)可以帮助您从图像元数据中获取此信息。