python 区分图像大小(A2、A3、A4)
#!/usr/bin/env python#说明,本人对象负责的项目有大量的加工图像,分别有A2 A3 A4 等规格,且这些图像都是在一起存储,按照相关的档案顺序全组;现在让我分别统计一共的图像数量 以及A2A3A4数量,经过一晚努力,现将代码公布如下:
#这里主要用到了Image这个模块,需要自行下载
#路径注意事项:在Windows里,需要将路径中的\全部改成//
#!/usr/bin/env python
import os
import os.path
import Image
A1=0
A2=0
A3=0
A4=0
total = 0
rootdir = "F://数据备份"
for parent,dirnames,filenames in os.walk(rootdir):
if len(filenames) > 0 and len(dirnames) == 0:
for i in range(len(filenames)):
filename = parent + "//" + filenames
if filename.find(".jpg") > 0:
print filename
image = Image.open(filename)
image_size = image.size
total+=1
if image_size <= 3500 :
A4 = A4 + 1
elif image_size > 3500 and image_size <= 6000:
A3 = A3 + 1
elif image_size > 6000 and image_size <= 9000:
A2 = A2 + 1
elif image_size > 9000:
A1 = A1 + 1
else:
continue
print "总数如下:%d"% total
print "A1一共有:%d"% A1
print "A2一共有:%d"% A2
print "A3一共有:%d"% A3
print "A4一共有:%d"% A4
页:
[1]