脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - Python实现图像尺寸和格式转换处理的示例详解

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

2023-04-06 17:49飞仔FeiZai Python

这篇文章主要为大家详细介绍了如何利用Python实现图像尺寸获取和格式转换处理的功能,文中的示例代码讲解详细,感兴趣的可以了解一下

实现代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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)

使用命令

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

到此这篇关于Python实现图像尺寸和格式转换处理的示例详解的文章就介绍到这了,更多相关Python图像内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/yuzhihui/p/17286675.html

延伸 · 阅读

精彩推荐