添加判断截图方法

This commit is contained in:
2025-09-12 14:40:52 +08:00
parent af08078380
commit bc4c95c18c

View File

@@ -268,3 +268,61 @@ def parse_clarity(image):
except Exception as e: except Exception as e:
logging.error("获取图片清晰度失败", exc_info=e) logging.error("获取图片清晰度失败", exc_info=e)
return clarity_result return clarity_result
def is_photo_by_exif(exif_tags):
"""分析EXIF数据判断是否为照片"""
# 照片通常包含的EXIF标签
photo_tags = [
'FNumber', # 光圈
'ExposureTime', # 曝光时间
'ISOSpeedRatings', # ISO
'FocalLength', # 焦距
'LensModel', # 镜头型号
'GPSLatitude' # GPS位置信息
]
# 统计照片相关的EXIF标签数量
photo_tag_count = 0
if exif_tags:
for tag in photo_tags:
if tag in exif_tags:
photo_tag_count += 1
# 如果有2个以上照片相关的EXIF标签倾向于是照片
if photo_tag_count >= 2:
return True
# 不确定是照片返回False
return False
def is_screenshot_by_image_features(image):
"""分析图像特征判断是否为截图"""
# 定义边缘像素标准差阈值,小于此阈值则认为图片是截图
edge_std_threshold = 20.0
try:
# 检查边缘像素的一致性(截图边缘通常更整齐)
edge_pixels = []
# 取图像边缘10像素
edge_pixels.extend(image[:10, :].flatten()) # 顶部边缘
edge_pixels.extend(image[-10:, :].flatten()) # 底部边缘
edge_pixels.extend(image[:, :10].flatten()) # 左侧边缘
edge_pixels.extend(image[:, -10:].flatten()) # 右侧边缘
# 计算边缘像素的标准差(值越小说明越一致)
edge_std = numpy.std(edge_pixels)
logging.info(f"边缘像素标准差: {edge_std}")
return edge_std < edge_std_threshold
except Exception as e:
logging.error("图像特征分析失败", exc_info=e)
return False
def is_screenshot(image, exif_tags):
"""综合判断是否是截图"""
# 先检查EXIF数据
result_of_exif = is_photo_by_exif(exif_tags)
# 如果有明显的照片EXIF信息直接判断为照片
if result_of_exif:
return False
# 分析图像特征
return is_screenshot_by_image_features(image)