from skimage import io
import cv2 as cv
import numpy as np
import os
from PIL import Image
class procImage():
def __init__(self):
# self.input_path=r'I:/footdeep/Hed-footdeep/train/pic'
self.input_path=r'G:/Spyder/Hed/hed-for-card/hed-tutorial-for-card/dataset/data640'
self.output_path=r'G:/Spyder/Hed/hed-for-card/hed-tutorial-for-card/dataset/data45'
def pngtojpg(self):
imagelist=os.listdir(self.input_path)
for im in imagelist:
if im.endswith('.png'):
src=os.path.join(os.path.abspath(self.input_path),im)
dst = os.path.join(os.path.abspath(self.input_path), str(im[0:-4]) + '.jpg')
try:
os.rename(src,dst)
except:
continue
print('finish')
def jpgtopng(self):
imagelist=os.listdir(self.input_path)
for im in imagelist:
if im.endswith('.jpg'):
src=os.path.join(os.path.abspath(self.input_path),im)
dst = os.path.join(os.path.abspath(self.input_path), str(im[0:-4]) + '.png')
try:
os.rename(src,dst)
except:
continue
print('finish')
def rename(self):#重命名并保存该文件夹下
imagelist=os.listdir(self.input_path)
i=4317
j=4317
print('图片开始')
for im in imagelist:
# print(im)
if im.endswith('.jpg'):
src=os.path.join(os.path.abspath(self.input_path),im)
dst = os.path.join(os.path.abspath(self.input_path), str(i) + '.jpg')
try:
os.rename(src,dst)
i=i+1
print('src',src,',对应的i为:',i,',dst',dst)
except:
continue
if im.endswith('.png'):
src=os.path.join(os.path.abspath(self.input_path),im)
dst = os.path.join(os.path.abspath(self.input_path), str(j) + '.png')
try:
os.rename(src,dst)
j=j+1
print(j)
except:
continue
print('最后一张图像编号为',i-1,'下个文件的编码开始为:',i)
def imagerotate(self):#图像旋转
imagelist=os.listdir(self.input_path)
i=0
j=0
for im in imagelist:
print('处理的图片是',im)
if im.endswith('.jpg'):
im=Image.open(os.path.join(self.input_path,im))
jout_im=im.rotate(135)
jout_im.save(os.path.join(self.output_path,str(i)+'.jpg'))
i=i+1
elif im.endswith('.png'):
im=Image.open(os.path.join(self.input_path,im))
pout_im=im.rotate(135)#角度
pout_im.save(os.path.join(self.output_path,str(j)+'.png'))
j=j+1
print('最后一张图像编号为',i-1,'下个文件的编码开始为:',i)
if __name__ == '__main__':
demo = procImage()
# demo.pngtojpg()
# demo.rename()
demo.imagerotate()