基本介绍
代码: https://github.com/xinntao/Real-ESRGAN
小图片转换大图片,使用 PIL 的图片resize,图片会比较模糊。
这里就可以用深度学习的方案。
库依赖
解决库冲突需要一定的时间,这里记录下,关键库的版本:
numpy==1.26.4 torch==2.1.0 torchvision==0.16.0 realesrgan==0.3.0 basicsr==1.4.2
然后,就是模型,可以从:魔搭社区 下载。
下载地址: https://www.modelscope.cn/models/muse/RealESRGAN_x4plus/files
效果对比
依赖引入
import matplotlib.pyplot as plt import numpy as np from realesrgan import RealESRGANer from basicsr.archs.rrdbnet_arch import RRDBNet from PIL import Image
模型加载
# 加载模型 model = RRDBNet( num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4 ) model_path = "/root/dcgan/RealESRGAN_x4plus/RealESRGAN_x4plus.pth" upsampler = RealESRGANer( scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0 ) upsampler
图片准备
base_path = "/root/dcgan/dcgan" img1_64 = "8602_2004.jpg" img2_64 = "31843_2010.jpg" img1_32 = "5368_2003.jpg" img2_32 = "764_2000.jpg" img_lst = [img1_64, img2_64]
对比绘制
对比:64*64图片转换为:128*128图片的效果
# 绘图对比 def compare_list(img_lst, outscale=2): fig, axes = plt.subplots(len(img_lst), 3, figsize=(10, 3 * len(img_lst))) for index, value in enumerate(img_lst): # 路径 file_full_name = f"{base_path}/images/{value}" # 原图 img = Image.open(file_full_name) # resize图片 img_resized = img.resize((128, 128), Image.Resampling.LANCZOS) # upsampler图片 img_np = np.array(img.convert('RGB')) output, _ = upsampler.enhance(img_np, outscale=outscale) result_img = Image.fromarray(output) # 图片展示 axes[index, 0].imshow(img) axes[index, 1].imshow(img_resized) axes[index, 2].imshow(result_img) plt.tight_layout(pad=0.1, h_pad=0.000001, w_pad=0.1) plt.show() # 对比64*64效果 compare_list(img_lst)
对比:32 * 32 转换为 128 * 128的效果
compare_list([img1_32, img2_32], outscale=3)
对比结论
1、小图片放大,还是有些效果。
2、放大比例越小,效果越好。64 -> 128 效果明细好些。