图片分辨率大小名字处理

设置图片大小

# coding=utf-8
import os  # 打开文件时需要
from PIL import Image
import re

Start_path = 'F:/faster-rcnn-tensorflow-python3-master/data/VOCdevkit2007/VOC2007/JPEGImages/'  # 你的图片目录
width_max = 375  # 图片最大宽度
depth_max = 500  # 图片最大高度

list = os.listdir(Start_path)
# print list
count = 0
for pic in list:
    path = Start_path + pic
    print(path)
    im = Image.open(path)
    w, h = im.size
    print (w,h)

#如果图片分辨率超过这个值,进行图片的等比例压缩
    if w > width_max:
        print(pic)
        print("图片名称为" + pic + "图片被修改")
        h_new = width_max * h // w
        w_new = width_max
        count = count + 1
        out = im.resize((w_new, h_new), Image.ANTIALIAS)
        new_pic = re.sub(pic[:-4], pic[:-4] , pic)
        # print new_pic
        new_path = Start_path + new_pic
        out.save(new_path)

    if h > depth_max:
        print(pic)
        print("图片名称为" + pic + "图片被修改")
        w_new = depth_max * w // h
        h_new = depth_max
        count = count + 1
        out = im.resize((w_new, h_new), Image.ANTIALIAS)
        new_pic = re.sub(pic[:-4], pic[:-4] , pic)
        # print new_pic
        new_path = Start_path + new_pic
        out.save(new_path)

print('END')
count = str(count)
print("共有" + count + "张图片尺寸被修改")

设置图片名字

import os

import os
path ='F:/faster-rcnn-tensorflow-python3-master/data/VOCdevkit2007/VOC2007/JPEGImages/'  # 你的图片目录
filelist = os.listdir(path) #该文件夹下所有的文件(包括文件夹)
count=0
for file in filelist:
    Olddir = os.path.join(path, file)  # 原来的文件路径
    if os.path.isdir(Olddir):  # 如果是文件夹则跳过
        continue
    filename = os.path.splitext(file)[0]  # 文件名
    filetype = os.path.splitext(file)[1]  # 文件扩展名
    print(filename)
    Newdir = os.path.join(path, str(count).zfill(5) + filetype)  # 用字符串函数zfill 以0补全所需位数
    os.rename(Olddir, Newdir)  # 重命名
    count += 1
# os.path.splitext(“文件路径”)    分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作

print ('END')
count=str(count)
print("共有"+count+"张图片尺寸被修改")

filelist = os.listdir(path)  # 该文件夹下所有的文件(包括文件夹)
count = 0
for file in filelist:
    Olddir = os.path.join(path, file)  # 原来的文件路径
    if os.path.isdir(Olddir):  # 如果是文件夹则跳过
        continue
    filename = os.path.splitext(file)[0]  # 文件名
    filetype = os.path.splitext(file)[1]  # 文件扩展名
    print(filename)
    Newdir = os.path.join(path, str(count).zfill(5) + filetype)  # 用字符串函数zfill 以0补全所需位数
    os.rename(Olddir, Newdir)  # 重命名
    count += 1
# os.path.splitext(“文件路径”)    分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作

print('END')
count = str(count)
print("共有" + count + "张图片尺寸被修改")

生成训练和测试需要的txt文件索引

# !/usr/bin/python
# -*- coding: utf-8 -*-
import os
import random  
  
trainval_percent = 0.8  #trainval占比例多少
train_percent = 0.7  #test数据集占比例多少
xmlfilepath = 'Annotations'  
txtsavepath = 'ImageSets\Main'  
total_xml = os.listdir(xmlfilepath)  
  
num=len(total_xml)  
list=range(num)  
tv=int(num*trainval_percent)  
tr=int(tv*train_percent)  
trainval= random.sample(list,tv)  
train=random.sample(trainval,tr)  
  
ftrainval = open('ImageSets/Main/trainval.txt', 'w')  
ftest = open('ImageSets/Main/test.txt', 'w')  
ftrain = open('ImageSets/Main/train.txt', 'w')  
fval = open('ImageSets/Main/val.txt', 'w')  
  
for i  in list:  
    name=total_xml[i][:-4]+'\n'  
    if i in trainval:  
        ftrainval.write(name)  
        if i in train:  
            ftrain.write(name)  
        else:  
            fval.write(name)  
    else:  
        ftest.write(name)  
  
ftrainval.close()  
ftrain.close()  
fval.close()  
ftest .close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值