av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

python實(shí)現(xiàn)圖片批量壓縮

瀏覽:7日期:2022-06-21 14:08:42

項(xiàng)目中大量用到圖片加載,由于圖片太大,加載速度很慢,因此需要對(duì)文件進(jìn)行統(tǒng)一壓縮

第一種 一:安裝包

python -m pip install Pillow二:導(dǎo)入包

from PIL import Imageimport os三:獲取圖片文件的大小

def get_size(file): # 獲取文件大小:KB size = os.path.getsize(file) return size / 1024四:輸出文件夾下的文件

dir_path = r’file_path’items = os.listdir(dir_path)for item in items: # print(item) path = os.path.join(dir_path, item) print(item)五:壓縮文件到指定大小,我期望的是150KB,step和quality可以修改到最合適的數(shù)值

def compress_image(infile, outfile=None, mb=150, step=10, quality=80): '''不改變圖片尺寸壓縮到指定大小 :param infile: 壓縮源文件 :param outfile: 壓縮文件保存地址 :param mb: 壓縮目標(biāo),KB :param step: 每次調(diào)整的壓縮比率 :param quality: 初始?jí)嚎s比率 :return: 壓縮文件地址,壓縮文件大小 ''' if outfile is None:outfile = infile o_size = get_size(infile) if o_size <= mb:im = Image.open(infile)im.save(outfile) while o_size > mb:im = Image.open(infile)im.save(outfile, quality=quality)if quality - step < 0: breakquality -= stepo_size = get_size(outfile)六:修改圖片尺寸,如果同時(shí)有修改尺寸和大小的需要,可以先修改尺寸,再壓縮大小

def resize_image(infile, outfile=’’, x_s=800): '''修改圖片尺寸 :param infile: 圖片源文件 :param outfile: 重設(shè)尺寸文件保存地址 :param x_s: 設(shè)置的寬度 :return: ''' im = Image.open(infile) x, y = im.size y_s = int(y * x_s / x) out = im.resize((x_s, y_s), Image.ANTIALIAS) out.save(outfile)七:運(yùn)行程序

if __name__ == ’__main__’: # 源路徑 # 壓縮后路徑 compress_image(r'file_path', r'E:docs2.JPG') # 源路徑 # 壓縮后路徑 resize_image(r'file_path', r'E:docs3.JPG')第二種

import osfrom PIL import Imageimport threading,timedef imgToProgressive(path): if not path.split(’.’)[-1:][0] in [’png’,’jpg’,’jpeg’]: #if path isn’t a image file,returnreturn if os.path.isdir(path):return##########transform img to progressive img = Image.open(path) destination = path.split(’.’)[:-1][0]+’_destination.’+path.split(’.’)[-1:][0] try:print(path.split(’’)[-1:][0],’開始轉(zhuǎn)換圖片’)img.save(destination, 'JPEG', quality=80, optimize=True, progressive=True) #轉(zhuǎn)換就是直接另存為print(path.split(’’)[-1:][0],’轉(zhuǎn)換完畢’) except IOError:PIL.ImageFile.MAXBLOCK = img.size[0] * img.size[1]img.save(destination, 'JPEG', quality=80, optimize=True, progressive=True)print(path.split(’’)[-1:][0],’轉(zhuǎn)換完畢’) print(’開始重命名文件’) os.remove(path) os.rename(destination,path)for d,_,fl in os.walk(os.getcwd()): #遍歷目錄下所有文件 for f in fl:try: imgToProgressive(d+’’+f)except: pass

以上就是python實(shí)現(xiàn)圖片批量壓縮的詳細(xì)內(nèi)容,更多關(guān)于python 圖片壓縮的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 欧美日韩大片 | 少妇一级淫片免费观看 | 黄色大片免费在线观看 | 国产h片在线观看 | 欧美精品日韩少妇 | 毛片在线观看网站 | 婷婷久久综合 | 18岁毛片| 国产aⅴ爽av久久久久成人 | 蜜臀久久99精品久久久久宅男 | 国产91在线观看 | 日韩亚洲欧美在线 | 福利网站在线观看 | 福利小视频在线观看 | 国产无遮挡又黄又爽免费网站 | 一区二区不卡 | 精品三级在线观看 | 天天干夜夜拍 | av手机在线免费观看 | 麻豆国产一区二区三区四区 | 久久久www成人免费精品 | av在线播放免费 | 欧美日韩亚洲一区二区三区 | 手机在线毛片 | 亚洲在线一区 | 九九九色 | 精品日韩一区二区三区 | cao在线 | 精品日韩一区二区三区 | 亚洲天堂久久 | 91久久久久 | 久久久久精 | 久久狠狠干 | 日韩在线综合 | 91精品成人 | 欧美另类综合 | 成人aaaa| 国产乱码久久久久久 | 精品亚洲国产成人av制服丝袜 | 中文字幕亚洲精品 | 精品久久一区二区三区 |