开发者

How can I read how many pixels an image has in Python [duplicate]

This question already has answers here: 开发者_运维百科 Closed 13 years ago.

Possible Duplicate:

How to check dimensions of all images in a directory using python?

I was wondering if somebody knows how can I read an image total amount of pixels in a python sript. Could you provide and example?

Thanks a lot.


here is an example:

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"))


Use PIL to load the image. The total number of pixels will be its width multiplied by its height.


Here is the example that you've asked for:

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, the Python Imaging Library can help you get this info from image's metadata.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜