镜头焦段统计
1.作用
统计文件夹及子文件夹中照片的常用焦段,并进行可视化,使用时把当前文件放在整理了所有jpg照片的文件夹中即可(可以放在子文件夹内),若要统计raw照片,可以适当进行后缀检测的修改。
2.思路
首先我们用py读取图片exif信息,这个信息中就含有了镜头的焦段,这里我们用到的库是exifread,如果没有的话需要先pip一下,然后我们引入os库方便对当前文件夹和子文件夹进行遍历,再引入matplotlib和seaborn进行图片绘制。
import exifread
import os
import seaborn as sns
import matplotlib.pyplot as plt
接着,我们用
tags = exifread.process_file()
方法来获取图片的exif信息,这个方法的返回值是一个字典,键是光圈快门iso等信息,值就是该信息对应的数值,我们这里统计的是焦段,所以我们找名为’EXIF FocalLength’的键值对即可。
由于这里我们需要统计整个文件夹和子文件夹当中所有图片的exif信息,所以我们要对整个文件夹进行遍历,找到所有尾缀是jpg的图片,png和raw格式暂不考虑。在此之前,我们新建一个字典用于储存焦距和对应的出现次数
dic = {}
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
fname = os.path.join(root, name)
if '.jpg' not in fname.lower():
continue
with open(fname, "rb") as file:
tags = (exifread.process_file(file))
if 'EXIF FocalLength' in tags.keys():
FocalLength = int(str(tags['EXIF FocalLength']))
else:
continue
if FocalLength in dic.keys():
dic[FocalLength] += 1
else:
dic[FocalLength] = 1
最后我们对统计出来的结果进行绘图,如果有输出需求也可以直接print字典
dic = dict(sorted(dic.items(), key=lambda x:x[0]))
top10 = list(dict(sorted(dic.items(), key=lambda x:x[1], reverse=True)).keys())[:11]
top10 = list(set(top10)|{24, 28, 35, 50, 85, 105, 200, 300, 400, 600})
# 这里展示的刻度是最多的十个和几个常用焦距(24 28 35 50 85等)的并集
sns.set()
plt.plot(dic.keys(), dic.values(), color='r')
plt.xticks(top10)
plt.xlabel("FocalLength")
plt.ylabel("Times")
plt.title("FocalLength Frenquency Since 2022.12.01")
plt.legend()
plt.show()
print(dic)
运行的时长取决于图片数
3.结果展示
广角较多是因为文件中有几个广角延时的素材,可以看到大多数焦段集中在了常用镜头焦段的两端,比如2470经常用24或70,然后一些定
焦镜头的焦段出现的也比较多,比如28,50等
4.完整代码
import exifread
import os
import seaborn as sns
import matplotlib.pyplot as plt
dic = {}
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
fname = os.path.join(root, name)
if '.jpg' not in fname.lower():
continue
with open(fname, "rb") as file:
tags = (exifread.process_file(file))
if 'EXIF FocalLength' in tags.keys():
FocalLength = int(str(tags['EXIF FocalLength']))
# print(FocalLength)
else:
continue
if FocalLength in dic.keys():
dic[FocalLength] += 1
else:
dic[FocalLength] = 1
dic = dict(sorted(dic.items(), key=lambda x:x[0]))
top10 = list(dict(sorted(dic.items(), key=lambda x:x[1], reverse=True)).keys())[:11]
top10 = list(set(top10)|{24, 28, 35, 50, 85, 105, 200, 300, 400, 600})
sns.set()
plt.plot(dic.keys(), dic.values(), color='r')
plt.xticks(top10)
plt.xlabel("FocalLength")
plt.ylabel("Times")
plt.title("FocalLength Frenquency Since 2022.12.01")
plt.legend()
plt.show()
print(dic)
5.总结
能把所学用在爱好上是很开心的一件事,希望能坚持学习,保持热爱,找到拍照和学习的平衡点
希望这篇文章对同样爱好摄影的您有所帮助。