python 批量转图片大小__批量定宽转图(淘宝批量转图专用)

本次使用了PYHONT库的PIL 与OS 模块

PIL 模块是一个图像处理模块,如果没有,可以win+r---->cmd---->pip install pil
os 为python自带的模块,使用时直接导入即可。

代码1__转出的图片宽度与高度都是可以指定

// An highlighted block
# -*- coding: utf-8 -*-
from os import listdir,mkdir,path
from PIL import Image
 
class image_aspect():
 
    def __init__(self, image_file, aspect_width, aspect_height):
        self.img = Image.open(image_file)
        self.aspect_width = aspect_width
        self.aspect_height = aspect_height
        self.result_image = None
 
    def change_aspect_rate(self):
        img_width = self.img.size[0]
        img_height = self.img.size[1]
 
        if (img_width / img_height) > (self.aspect_width / self.aspect_height):
            rate = self.aspect_width / img_width
        else:
            rate = self.aspect_height / img_height
 
        rate = round(rate, 1)
        #print(rate)
        self.img = self.img.resize((int(img_width * rate), int(img_height * rate)))
        return self
 
    def past_background(self):
        self.result_image = Image.new("RGB", [self.aspect_width, self.aspect_height], (0, 0, 0, 255))
        self.result_image.paste(self.img, (int((self.aspect_width - self.img.size[0]) / 2), int((self.aspect_height - self.img.size[1]) / 2)))
        return self
 
    def save_result(self, file_name):
        self.result_image.save(file_name)
 
 
if __name__ == "__main__":
    path_in=r'D:\拼多多\窗帘\短帘-2021-04-11\old'#此处为照片所在的的目录,转好照片将在同一级目录
    width_out=800 #此处输入想要的照片宽度
    high_out=800 #此处输入想要的照片宽度
    path_new=path_in+'_out'
    if path.isdir(path_new)==False:
        mkdir(path_new)
    count=0
    for i in listdir(path_in):

        path_out=str(path_new)+r'\{}{}'.format(count,i)
        image_aspect(r"{}\{}".format(path_in, i), width_out, high_out).change_aspect_rate().past_background().save_result(r"{}".format(path_out))
        count+=1
        
print(f'已将目录:{path_in}中共计{count}张照片转成为{width_out}*{high_out}的格式')

代码2(淘宝批量转图专用)__转出的图片宽度指定,高度随原比例变化

// An highlighted block

# -*- coding: utf-8 -*-
from os import listdir,mkdir,path
from PIL import Image
 
class image_aspect():
 
    def __init__(self, image_file, aspect_width):
        
        self.img = Image.open(image_file)
        self.aspect_width = aspect_width
        #self.aspect_height = aspect_height
        self.result_image = None
 
    def change_aspect_rate(self):
        img_width = self.img.size[0]
        img_height = self.img.size[1]
 
        #if (img_width / img_height) > (self.aspect_width / self.aspect_height):
        rate = self.aspect_width / img_width
        #else:
            #rate = self.aspect_height / img_height
 
        self.rate = round(rate, 1)
        #print(rate)
        self.img = self.img.resize((int(img_width * rate), int(img_height * rate)))
        return self#,rate
 
    def past_background(self):
        high=int(self.img.size[1]*self.rate)
        self.result_image = Image.new("RGB", [self.aspect_width, high], (0, 0, 0, 255))
        self.result_image.paste(self.img, (int((self.aspect_width - self.img.size[0]) / 2), int((high - self.img.size[1]) / 2)))
        return self
 
    def save_result(self, file_name):
        self.result_image.save(file_name)
 
 
if __name__ == "__main__":
    path_in=r'D:\拼多多\窗帘\短帘-2021-04-11\old'#此处为照片所在的的目录,转好照片将在同一级目录
    width_out=800 #此处输入想要的照片宽度
    path_new=path_in+'_out'
    if path.isdir(path_new)==False:
        mkdir(path_new)
        print(path_new,'目录已建立')
    else:
        print('目录已存在')
        pass
    count=0
    for i in listdir(path_in):
        path_out=path_new+r'\{}{}'.format(count,i)
        image_aspect(r"{}\{}".format(path_in, i), width_out).change_aspect_rate().past_background().save_result(r"{}".format(path_out))
        count+=1
        
print(f'已将目录:{path_in}中共计{count}张照片转成宽度为{width_out}的格式')



写在最后

本帖的大部分代码来源于这位博主https://blog.csdn.net/huuinn/article/details/78746400?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-9.control&dist_request_id=&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-9.control

本人在原博基础上增加了批量操作及增加了定宽图片操作
[1]: http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference
[2]: https://mermaidjs.github.io/
[3]: https://mermaidjs.github.io/
[4]: http://adrai.github.io/flowchart.js/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值