Python實現(xiàn)圖像尺寸和格式轉(zhuǎn)換處理的示例詳解
更新時間:2023年04月04日 16:04:44 作者:飛仔FeiZai
這篇文章主要為大家詳細(xì)介紹了如何利用Python實現(xiàn)圖像尺寸獲取和格式轉(zhuǎn)換處理的功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
實現(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 產(chǎn)生token及token驗證的方法
今天小編就為大家分享一篇python 產(chǎn)生token及token驗證的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
pycharm的console輸入實現(xiàn)換行的方法
今天小編就為大家分享一篇pycharm的console輸入實現(xiàn)換行的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python 實戰(zhàn)開發(fā)校園管理系統(tǒng)詳細(xì)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python開發(fā)一套校園管理系統(tǒng),包含各種人員,如教師、學(xué)生等。學(xué)校的系統(tǒng)通常還包括一些課程的信息,大家可以在過程中查缺補漏,提升水平2021-10-10
Python數(shù)據(jù)可視化之分析熱門話題“丁克家庭都怎么樣了”
今天小編就以一個數(shù)據(jù)分析師的視角來向大家講述一下年輕人群體對于丁克的態(tài)度以及那些丁克家庭他們的想法是怎么樣的?他們是否有過后悔當(dāng)初的決定,需要的朋友可以參考下2021-06-06

