Python 批量处理图像尺寸

import os

from PIL import Image, ImageDraw, ImageFont


class ImageMerger():
    def __init__(self):
        self.font = ImageFont.truetype("font\\simhei.ttf", 18)

    def merge(self, src_img_path, des_width, des_height=None):
        if os.path.exists(src_img_path) is False:
            print("图片不存在", src_img_path)
            return
        src_im = Image.open(src_img_path)
        orate = src_im.width / src_im.height;
        width = des_width;
        height = des_height;
        if des_height is None:
            height = width / orate;
            height=int(height)
        #print(width, height)
        if src_im.width > src_im.height:
            re_width = width
            re_height = re_width / orate;
            re_width=int(re_width)
            re_height=int(re_height)
            src_im = src_im.resize((re_width, re_height),Image.ANTIALIAS)
        else:
            re_height = height;
            re_width = orate * re_height
            re_width = int(re_width)

            src_im = src_im.resize((re_width, re_height),Image.ANTIALIAS)
        #print("调整之后", src_im.width, src_im.height)
        des_im = Image.new('RGB', (width, height), 0xffffff)
        box = self.get_box_pos(width, height, src_im.width, src_im.height)
        #print("粘贴区域",box)
        des_im.paste(src_im, box)
        des_im.save(src_img_path)

    def get_box_pos(self, des_width, des_height, src_width, src_height):
        del_width = des_width - src_width
        del_height = des_height - src_height;
        x1 = del_width / 2;
        y1 = del_height / 2
        x2 = x1 + src_width;
        y2 = y1 + src_height;
        x1 = int(x1)
        y1 = int(y1)
        x2 = int(x2)
        y2 = int(y2)
        return (x1, y1, x2, y2)

import os

from pic_merger import ImageMerger


class BatchResize():
    def __init__(self):
        self.main_dir = "C://software//导数据//AllCarRacing"
        self.resizer = ImageMerger()

    def get_file_ex(self,path):
        try:
            return os.path.splitext(path)[1]
        except:
            return None;

    def resize(self):
        file_list = os.listdir(self.main_dir)
        for item in file_list:
            banner_dir = self.main_dir + "//" + item + "//banner"
            banner_imgs = os.listdir(banner_dir)
            for banner_img in banner_imgs:
                banner_img_path = banner_dir + "//" + banner_img;
                if self.get_file_ex(banner_img_path)!=".jpg":
                    continue;
                try:
                    self.resizer.merge(banner_img_path, 1000, 1000)
                except:
                    print("文件错误", banner_img_path)

            showpic_dir = self.main_dir + "//" + item + "//showpic"
            showpic_imgs = os.listdir(showpic_dir)
            for showpic_img in showpic_imgs:
                showpic_img_path = showpic_dir + "//" + showpic_img;

                try:
                    self.resizer.merge(showpic_img_path, 1000)
                except:
                    print("文件错误", showpic_img_path)


resizer = BatchResize()
resizer.resize()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值