#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import os
import cv2
import numpy as np
from numpy.core.fromnumeric import resize
#创建一个不存在的文件夹
def makefilepath(folder_path):
if not os.path.exists(folder_path):
os.makedirs(folder_path)
#从文件夹载入多种图片,处理后将结果保存到一个文件夹
#处理范围包括:图片类型的转换,名字转换,文件名导出
def read__image(open_path,save_path):
nums=0
images=[]
for dir_image in os.listdir(open_path): # os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
full_path = os.path.abspath(os.path.join(open_path,dir_image))
if dir_image.endswith('.jpg'):
image = cv2.imread(full_path)
p,f = os.path.split(full_path) #切分文件名和路径
n,ext = os.path.splitext(f) #切分后缀和路径
image= cv2.resize(image, (500, 500), interpolation=cv2.INTER_CUBIC)
image_path = (save_path+'%s.png' % (n)) # 注意这里图片名一定要加上扩展名,否则后面imwrite的时候会报错
cv2.imwrite(image_path, image)
nums=nums+1
print("Output over!The total of image are %s ."%nums)
#导出图片的名字,写入到文件夹中
def Outputimgname(imgpath):
nums=0
images=[]
for dir_image in os.listdir(imgpath): # os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
full_path = os.path.abspath(os.path.join(imgpath,dir_image))
if dir_image.endswith('.jpg'):
image = cv2.imread(full_path)
p,f = os.path.split(full_path) #切分文件名和路径
n,ext = os.path.splitext(f) #切分后缀和路径
#将图片名写入txt文档
with open("testdata.txt","a") as f:
f.write(n)
f.write('\n')
nums=nums+1
print("Output over!The total of image are %s ."%nums)
#载入图片,处理后保存到一个列表中
def GetImg(open_path):
patch=[]
for dir_image in os.listdir(open_path): # os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
full_path = os.path.abspath(os.path.join(open_path,dir_image))
if dir_image.endswith('.jpg'):
image = cv2.imread(full_path)
resImg=cv2.resize(image,(227,227))
patch.append(resImg)
return patch
#输入一张图片,产生旋转,缩放,长宽调整等形式
def DateArgutation(images):
img=images
imgInfo=img.shape
height=imgInfo[0]
width=imgInfo[1]
agutationimg=[]
#旋转3张
angle=[90,180,280]
for a in angle:
matRotate=cv2.getRotationMatrix2D((height*0.5,width*0.5),a,0.5)
rotateImg=cv2.warpAffine(img,matRotate,(height,width))
agutationimg.append( rotateImg)
#缩放3张
scale=[0.5,2,4]
for s in scale:
dstHeight=int(height*s)
dstWidth=int(width*s)
resImg=cv2.resize(img,(dstWidth,dstHeight))
agutationimg.append(resImg)
#长宽
change=[[40,50],[60,50],[60,40]]
for h,w in change:
reshwImg=cv2.resize(img,(h,w))
agutationimg.append( reshwImg)
print('success!')
return agutationimg
if __name__=='__main__':
#打开路径
openpath1="F:/pytorchdeeplearnbook/program/programs/data/VOC2012/01mylittletestdata/testdata01/"
#保存路径
savepath1="F:/pytorchdeeplearnbook/program/programs/data/VOC2012/01mylittletestdata/testdatalabel01/"
read__image(openpath1,savepath1)