如何在Python中读取图像的像素数
4 个回答
5
这是你所要求的例子:
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
6
使用 PIL 来加载图片。图片的总像素数量就是它的宽度乘以高度。
23
这里有一个例子:
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"))