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

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

在这里插入图片描述

根据voc数据集把图片按比例缩小,最长边为500,其他边在这个基础上按比例缩小。

代码改为:

#coding=utf-8
import os  #打开文件时需要
from PIL import Image
import re
 
Start_path='//media//drl//系统//RiceTiller-Annotated//6//tiller_new//' #你的图片目录
N_path='//media//drl//系统//RiceTiller-Annotated//6//tiller_resize//'
#iphone5_width=375 #图片最大宽度
#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>h:
        print pic
        print "图片名称为"+pic+"图片被修改"
        h_new=500*h/w
        w_new=500
        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>w:
        print pic
        print "图片名称为"+pic+"图片被修改"
        w_new=500*w/h
        h_new=500
        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()  

在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值