欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)圖像尺寸和格式轉(zhuǎn)換處理的示例詳解

 更新時(shí)間:2023年04月04日 16:04:44   作者:飛仔FeiZai  
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)圖像尺寸獲取和格式轉(zhuǎn)換處理的功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下

實(shí)現(xiàn)代碼

# 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

        # 限制最長(zhǎng)邊或最短邊
        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)
            # 轉(zhuǎn)換為 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')
    # 是否限制最短邊開關(guān):0-限制最長(zhǎng)邊;1-限制最短邊;2-不限制
    parser.add_argument('--limitshortest', type=str, default='2', help='0-limit longest; 1-limit shortest; 2-not limit')
    # 設(shè)置最短邊數(shù)值
    parser.add_argument('--shortestedge', type=str, default='500', help='shortest edge size')
    # 設(shè)置最長(zhǎng)邊數(shù)值
    parser.add_argument('--longestedge', type=str, default='2000', help='longest edge size')
    # 是否轉(zhuǎn)換 webp 格式圖像開關(guān):0-不轉(zhuǎn)換;1-轉(zhuǎn)換
    parser.add_argument('--towebp', type=str, default='0', help='is convert to webp, 0-false, 1-true')
    # 是否限制寬度或高度數(shù)值開關(guān)
    parser.add_argument(
        '--limitwidthorheight',
        type=str,
        default='2',
        help='is limit width or height; 0-limit width; 1-limit height; 2-not limit')
    # 限制寬度數(shù)值,高度按比例計(jì)算
    parser.add_argument('--limitwidth', type=str, default='1080', help='limit width')
    # 限制高度數(shù)值,寬度按比例計(jì)算
    parser.add_argument('--limitheight', type=str, default='1080', help='limit height')
    args = parser.parse_args()

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

使用命令

# 限最長(zhǎng)邊 2000px,并將格式轉(zhuǎn)換為 webp 格式
python batch_handle_image.py --input /input_image --output /output_image --limitshortest 0 --longestedge 2000 --towebp 1

到此這篇關(guān)于Python實(shí)現(xiàn)圖像尺寸和格式轉(zhuǎn)換處理的示例詳解的文章就介紹到這了,更多相關(guān)Python圖像內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論