Ubuntu 16.04 Batch reduced images, batch image name

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Ubuntu 16.04
virtual environment: tensorflowt python = 2.7

Open a terminal in the folder where the picture file, and then enter the following line of code in the command line can batch will be converted to .jpg format pictures .JPG format.

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

First, reduce the batch image pixels

Here Insert Picture DescriptionHere Insert Picture Description

#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+"张图片尺寸被修改"

Here Insert Picture Description
Here Insert Picture Description

Second, batch image format name to 000001.jpg

Here Insert Picture Description

Here Insert Picture Description

#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()

Here Insert Picture Description

At this point, you can run labelImg image annotation software to annotate images, generate xml files on Annotations folder.
Here Insert Picture Description

Third, generate ImageSets / Main below txt file.

Here Insert Picture Description

# !/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()  

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/kellyroslyn/article/details/93364072