python批量图片进行双三插值BiCubic后,输出保存(亲测可用)。

双三次插值部分,代码来自,详见该博客。
之前网上找了很多代码,没有批量运行生成的,虽说批量部分只需加个循环,但是也需要费些工夫,在大佬帮助之下,解决了,还是想着记录一下。(本人菜鸟就是这样 ----> 费时)。

from PIL import Image

import numpy as np
import math
import os



os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
def BiBubic(x):
    x=abs(x)
    if x<=1:
        return 1-2*(x**2)+(x**3)
    elif x<2:
        return 4-8*x+5*(x**2)-(x**3)
    else:
        return 0

def BiCubic_interpolation(img,dstH,dstW):
    scrH,scrW,_=img.shape
    #img=np.pad(img,((1,3),(1,3),(0,0)),'constant')
    retimg=np.zeros((dstH,dstW,3),dtype=np.uint8)
    for i in range(dstH):
        for j in range(dstW):
            scrx=i*(scrH/dstH)
            scry=j*(scrW/dstW)
            x=math.floor(scrx)
            y=math.floor(scry)
            u=scrx-x
            v=scry-y
            tmp=0
            for ii in range(-1,2):
                for jj in range(-1,2):
                    if x+ii<0 or y+jj<0 or x+ii>=scrH or y+jj>=scrW:
                        continue
                    tmp+=img[x+ii,y+jj]*BiBubic(ii-u)*BiBubic(jj-v)
            retimg[i,j]=np.clip(tmp,0,255)
    return retimg

im_path = r"D:/luowei_temp/SRCNN_try1/Super-Resolution_CNN-master/dataset1/pngdata/LR_data/"
out_path = r'D:/luowei_temp/SRCNN_try1/Super-Resolution_CNN-master/dataset1/pngdata/LR_biclPNG/'  #事先建立好文件夹
files = os.listdir(im_path)
for spl_file in files:
    file_name = spl_file.strip('D:/luowei_temp/SRCNN_try1/Super-Resolution_CNN-master/dataset1/pngdata/LR_data,.')
    print('--print-bicl-:',file_name)

    image = np.array((Image.open(im_path + spl_file)))
    image3 = BiCubic_interpolation(image, image.shape[0] * 4, image.shape[1] * 4)    # 4 为放大倍数  可改
    # 因为图片在imread过程中,cv2读取的结果图片形式为BRG 需要转化RGB
    image3 = Image.fromarray(image3.astype('uint8')).convert('RGB')  
    image3.save(out_path + file_name+'.png')   
  • 最终的输出为png格式,若需要jpg,可能需要进一步转化,详见:
  • jpg 2 png image 格式批量互换
  • 注意路径问题,仔细琢磨,多半出现错误的地方。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值