图像裁剪 重命名 批量操作

1.图像裁剪函数

import os
from PIL import Image

#用PIL库批量裁剪指定大小的图像(自动填充)
def img_crop(img_path, save_path):
    files = os.listdir(img_path)
    for file in files:
        a, b = os.path.splitext(file)
        img = Image.open(os.path.join(img_path + "/" + file))
        width, hight = img.size
        rewidth = width*0.65  # 需要切成图片块的大小,默认大小为w*w,可以自己设置
        rehight = hight
        id = 1
        i = 0
        padw = padh = 0  # 当宽高除不尽切块大小时,对最后一块进行填充
        if width % rewidth != 0:
            padw = 1  # 宽除不尽的情况
        if hight % rehight != 0:
            padh = 1  # 高除不尽的情况

        # 默认从最左上角向右裁剪,再向下裁剪
        while i + rehight <= hight:
            j = 0
            while j + rewidth <= width:
                new_img = img.crop((j, i, j + rewidth, i + rehight))
                new_img.save(save_path + a + "_" + str(id) + b)
                id += 1
                j += rewidth
            if padw == 1:  # 宽有除不尽的情况
                new_img = img.crop((width - rewidth, i, width, i + rehight))
                new_img.save(save_path + a + "_" + str(id) + b)
                id += 1
            i = i + rehight

        if padh == 1:  # 高除不尽的情况
            j = 0
            while j + rewidth <= width:
                new_img = img.crop((j, hight - rehight, j + rewidth, hight))
                new_img.save(save_path + a + "_" + str(id) + b)
                id += 1
                j += rewidth
            if padw == 1:
                new_img = img.crop((width - rewidth, hight - rehight, width, hight))
                new_img.save(save_path + a + "_" + str(id) + b)
                id += 1`在这里插入代码片`
if __name__ == '__main__':
    # img_path:输入图像的路径
    img_path = 'F:/Corel5K-master/test/'
    # save_path:图像保存的位置
    save_path = 'F:/Corel5K-master/test_crop/'
    # save_path2 = 'F:/Corel5K-master/test_crop2/'
    img_crop(img_path, save_path)

   # img_crop(img_path, save_path2)

2.图像重命名

import os


class BatchRename():

    def __init__(self):
        self.path = 'F:/Corel5K-master/test/'  # 图片的路径

    def rename(self):
        filelist = os.listdir(self.path)
        filelist.sort()
        total_num = len(filelist)  # 获取文件中有多少图片
        i = 1  # 文件命名从哪里开始(即命名从哪里开始)
        for item in filelist:
            if item.endswith('.jpeg'):
                src = os.path.join(self.path, item)
                dst = os.path.join(os.path.abspath(self.path), str(i) + '.jpeg')

                try:
                    os.rename(src, dst)
                    print('converting %s to %s ...' % (src, dst))
                    i = i + 1
                except Exception as e:
                    print(e)
                    print('rename dir fail\r\n')

        print('total %d to rename & converted %d jpgs' % (total_num, i))


if __name__ == '__main__':
    demo = BatchRename()  # 创建对象
    demo.rename()  # 调用对象的方法

3.选取图片重命名到另一文件夹

import os
import shutil

path = 'E:\\move\\桌面\\2'
new_path = 'E:\\move\\桌面\\1'
filelist = os.listdir(path)
i = 1

for item in filelist:
    if item.endswith('5.bmp') or item.endswith('6.bmp') or item.endswith('7.bmp') or item.endswith('8.bmp'):
        src = os.path.join(os.path.abspath(path), item)
        dst = os.path.join(os.path.abspath(new_path),item)
        new_name = os.path.join(os.path.abspath(new_path),''+str(i)+'.bmp')
       #复制图像
        shutil.copy(src,dst)
       #重命名
        os.rename(dst, new_name)
        i += 1

        print(src)
        print(new_name)

4.裁剪加复制到另一文件夹 每个文件夹均从1开始命名 整体程序

import os
import shutil
from PIL import Image

#用PIL库批量裁剪指定大小的图像(自动填充)
def img_crop(img_path, save_path):
    files = os.listdir(img_path)
    files.sort(key=lambda x: int(float(x[:-4])))
    for file in files:
        a, b = os.path.splitext(file)
        img = Image.open(os.path.join(img_path + "/" + file))
        width, hight = img.size
        rewidth = width*0.65  # 需要切成图片块的大小,默认大小为w*w,可以自己设置
        rehight = hight
        id = 1
        i = 0
        padw = padh = 0  # 当宽高除不尽切块大小时,对最后一块进行填充
        if width % rewidth != 0:
            padw = 1  # 宽除不尽的情况
        if hight % rehight != 0:
            padh = 1  # 高除不尽的情况
        # 默认从最左上角向右裁剪,再向下裁剪
        while i + rehight <= hight:
            j = 0
            while j + rewidth <= width:
                new_img = img.crop((j, i, j + rewidth, i + rehight))
                new_img.save(save_path + a + "_" + str(id) + b)
                id += 1
                j += rewidth
            if padw == 1:  # 宽有除不尽的情况
                new_img = img.crop((width - rewidth, i, width, i + rehight))
                new_img.save(save_path + a + "_" + str(id) + b)
                id += 1
            i = i + rehight
        if padh == 1:  # 高除不尽的情况
            j = 0
            while j + rewidth <= width:
                new_img = img.crop((j, hight - rehight, j + rewidth, hight))
                new_img.save(save_path + a + "_" + str(id) + b)
                id += 1
                j += rewidth
            if padw == 1:
                new_img = img.crop((width - rewidth, hight - rehight, width, hight))
                new_img.save(save_path + a + "_" + str(id) + b)
                id += 1


if __name__ == '__main__':
    # img_path:输入图像的路径
    img_path = 'F:/Corel5K-master/test/'
    # save_path:图像保存的位置
    save_path1 = 'F:/Corel5K-master/test_crop/'
    img_crop(img_path, save_path1)
    # save_path2 = 'F:/Corel5K-master/test_crop2/'
    save_path2 = 'F:/Corel5K-master/test_crop1/'
    save_path3 = 'F:/Corel5K-master/test_crop2/'
    filelist = os.listdir(save_path1)
    filelist.sort(key=lambda x: int(float(x[:-4])))
    i = 1
    j = 1
    for item in filelist:
        if item.endswith('_1.jpeg'):
            src = os.path.join(os.path.abspath(save_path1), item)
            dst1 = os.path.join(os.path.abspath(save_path2), item)
            new_name1 = os.path.join(os.path.abspath(save_path2), '' + str(i) + '.jpeg')
            # 复制图像
            shutil.copy(src, dst1)
            # 重命名
            os.rename(dst1, new_name1)
            i += 1
            print(src)
            print(new_name1)
        if item.endswith('_2.jpeg') :
            src = os.path.join(os.path.abspath(save_path1), item)
            dst2 = os.path.join(os.path.abspath(save_path3), item)
            new_name2 = os.path.join(os.path.abspath(save_path3), '' + str(j) + '.jpeg')
            # 复制图像
            shutil.copy(src, dst2)
            # 重命名
            os.rename(dst2, new_name2)
            j += 1
            print(src)
            print(new_name2)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值