我想知道是否有人知道如何在 python sript 中读取图像总像素数。你能提供和例子吗?
非常感谢。
这是一个例子:
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"))
使用PIL加载图像。像素的总数将是它的宽度乘以它的高度。
这是您要求的示例:
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
PIL,Python 图像库可以帮助您从图像的元数据中获取此信息。