实现了如下简单的功能
1:从一个文件夹中将现有的图片的路径做成一个list
2:根据上述做出的list,生成一个目标对象的路径以及名称并保存为list
3:通过一个统一的操作,将现有的图片做了操作后的结果保存到我定义的路径中,并根据自己的要求进行命名
·
#- * - utf -8 -*-
# 实现了如下简单的功能
#1:从一个文件夹中将现有的图片的路径做成一个list
#2:根据上述做出的list,生成一个目标对象的路径以及名称并保存为list
#3:通过一个统一的操作,将现有的图片做了操作后的结果保存到我定义的路径中,并根据自己的要求进行命名
import numpy as np
from PIL import Image
import tensorlayer as tl
import os
import glob
def read_imagepath_to_list(imagepath):
if tl.global_flag['mode'] == "train":
#filename = os.listdir(imagepath)
data_dir = os.path.join(os.getcwd(), imagepath)
elif tl.global_flag['mode'] == "test":
pass
else:
print("mode is run, we need 'test' or 'train' ")
data = sorted(glob.glob(os.path.join(data_dir, "*.bmp"))) #in fun glob.glob() * represent everything
return data
def creat_LR_imagepath(HR_imagepath_list,imagepath_LR):
# using HR_imagepath_list to create LR imagepath list
LR_imagepath_list = []
for i in range(len(HR_imagepath_list)):
temp_path = HR_imagepath_list[i].strip().split('/')
LR_pic_name = imagepath_LR + '/' +'LR_' + temp_path[-1]
LR_imagepath_list.append(LR_pic_name)
return LR_imagepath_list
def bicubic_downsampling(path, out_path, scale=3):
"""
Preprocess single image file
(1) Read original image
(2) Normalize
(3) Downsampled by scale factor (using anti-aliasing)
"""
image = Image.open(path)
(width, height) = image.size
# because bmpimagefile object has no attribute 'shape'
# so we want to transfer to np type
image_2 = np.array(image)
image.close()
cropped_image = Image.fromarray(modcrop(image_2, scale))
(width, height) = cropped_image.size
new_width, new_height = int(width / scale), int(height / scale)
scaled_image = cropped_image.resize((new_width, new_height), Image.ANTIALIAS)
cropped_image.close()
scaled_image.save(out_path)
#(width, height) = scaled_image.size
# scaled_image.show()
#downdamoled_image = np.array(list(scaled_image.getdata())).astype(np.float).reshape((height, width,3))
def modcrop(image, scale=3):
"""
To scale down and up the original image, first thing to do is to have no remainder while scaling operation.
We need to find modulo of height (and width) and scale factor.
Then, subtract the modulo from height (and width) of original image size.
There would be no remainder even after scaling operation.
"""
if len(image.shape) == 3:
h, w, _ = image.shape
h = h - np.mod(h, scale)
w = w - np.mod(w, scale)
image = image[0:h, 0:w, :]
else:
h, w = image.shape
h = h - np.mod(h, scale)
w = w - np.mod(w, scale)
image = image[0:h, 0:w]
return image
if __name__ == "__main__":
tl.global_flag['mode'] = 'train'
imagepath = '/home/jzj/yuanlei/LapSRN-tensorflow-master/Train1'
imagepath_LR = '/home/jzj/yuanlei/LapSRN-tensorflow-master/TrainLR'
if not os.path.exists(imagepath_LR) :
os.makedirs(imagepath_LR)
HR_imagepath_list = read_imagepath_to_list(imagepath)
LR_imagepath_list = creat_LR_imagepath(HR_imagepath_list, imagepath_LR)
#for each image create a bicubic downsampling operation
for i in range(len(HR_imagepath_list)):
bicubic_downsampling(path= HR_imagepath_list[i], out_path=LR_imagepath_list[i])
·