Python實(shí)現(xiàn)圖像尺寸和格式轉(zhuǎn)換處理的示例詳解
實(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)文章
kafka-python批量發(fā)送數(shù)據(jù)的實(shí)例
今天小編就為大家分享一篇kafka-python批量發(fā)送數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python標(biāo)準(zhǔn)庫(kù)06之子進(jìn)程 (subprocess包) 詳解
本篇文章主要介紹了Python標(biāo)準(zhǔn)庫(kù)06之子進(jìn)程 (subprocess包) 詳解,具有一定的參考價(jià)值,有興趣的同學(xué)可以了解一下。2016-12-12使用ITK-SNAP進(jìn)行摳圖操作并保存mask的實(shí)例
這篇文章主要介紹了使用ITK-SNAP進(jìn)行摳圖操作并保存mask的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07selenium python 實(shí)現(xiàn)基本自動(dòng)化測(cè)試的示例代碼
這篇文章主要介紹了selenium python 實(shí)現(xiàn)基本自動(dòng)化測(cè)試的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02Python操作sqlite3快速、安全插入數(shù)據(jù)(防注入)的實(shí)例
這篇文章主要介紹了Python操作sqlite3快速、安全插入數(shù)據(jù)(防注入)的實(shí)例,通過在一個(gè)表格中進(jìn)行操作來論述如何使用Python快速安全地操作sqlite3,需要的朋友可以參考下2014-04-04pycharm 使用心得(八)如何調(diào)用另一文件中的函數(shù)
事件環(huán)境: pycharm 編寫了函數(shù)do() 保存在make.py 如何在另一個(gè)file里調(diào)用do函數(shù)?2014-06-06python使用socket向客戶端發(fā)送數(shù)據(jù)的方法
這篇文章主要介紹了python使用socket向客戶端發(fā)送數(shù)據(jù)的方法,涉及Python使用socket實(shí)現(xiàn)數(shù)據(jù)通信的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04python讀取TXT到數(shù)組及列表去重后按原來順序排序的方法
這篇文章主要介紹了python讀取TXT到數(shù)組及列表去重后按原來順序排序的方法,涉及Python操作txt文件、列表去重及排序的相關(guān)技巧,需要的朋友可以參考下2015-06-06