Ubuntu 16.04批量缩小图像、批量命名图像

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

Ubuntu 16.04
虚拟环境:tensorflowt python=2.7

在图片所在文件夹打开终端,然后在命令行输入下面这一行代码可以批量将图片的 .JPG格式转为 .jpg格式。

ls *.JPG|sed -r 's#(.*).JPG#mv & \1.jpg#'|bash

一、批量缩小图像像素

在这里插入图片描述在这里插入图片描述

#coding=utf-8
import os  #打开文件时需要
from PIL import Image
import re
 
Start_path='//media//drl//系统//RiceTiller-Annotated//data_new//JPEGImages//old//' #你的图片目录
N_path='//media//drl//系统//RiceTiller-Annotated//data_new//JPEGImages//new//'
iphone5_width=333 #图片最大宽度
iphone5_depth=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
    #iphone 5的分辨率为1136*640,如果图片分辨率超过这个值,进行图片的等比例压缩
 
    if w>iphone5_width:
        print pic
        print "图片名称为"+pic+"图片被修改"
        h_new=iphone5_width*h/w
        w_new=iphone5_width
        count=count+1
        out = im.resize((w_new,h_new),Image.ANTIALIAS)
        new_pic=re.sub(pic[:-4],pic[:-4],pic) #+'_new'
        #print new_pic
        new_path=N_path+new_pic  #Start_path+new_pic
        out.save(new_path)
 
    if h>iphone5_depth:
        print pic
        print "图片名称为"+pic+"图片被修改"
        w=iphone5_depth*w/h
        h=iphone5_depth
        count=count+1
        out = im.resize((w_new,h_new),Image.ANTIALIAS)
        new_pic=re.sub(pic[:-4],pic[:-4],pic) #+'_new'
        #print new_pic
        new_path=N_path+new_pic #Start_path+new_pic
        out.save(new_path)
 
print 'END'
count=str(count)
print "共有"+count+"张图片尺寸被修改"

在这里插入图片描述
在这里插入图片描述

二、批量将图像名字改为000001.jpg格式

在这里插入图片描述

在这里插入图片描述

#coding=utf-8
import os  #打开文件时需要
from PIL import Image
import re
 
class BatchRename():
    def __init__(self):
        #我的图片文件夹路径
        self.path = '//media//drl//系统//RiceTiller-Annotated//data_new//JPEGImages//new'
 
    def rename(self):
        filelist = os.listdir(self.path)
        total_num = len(filelist)
        i = 000000 #图片编号从多少开始,不要跟VOC原本的编号重复了
        n = 6
        for item in filelist:
            if item.endswith('.jpg'):
                n = 6 - len(str(i))
                src = os.path.join(os.path.abspath(self.path), item)
                dst = os.path.join(os.path.abspath(self.path), str(0)*n + str(i) + '.jpg')
                try:
                    os.rename(src, dst)
                    print 'converting %s to %s ...' % (src, dst)
                    i = i + 1
                except:
                    continue
        print 'total %d to rename & converted %d jpgs' % (total_num, i)
if __name__ == '__main__':
    demo = BatchRename()
    demo.rename()

在这里插入图片描述

至此,就可以运行labelImg图像标注软件来标注图像,生成xml文件,放在Annotations文件夹下。
在这里插入图片描述

三、生成 ImageSets/Main下面的txt文件。

在这里插入图片描述

# !/usr/bin/python
# -*- coding: utf-8 -*-
import os
import random  
  
trainval_percent = 0.8  #trainval占比例多少
train_percent = 0.7  #test数据集占比例多少
xmlfilepath = '/media/drl/系统/RiceTiller-Annotated/data_new/JPEGImages/Annotations'  
txtsavepath = '/media/drl/系统/RiceTiller-Annotated/data_new/JPEGImages/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('/media/drl/系统/RiceTiller-Annotated/data_new/JPEGImages/ImageSets/Main/trainval.txt', 'w')  
ftest = open('/media/drl/系统/RiceTiller-Annotated/data_new/JPEGImages/ImageSets/Main/test.txt', 'w')  
ftrain = open('/media/drl/系统/RiceTiller-Annotated/data_new/JPEGImages/ImageSets/Main/train.txt', 'w')  
fval = open('/media/drl/系统/RiceTiller-Annotated/data_new/JPEGImages/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()  

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/kellyroslyn/article/details/93364072
今日推荐