最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

實現(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

        # 限制最長邊或最短邊
        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-限制最長邊;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è)置最長邊數(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ù)值,高度按比例計算
    parser.add_argument('--limitwidth', type=str, default='1080', help='limit width')
    # 限制高度數(shù)值,寬度按比例計算
    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,并將格式轉(zhuǎn)換為 webp 格式
python batch_handle_image.py --input /input_image --output /output_image --limitshortest 0 --longestedge 2000 --towebp 1

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

相關(guān)文章

  • Python 尋找局部最高點的實現(xiàn)

    Python 尋找局部最高點的實現(xiàn)

    今天小編就為大家分享一篇Python 尋找局部最高點的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python進程管理工具supervisor使用實例

    python進程管理工具supervisor使用實例

    這篇文章主要介紹了python進程管理工具supervisor使用實例,本文介紹了supervisor的安裝、配置、使用等內(nèi)容,需要的朋友可以參考下
    2014-09-09
  • python 產(chǎn)生token及token驗證的方法

    python 產(chǎn)生token及token驗證的方法

    今天小編就為大家分享一篇python 產(chǎn)生token及token驗證的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python 圖像處理畫一個正弦函數(shù)代碼實例

    python 圖像處理畫一個正弦函數(shù)代碼實例

    這篇文章主要介紹了python 圖像處理畫一個正弦函數(shù)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • pyqt5 設(shè)置窗體透明控件不透明的操作

    pyqt5 設(shè)置窗體透明控件不透明的操作

    這篇文章主要介紹了pyqt5 設(shè)置窗體透明控件不透明的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • pycharm的console輸入實現(xiàn)換行的方法

    pycharm的console輸入實現(xiàn)換行的方法

    今天小編就為大家分享一篇pycharm的console輸入實現(xiàn)換行的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python必備技巧之字符數(shù)據(jù)操作詳解

    Python必備技巧之字符數(shù)據(jù)操作詳解

    處理字符數(shù)據(jù)是編程不可或缺的一部分。Python?提供了一組豐富的運算符、函數(shù)和方法來處理字符串。包括字符串運算符、內(nèi)置函數(shù)、索引、切片和內(nèi)置方法。快來學(xué)習(xí)一下吧
    2022-03-03
  • Python 實戰(zhàn)開發(fā)校園管理系統(tǒng)詳細流程

    Python 實戰(zhàn)開發(fā)校園管理系統(tǒng)詳細流程

    讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python開發(fā)一套校園管理系統(tǒng),包含各種人員,如教師、學(xué)生等。學(xué)校的系統(tǒng)通常還包括一些課程的信息,大家可以在過程中查缺補漏,提升水平
    2021-10-10
  • python實現(xiàn)自定義日志的具體方法

    python實現(xiàn)自定義日志的具體方法

    在本篇文章里小編給大家整理的是一篇關(guān)于python實現(xiàn)自定義日志的具體方法,有興趣的朋友們可以學(xué)習(xí)下。
    2021-05-05
  • Python數(shù)據(jù)可視化之分析熱門話題“丁克家庭都怎么樣了”

    Python數(shù)據(jù)可視化之分析熱門話題“丁克家庭都怎么樣了”

    今天小編就以一個數(shù)據(jù)分析師的視角來向大家講述一下年輕人群體對于丁克的態(tài)度以及那些丁克家庭他們的想法是怎么樣的?他們是否有過后悔當(dāng)初的決定,需要的朋友可以參考下
    2021-06-06

最新評論

昌黎县| 扎兰屯市| 宜阳县| 龙游县| 瓮安县| 外汇| 新泰市| 婺源县| 辽阳县| 安新县| 岗巴县| 洪江市| 凤城市| 桐庐县| 自贡市| 晋宁县| 山西省| 珲春市| 乐都县| 南平市| 金坛市| 仙居县| 淅川县| 永城市| 汝南县| 弋阳县| 唐海县| 石柱| 凯里市| 宁强县| 肥乡县| 南开区| 凤庆县| 犍为县| 晋中市| 湘潭市| 太谷县| 泸定县| 宁乡县| 荃湾区| 鹤庆县|