如果两张图片不一样大,预处理:
!pip install Pillow
from PIL import Image # 打开图像 img1 = Image.open('img1.jpg') img2 = Image.open('img2.jpg') # 调整图像大小,统一为img1的大小 img2 = img2.resize(img1.size, Image.ANTIALIAS) # 确保图像都是RGB if img1.mode != 'RGB': img1 = img1.convert('RGB') if img2.mode != 'RGB': img2 = img2.convert('RGB') # 保存调整后的图像,如果需要 img1.save('adjusted_img1.jpg') img2.save('adjusted_img2.jpg')
接着开始跑PSNR
!pip install scikit-image
from skimage import io, metrics # 加载图像 img1 = io.imread('adjusted_img1.jpg') img2 = io.imread('adjusted_img2.jpg') # 确保两个图像具有相同的尺寸 if img1.shape != img2.shape: raise ValueError("Images must have the same dimensions.") # 计算PSNR psnr_value = metrics.peak_signal_noise_ratio(img1, img2, data_range=img2.max() - img2.min()) print("PSNR:", psnr_value)
标签:adjusted,PSNR,jpg,RGB,colab,img2,img1,比较 From: https://www.cnblogs.com/Anonytt/p/18278314