常用镜头焦段统计(Python实现)

镜头焦段统计

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.总结

能把所学用在爱好上是很开心的一件事,希望能坚持学习,保持热爱,找到拍照和学习的平衡点
希望这篇文章对同样爱好摄影的您有所帮助。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python可以通过使用字典和列表等数据结构来实现分区统计。下面是一个示例的方法来实现分区统计。 假设有一个包含学生信息的列表,每个学生信息包括姓名和年龄。现在需要按照年龄进行分区统计统计每个年龄段内有多少学生。 首先,我们可以创建一个空的字典用来存储不同年龄段的学生数量。然后,遍历学生列表,对于每个学生,获取其年龄信息进行分区统计。 具体实现如下: ```python # 学生信息列表 students = [ {"name": "Tom", "age": 18}, {"name": "Mary", "age": 19}, {"name": "John", "age": 20}, {"name": "Alice", "age": 18}, {"name": "Bob", "age": 19}, {"name": "Linda", "age": 20} ] # 创建空字典用于分区统计 age_count = {} # 遍历学生列表,进行分区统计 for student in students: age = student["age"] # 获取学生的年龄信息 if age in age_count: # 判断当前年龄是否已经在字典中存在 age_count[age] += 1 # 如果存在,则对应年龄的数量加1 else: age_count[age] = 1 # 如果不存在,则将该年龄添加到字典中并设置为1 # 输出分区统计结果 for age, count in age_count.items(): print(f"年龄{age}的学生数量为{count}人") ``` 以上代码运行后,会输出每个年龄段的学生数量。例如,上述示例输出结果为: ``` 年龄18的学生数量为2人 年龄19的学生数量为2人 年龄20的学生数量为2人 ``` 这就是使用Python实现分区统计的方法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值