Python 批量处理PNG、JPG图片 去白边

每次写论文用seaborn画出来的图白边都很大并且没法调,每次都用PS软件去白边很麻烦。索性写了个脚本,在不改变图片像素密度(尺寸变了,相当于切割)的情况下,批量去掉图片白边,参考了部分网上的程序,自己写了个简单的脚本,放到一个Python文件里直接执行就行:

import numpy as np
import matplotlib.image as mpimg
import os
import time

# 去除图片白边
def resize_figure(img_path):
    img = mpimg.imread(img_path)
    ini_size = img.shape
    x = 0
    xx = img.shape[0]
    y = 0
    yy = img.shape[1]
    for channel in range(img.shape[2]):
        for i in np.arange(0, img.shape[0], 1):
            if img[i, :, channel].sum() != img.shape[1]:
                x = max(x, i)
                break
        for i in np.arange(img.shape[0] - 1, -1, -1):
            if img[i, :, channel].sum() != img.shape[1]:
                xx = min(xx, i)
                break
        for j in np.arange(0, img.shape[1], 1):
            if img[:, j, channel].sum() != img.shape[0]:
                y = max(y, j)
                break
        for j in np.arange(img.shape[1] - 1, -1, -1):
            if img[:, j, channel].sum() != img.shape[0]:
                yy = min(yy, j)
                break
    cutted_res = img[x - 5:xx + 5, y - 5:yy + 5, :]
    return cutted_res, ini_size, cutted_res.shape


# 获得某个文件夹下的所有的文件路径
def DFS_file_search(dict_name=None, out=None):
    # list.pop() list.append()这两个方法就可以实现栈维护功能
    if dict_name is None:  # 代表该根目录
        dict_name = './'
    stack = []
    result_txt = []
    stack.append(dict_name)
    while len(stack) != 0:  # 栈空代表所有目录均已完成访问
        temp_name = stack.pop()
        try:
            temp_name2 = os.listdir(temp_name) # list ["","",...]
            for eve in temp_name2:
                stack.append(temp_name + "\\" + eve)  # 维持绝对路径的表达
        except NotADirectoryError:
            result_txt.append(temp_name)

    # 检查是不是图片格式
    if out is None:
        out = 'cutted'  # 默认的名字
    res = []
    for eve_path in result_txt:
        if eve_path.find('.') != -1 and eve_path.find(out) == -1:
            if eve_path.split('.')[-1] in ['png', 'PNG', 'jpg', 'JPG', 'eps', 'EPS']:
                res.append(eve_path)
    return res


if __name__ == '__main__':

    read_root_path = None  # 要读取的文件夹,None默认为当前根目录,也可以自己指定文件夹
    save_root_path = 'cutted'  # 输出的文件夹

    if not os.path.exists(save_root_path):
        os.mkdir(save_root_path)
    paths = DFS_file_search(read_root_path, save_root_path)
    print('要处理的图片的路径为:', paths)
    for path in paths:
        print('开始处理 ' + path)
        t1 = time.time()
        cutted_figure, ini_size, final_size = resize_figure(path)
        temp_name = path.split('.')[-2].split('/')[-1]
        mpimg.imsave('./' + save_root_path + "/" + temp_name + '.png', cutted_figure)
        print('处理完毕 ' + path, '  耗时:', time.time()-t1, " 秒", ' 处理前shape:', ini_size, ' 处理前shape:',final_size)


  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值