亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

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

瀏覽:4日期: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],’開(kāi)始轉(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(’開(kāi)始重命名文件’) 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)文章:
主站蜘蛛池模板: 国产又黄又爽又色视频观看免费 | 亚洲精品自拍 | 日日草夜夜操 | 免费一级乱子伦片 | 亚洲精品网站在线观看不卡无广告 | 理论片我不卡在线观看 | 欧美色视频日本片高清在线观看 | 成人欧美午夜视频毛片 | 中文在线播放 | 九九精品视频一区在线 | 日本欧美成 | 久久精品亚洲精品一区 | 国产xxxx99真实实拍 | 久久亚洲综合 | 久久99国产一区二区三区 | 草久久| 亚洲国产乱 | 久久免费播放视频 | 亚洲ss| 日本三级免费网站 | 国产成人精品天堂 | 亚洲精品乱无伦码 | 亚洲免费精品 | 国产在线免| 91香蕉国产线在线观看免费 | 国产成人精彩在线视频50 | 国产精品一区二区 尿失禁 国产精品一区二区三 | 久久婷婷色香五月综合激情 | 免费国产一级特黄aa大片在线 | a级特黄毛片免费观看 | 日本欧美高清 | 久久成人小视频 | 午夜视频网站在线观看 | 精品国产免费观看 | 玖玖爱在线播放 | 日韩欧美亚洲国产一区二区三区 | 1024毛片| 老湿机一区午夜精品免费福利 | 一级特黄a 大片免费 | 天天综合色 | 天天影视色香欲综合网网站麻豆 |