# -*- coding: utf-8 -*- import cv2 import os import numpy as np from PIL import Image from psd_tools import PSDImage def image_specifications(image_path, image2_path): # 打开图像文件 image = Image.open(image_path) image2 = Image.open(image2_path) # 获取图像尺寸(宽度和高度) 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 file_inspect(): # 文件路径 png_file = 'K:/work/mine_clearance/class1/userfiles/result1.png' psd_file = 'K:/work/mine_clearance/class1/userfiles/result1.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('result2.psd文件与result2.png不匹配') return 0 return 1 return 0 else: print('缺少文件') 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 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 split_image(image_path, split_path): # 读取图片 image = cv2.imread(image_path) # 获取图片的宽度和高度 height, width, _ = image.shape # 计算每个小块的宽度和高度 block_width = width // 2 block_height = height // 2 # 切分图片并保存 upper_left = image[0:block_height, 0:block_width] cv2.imwrite(split_path[0], upper_left) upper_right = image[0:block_height, block_width:width] cv2.imwrite(split_path[1], upper_right) lower_left = image[block_height:height, 0:block_width] cv2.imwrite(split_path[2], lower_left) lower_right = image[block_height:height, block_width:width] cv2.imwrite(split_path[3], lower_right) def calculate_similarity(img1_path, img2_path): # 加载图像 img1 = cv2.imread(img1_path, cv2.IMREAD_GRAYSCALE) img2 = cv2.imread(img2_path, cv2.IMREAD_GRAYSCALE) # 创建ORB对象 orb = cv2.ORB_create() # 检测和计算图像特征 kp1, des1 = orb.detectAndCompute(img1, None) kp2, des2 = orb.detectAndCompute(img2, None) # 创建BFMatcher匹配器 bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # 使用KNN算法进行特征匹配 matches = bf.match(des1, des2) # 对匹配结果进行排序 matches = sorted(matches, key=lambda x: x.distance) # 计算相似度 similarity = len(matches) / max(len(kp1), len(kp2)) * 100 return similarity if __name__ == '__main__': # 图像文件路径 image1 = 'K:/work/mine_clearance/class1/result2.png' image2 = 'K:/work/mine_clearance/class1/userfiles/result1.png' if file_inspect(): image_specifications(image1, image2) data = [] data_path = [] data_path.append( ['K:/work/mine_clearance/class1/userfiles/upper_left1.png', 'K:/work/mine_clearance/class1/userfiles/upper_right1.png', 'K:/work/mine_clearance/class1/userfiles/lower_left1.png', 'K:/work/mine_clearance/class1/userfiles/lower_right1.png']) data_path.append( ['K:/work/mine_clearance/class1/userfiles/upper_left2.png', 'K:/work/mine_clearance/class1/userfiles/upper_right2.png', 'K:/work/mine_clearance/class1/userfiles/lower_left2.png', 'K:/work/mine_clearance/class1/userfiles/lower_right2.png']) # 调用函数对图片进行切分 split_image(image1, data_path[0]) split_image(image2, data_path[1]) flage = 1 # 计算图像相似度 for i in range(4): data.append(calculate_similarity(data_path[0][i], data_path[1][i])) if data[i] < 60 or data[i] > 99: flage = 0 # # 显示相似度 if flage == 1: print('文字特效制作成功') else: print('还需要进一步修改') print("图像左上相似度: {:.2f}%".format(data[0])) print("图像右上度: {:.2f}%".format(data[1])) print("图像左下相似度: {:.2f}%".format(data[2])) print("图像右下相似度: {:.2f}%".format(data[3]))