python对双层目录下的图像数据进行规律重命名并重新调整图片大小(类似resize)

# -*- coding:utf-8 -*-

import os
from PIL import Image

path = '/home/xn/caffe/examples/facetestquestions/ImageDatainc/'
#Rename the picture
def rename():
    j = 0
    for image_class in os.listdir(path):
        i = 0
        image_path = path + image_class + "/"
        for imgname in os.listdir(image_path):
            Olddir = os.path.join(image_path, imgname)
            if os.path.isdir(Olddir):
                continue
            #imagename = os.path.splitext(imgname)[0]
            imgtype = os.path.splitext(imgname)[1]
            Newdir = os.path.join(path + image_class, imgname[0:3] + format(str(j), '0>2s') + format(str(i), '0>2s') + imgtype)
            i = i + 1
            os.rename(Olddir, Newdir)
        j = j + 1
rename()


#Resize and save the image
def convertjpg(jpgfile,outdir,width=300,height=300):
    img=Image.open(jpgfile)
    try:
        new_img=img.resize((width,height),Image.BILINEAR)
        new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
    except Exception as e:
        print(e)

for image_class in os.listdir(path):
    image_path = path + image_class + "/"
    for imgname in os.listdir(image_path):
        convertjpg(image_path + imgname, image_path)

#Execute the .sh file to create lmdb
#cmd = '/home/xn/caffe/examples/facetestquestions/create_imagenet.sh'
#import subprocess
#p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
#while p.poll() == None:
#     line = p.stdout.readline()
#     print line
#     result = result + line

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值