将一个文件夹中的所有图片压缩,并保存到另一个文件夹中

from PIL import Image
from os import listdir
import numpy as np

path = '/home/hscuabc/WorkSpace/Python-srf/深度神经网络/data/人脸识别测试数据/negdata'
save_path = '/home/hscuabc/WorkSpace/Python-srf/深度神经网络/data/人脸识别测试数据/reshape_negdata'
w_expect = 20;l_expect = 20

img_path = path + '/image_0.png'


def reshape_img(Img_path, save_img_path):
    im = Image.open(Img_path)
    expect_img = im.resize((w_expect, l_expect), Image.ANTIALIAS)
    expect_img.save(save_img_path)
    return


def all_file_reshape(file_path, save_path):
    all_file = listdir(file_path)
    cnt = 0
    for file in all_file:
        Img_path = file_path + '/' + file
        save_img_path = save_path + '/' + file[6:]
        reshape_img(Img_path, save_img_path)
        cnt = cnt + 1
        print(cnt)

all_file_reshape(path, save_path)

自己在搞深度学习的时候发现negdata图片是50*50的,posdata图片是20*20的,想统一成20*20的

于是就决定写一个脚本搞定他

记得以前群里有个人问有没有可以批量压缩图片的软件

下什么软件呀

直接自己写脚本

(莫名感觉自己好厉害

posted @ 2018-02-20 16:48  shensobaolibin  阅读(401)  评论(0编辑  收藏  举报