From bc4c95c18c69d92921f6a1586c8b47839313e4e0 Mon Sep 17 00:00:00 2001 From: liuyebo <1515783401@qq.com> Date: Fri, 12 Sep 2025 14:40:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=A4=E6=96=AD=E6=88=AA?= =?UTF-8?q?=E5=9B=BE=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/image_util.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/util/image_util.py b/util/image_util.py index 31a29da..f1949cc 100644 --- a/util/image_util.py +++ b/util/image_util.py @@ -268,3 +268,61 @@ def parse_clarity(image): except Exception as e: logging.error("获取图片清晰度失败", exc_info=e) 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)