#实现了如下简单的功能
#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 globdef 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":passelse:print("mode is run, we need 'test' or 'train' ")data = sorted(glob.glob(os.path.join(data_dir, "*.bmp"))) #in fun glob.glob() * represent everythingreturn  datadef creat_LR_imagepath(HR_imagepath_list,imagepath_LR):# using HR_imagepath_list to create LR imagepath listLR_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_listdef 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 typeimage_2 = np.array(image)image.close()cropped_image = Image.fromarray(modcrop(image_2, scale))(width, height) = cropped_image.sizenew_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.shapeh = h - np.mod(h, scale)w = w - np.mod(w, scale)image = image[0:h, 0:w, :]else:h, w = image.shapeh = h - np.mod(h, scale)w = w - np.mod(w, scale)image = image[0:h, 0:w]return imageif __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 operationfor i in range(len(HR_imagepath_list)):bicubic_downsampling(path= HR_imagepath_list[i], out_path=LR_imagepath_list[i])

·

【Python】批量对文件做downsample并且以指定名称保存到文件夹中相关推荐

  1. MATLAB 实现读取数据文件,画图并自动将图片保存到文件夹中

    实现读取峰值数据,增加道数筛选符合要求的道数值,画出直方图并自动将图片保存到文件夹中,也可选择将直方图中点连线. %%读取峰值数据,增加道数筛选符合要求的道数值,画出直方图并自动将图片保存到文件夹中, ...

  2. python批量转换文件编码

    python批量转换文件编码   3年之前   python 今天在 eclipse 中导入了个之前的 swing 项目,结果跑起来后乱码,检查代码发现竟然一部分 java 文件是 utf-8 编码, ...

  3. Python批量处理文件的方法总结(包括folder、txt、xml、excel)

    目录 一.使用Python批量创建folder 二.使用python批量创建txt 三.使用 Python批量修改文件名 四.使用Python读取txt中的数据并创建excel 五.使用Python批 ...

  4. 怎样用python批量处理文件夹_套娃式文件夹如何通过Python批量处理

    前言 在我对项目组的一些训练图像进行预处理的时候,发现处理的图像是分好了类,在文件夹里的文件夹里,套娃式存储的,所以对我批处理,以及按原文件夹规则进行存储的时候,就会造成很大困扰 但通过下面几个函数的 ...

  5. 怎样用python批量处理文件夹_python批量处理文件或文件夹

    本文实例为大家分享了python批量处理文件或文件夹的具体代码,供大家参考,具体内容如下 # -*- coding: utf-8 -*- import os,shutil import sys imp ...

  6. python删除文件和linux删除文件区别_使用Python批量删除文件列表

    使用Python批量删除文件列表 环境: 已知要删除的文件列表,即确定哪些文件要删除. 代码如下: #!/usr/bin/env python #coding=utf-8 #目的:本程序主要为删除给定 ...

  7. python批量新建文件_python批量处理

    python opencv图像二值化批量处理 from skimage import data_dir,io,transform,color,filters import numpy as np im ...

  8. Python批量复制文件

    Python批量复制文件 首先需要安装shutil库 pip install pytest-shutil # -*- coding: utf-8 -*- """ Crea ...

  9. python批量移动文件_用python批量移动文件

    我是用来移动图片的,其他格式的文档也是可以的,改下后缀列表就可以了 import os,shutil import datetime #将文件夹里的图片全部移动到新文件夹中 #revised by S ...

最新文章

  1. Connot resolve Symbol '.......'
  2. linux 4.4内核是什么,Linux 内核 4.4 LTS 将于2016年1月10日发布
  3. mysql timestampt 输入字符串的格式不正确._mysql中取出的时间格式不正确
  4. PHP | 检查字符串中是否存在特定的单词/子字符串
  5. postfix过滤器名称不一致导致postfix/qmg:warning: connect to transport xxxx: No such file or directory...
  6. 熵值法确定权重(matlab附代码)
  7. 太牛逼了!自从项目中用了Disruptor之后,性能提升了2.5倍
  8. unity 闪烁的gui button
  9. OpenCV-图像处理(19、Canny边缘检测)
  10. Javascript中的运算符及其优先级顺序
  11. 分布式协议与算法(一)Paxos 算法
  12. 显示万年历的程序(汇编语言实现,附源代码)
  13. spark入门名词解释
  14. scrollView无限滚动,横屏和竖屏
  15. 龙芯+银河麒麟QT环境配置+fpm打包
  16. 51_数码管 - DS1302可调时钟
  17. TL494电源芯片使用记录分享
  18. 聊一下测试工程师的面试与招聘【转载自安大叔】
  19. centos离线、在线安装
  20. 流媒体通信开发系列索引 -- SIP 电话项目实战系列

热门文章

  1. mysql 慢查询优化_MySQL 性能优化之慢查询
  2. tinymce编辑器上传图片插件配置
  3. 关于JS运算,出现多余小数点尾数,浮点问题处理
  4. 使用HTML(Web)开发iOS/iPhone/iPad应用
  5. 用httpclient抓取全国火车票信息
  6. 网狐棋牌QueueService
  7. Hug pylons, not trees 拥抱电网,而非树木 | 经济学人20230408版双语精翻
  8. 什么是(抽象方法,普通方法,抽象类、接口),区别又是什么?
  9. 用Mathematica实现各类积分图形区域绘制与积分计算及结果的快速检验方法(一)
  10. RFID智能仓储管理解决方案,你get到了吗-新导智能