import os
'''要重命名的图片路径'''
file_path = "F:/code/pytorch-deeplab-xception-master2/dataloaders/datasets/own/train/images"
files = os.listdir(file_path)
for file in files:
if file.endswith('jpg'):
# 要指明重命名之后的路径
src = os.path.join(file_path, file)
r_name = file.split('.')[0] + '.tif'
dct = os.path.join(file_path, r_name)
os.rename(src, dct)
from PIL import Image
import os
input_dir = '输入文件夹/'
out_dir = '输出文件夹/'
a = os.listdir(file_dir)
for i in a:
print(i)
I = Image.open(file_dir + i)
L = I.convert('p')
L.save(out_dir + i)
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Oct 24 10:47:36 2018 @author: yxh """ import numpy as np from PIL import Image import matplotlib.pyplot as plt import os import sys import shutil path='F:/code/pytorch-deeplab-xception-master/dataloaders/datasets/invoice/SegmentationClass3/' newpath='F:/code/pytorch-deeplab-xception-master/dataloaders/datasets/invoice/SegmentationClass' def turnto24(path): files = os.listdir(path) files = np.sort(files) i=0 for f in files: imgpath = path + f img=Image.open(imgpath).convert('P') dirpath = newpath file_name, file_extend = os.path.splitext(f) dst = os.path.join(os.path.abspath(dirpath), file_name + '.png') img.save(dst) turnto24(path)
from sklearn.model_selection import train_test_split import os # “”“” # train_test_split的用法 # train_size:训练集大小 # # float:0 - 1 # 之间,表示训练集所占的比例 # # int:直接指定训练集的数量 # # None:自动为测试集的补集,也就是原始数据集减去测试集 # # test_size:测试集大小,默认值是0 # .25 # # float:0 - 1 # 之间,表示测试集所占的比例 # # int:直接指定测试集的数量 # # None:自动为训练集的补集,也就是原始数据集减去训练集 # # random_state:可以理解为随机数种子,主要是为了复现结果而设置 # # shuffle:表示是否打乱数据位置,True或者False,默认是True # # stratify:表示是否按照样本比例(不同类别的比例)来划分数据集,例如原始数据集 # 类A: 类B = 75 %:25 %,那么划分的测试集和训练集中的A: B的比例都会是75 %:25 %;可用于样本类别差异很大的情况,一般使用为:stratify = y,即用数据集的标签y来进行划分。 # “”“ imagedir = 'F:/code/pytorch-deeplab-xception-master/datasets/invoice/JPEGImages/' outdir = 'F:/code/pytorch-deeplab-xception-master/datasets/invoice/ImageSets/Segmentation/' images = [] for file in os.listdir(imagedir): filename = file.split('.')[0] images.append(filename) # 训练集测试集验证集比例为:4:2:2 train, test = train_test_split(images, train_size=0.5, random_state=0) val, test = train_test_split(test, train_size=0.5, random_state=0) with open(outdir + "train.txt", 'w') as f: f.write('\n'.join(train)) with open(outdir + "val.txt", 'w') as f: f.write('\n'.join(val)) with open(outdir + "test.txt", 'w') as f: f.write('\n'.join(test))