Python实现图像尺寸和格式转换处理的示例详解

作者:飞仔FeiZai 时间:2021-02-17 06:33:10 

实现代码

# batch_handle_image.py

import argparse
import glob
import os
from PIL import Image

def main(args):
   limit_shortest = int(args.limitshortest)
   shortest_edge = int(args.shortestedge)
   longest_edge = int(args.longestedge)
   limit_width_or_height = int(args.limitwidthorheight)
   limit_width = int(args.limitwidth)
   limit_height = int(args.limitheight)
   to_webp = int(args.towebp)

path_list = sorted(glob.glob(os.path.join(args.input, '*')))
   for path in path_list:
       print(path)
       basename = os.path.splitext(os.path.basename(path))[0]

img = Image.open(path)
       width, height = img.size

# 限制最长边或最短边
       if limit_shortest == 1:
           # save the smallest image which the shortest edge is shortest_edge
           if width < height:
               ratio = height / width
               width = shortest_edge
               height = int(width * ratio)
           else:
               ratio = width / height
               height = shortest_edge
               width = int(height * ratio)
       elif limit_shortest == 0:
           # save the smallest image which the longest edge is longest_edge
           if width < height:
               ratio = width / height
               height = longest_edge
               width = int(height * ratio)
           else:
               ratio = height / width
               width = longest_edge
               height = int(width * ratio)

# 限制宽或高
       if limit_width_or_height == 0:
           # 限宽
           ratio = height / width
           width = limit_width
           height = int(width * ratio)
       elif limit_width_or_height == 1:
           # 限高
           ratio = width / height
           height = limit_height
           width = int(height * ratio)

idx = 0

rlt = img.resize((int(width), int(height)), resample=Image.ANTIALIAS)
       rlt = rlt.convert('RGB')
       rlt.save(os.path.join(args.output, f'{basename}T{idx+1}.png'), 'PNG')
       if to_webp == 1:
           os.makedirs(os.path.join(args.output, 'to_webp'), exist_ok=True)
           # 转换为 webp 格式图片
           rlt.save(os.path.join(args.output, 'to_webp', f'{basename}T{idx+1}.webp'), 'WEBP')

if __name__ == '__main__':
   """batch modify image size, and convert to webp
   """
   parser = argparse.ArgumentParser()
   parser.add_argument('--input', type=str, default='datasets/MY/YT', help='Input folder')
   parser.add_argument('--output', type=str, default='datasets/MY/YT_smallsize', help='Output folder')
   # 是否限制最短边开关:0-限制最长边;1-限制最短边;2-不限制
   parser.add_argument('--limitshortest', type=str, default='2', help='0-limit longest; 1-limit shortest; 2-not limit')
   # 设置最短边数值
   parser.add_argument('--shortestedge', type=str, default='500', help='shortest edge size')
   # 设置最长边数值
   parser.add_argument('--longestedge', type=str, default='2000', help='longest edge size')
   # 是否转换 webp 格式图像开关:0-不转换;1-转换
   parser.add_argument('--towebp', type=str, default='0', help='is convert to webp, 0-false, 1-true')
   # 是否限制宽度或高度数值开关
   parser.add_argument(
       '--limitwidthorheight',
       type=str,
       default='2',
       help='is limit width or height; 0-limit width; 1-limit height; 2-not limit')
   # 限制宽度数值,高度按比例计算
   parser.add_argument('--limitwidth', type=str, default='1080', help='limit width')
   # 限制高度数值,宽度按比例计算
   parser.add_argument('--limitheight', type=str, default='1080', help='limit height')
   args = parser.parse_args()

os.makedirs(args.output, exist_ok=True)
   main(args)

使用命令

# 限最长边 2000px,并将格式转换为 webp 格式
python batch_handle_image.py --input /input_image --output /output_image --limitshortest 0 --longestedge 2000 --towebp 1

来源:https://www.cnblogs.com/yuzhihui/p/17286675.html

标签:Python,图像,尺寸,转换
0
投稿

猜你喜欢

  • PyTorch中的CUDA的操作方法

    2022-02-24 18:54:41
  • python操作链表的示例代码

    2023-08-08 23:55:41
  • SQL Server 2000安装故障

    2008-01-22 19:10:00
  • Python 读取千万级数据自动写入 MySQL 数据库

    2023-11-08 09:25:47
  • 如何利用Python和matplotlib更改纵横坐标刻度颜色

    2022-06-02 04:00:03
  • 用XML创建可排序、分页的数据显示页面

    2008-04-22 18:25:00
  • FCKeditor 编辑器实战技巧 Ⅰ

    2008-10-08 10:22:00
  • 简洁的是最好的吗?

    2010-06-24 21:46:00
  • banner字体设计与应用

    2009-07-06 14:42:00
  • ASP 高级模板引擎实现类

    2011-03-25 10:54:00
  • TensorFlow神经网络学习之张量与变量概念

    2023-07-06 20:58:02
  • python数学建模是加深Numpy和Pandas学习

    2021-04-28 17:21:12
  • asp伪静态情况下实现的utf-8文件缓存实现代码

    2011-02-24 10:49:00
  • javascript修正12个浏览器兼容问题[译]

    2009-04-23 12:19:00
  • php完全过滤HTML,JS,CSS等标签

    2023-10-09 08:07:34
  • 用javascript做拖动层布局的思路

    2008-05-30 13:38:00
  • 2008农历新年各大网站Logo秀

    2008-02-11 16:33:00
  • asp如何用组件实现自动发送电子邮件?

    2010-06-16 09:56:00
  • phpstorm断点调试方法图文详解

    2023-05-30 01:06:40
  • php中替换字符串函数strtr()和str_repalce()的用法与区别

    2023-11-17 06:12:53
  • asp之家 网络编程 m.aspxhome.com