# -*- coding: utf-8 -*- import cv2 import os import numpy as np from PIL import Image from psd_tools import PSDImage def file_inspect(): # 文件路径 png_file = 'K:/work/mine_clearance/class1/userfiles/result11.png' psd_file = 'K:/work/mine_clearance/class1/userfiles/result11.psd' result_png = 'K:/work/mine_clearance/class1/result4.png' # 检查文件是否存在 if os.path.exists(png_file) and os.path.exists(psd_file): if psd_to_png(psd_file, result_png): if image_similarity(png_file, result_png) < 0.99: print('result3.psd文件与result3.png不匹配') return 0 return 1 return 0 else: print('缺少文件') return 0 def image_specifications(): # 打开图像文件 image = Image.open('K:/work/mine_clearance/class1/userfiles/result11.png') image2 = Image.open('K:/work/mine_clearance/class1/result4.png') # 获取图像尺寸(宽度和高度) width, height = image2.size width2, height2 = image.size width_difference = abs(width - width2) height_difference = abs(height - height2) if width_difference > 20 or height_difference > 20: print('图片未按照规格修改,请修改在测试!') def similitude(): image1 = 'K:/work/mine_clearance/class1/userfiles/result11.png' image2 = 'K:/work/mine_clearance/class1/result2.png' similarity = image_similarity(image1, image2) if similarity > 0.6 and similarity < 0.99: print('海报制作成功') elif similarity == 1: print('请自己制作!') else: print('还需要进一步修改') print("图像相似度: {:.2f}%".format(similarity*100)) def psd_to_png(psd_path, png_path): try: psd_image = PSDImage.open(psd_path) image_pil = psd_image.topil() image_np = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR) cv2.imwrite(png_path, image_np) return 1 except Exception as e: print("psd文件与png文件相同大小") return 0 def image_similarity(image1, image2): # 读取图片 img1 = cv2.imread(image1) img2 = cv2.imread(image2) # 将图片调整为相同尺寸 img1 = cv2.resize(img1, img2.shape[:2][::-1]) # 计算直方图差异 hist1 = cv2.calcHist([img1], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256]) hist2 = cv2.calcHist([img2], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256]) similarity = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL) return similarity if __name__=="__main__": if file_inspect(): similitude() image_specifications()