【发布时间】:2010-12-07 05:34:55
【问题描述】:
可能重复:
How to check dimensions of all images in a directory using python?
我想知道是否有人知道如何在 python sript 中读取图像总像素数。你能提供和例子吗?
非常感谢。
【问题讨论】:
-
如果这是一个骗子,它比原来的有用得多。
可能重复:
How to check dimensions of all images in a directory using python?
我想知道是否有人知道如何在 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"))
【讨论】:
open(filepath) 不是必需的 - Image.open() 将只接受文件名。
unresolved file reference 错误。
使用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 图像库可以帮助您从图像的元数据中获取此信息。
【讨论】: