myk889090
2023-12-11 10:12:46 +08:00
import os
from PIL import Image
import math
import numpy as np
# 定义计算 psnr 的函数
def psnr(img1, img2):
mse = np.mean((img1 - img2) ** 2 )
if mse == 0:
return 100
PIXEL_MAX = 255.0
return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
# 获取所有 jpg 图片的路径
jpg_list = []
for root, dirs, files in os.walk('images'):
for file in files:
if file.endswith('.jpg'):
jpg_list.append(os.path.join(root, file))
# 遍历所有图片计算 psnr
for i in range(len(jpg_list)):
img1 = Image.open(jpg_list[i])
img2 = Image.open('original.jpg')
img1 = np.array(img1)
img2 = np.array(img2)
print(jpg_list[i] + ' psnr: ', psnr(img1, img2))