人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

python 批量壓縮圖片的腳本

發(fā)布日期:2022-03-26 16:42 | 文章來源:腳本之家

簡介

用Python批量壓縮圖片,把文件夾或圖片直接拖入即可

需要 Needs

Python 3

Pillow (用pip install pillow來安裝即可)

用法 Usage

把文件夾或圖片直接拖入即可。如果拖入的是文件夾,則會遍歷子文件夾把所有圖片都壓縮了。

注意,壓縮后的文件會直接替換原來的文件,文件名不變,尺寸不變,只改變壓縮質(zhì)量。

文件的開頭有兩個變量:

SIZE_CUT = 4 表示大于4MB的圖片都會進行壓縮

QUALITY = 90 表示壓縮質(zhì)量90,這個質(zhì)量基本人眼是看不出來啥差距的,而且很多原先10M的圖能壓縮一半。80以下的質(zhì)量大概就不太行了。

代碼

#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# Created by Mario Chen, 01.04.2021, Shenzhen
# My Github site: https://github.com/Mario-Hero
import sys
import os
from PIL import Image
SIZE_CUT = 4# picture over this size should be compressed. Units: MB
QUALITY = 90  # 90 is good, this number should not be smaller than 80.

def isPic(name):
 namelower = name.lower()
 return namelower.endswith("jpeg") or namelower.endswith("jpg") or namelower.endswith("png")

def compressImg(file):
 #print("The size of", file, "is: ", os.path.getsize(file))
 im = Image.open(file)
 im.save(file, quality=QUALITY)

def compress(folder):
 try:
  if os.path.isdir(folder):
print(folder)
file_list = os.listdir(folder)
for file in file_list:
 if os.path.isdir(folder+"/"+file):
  #print(folder +"/"+ file)
  compress(folder +"/"+file)
 else:
  if isPic(file):if os.path.getsize(folder + "/" + file) > (SIZE_CUT * 1024 * 1024):
compressImg(folder + "/" + file)
print(file)
  else:
if isPic(folder):
 if os.path.getsize(folder) > (SIZE_CUT * 1024 * 1024):
  compressImg(folder)
 except BaseException:
  return

if __name__ == '__main__':
 for folder in sys.argv:
  #print(folder)
  compress(folder)
 print("Finish.")
 #os.system("pause")

實現(xiàn)效果

壓縮后大小

另外一種圖片壓縮實現(xiàn)方式

同樣自動遍歷目錄下的圖片

import os
from PIL import Image
import threading,time
def imgToProgressive(path):
 if not path.split('.')[-1:][0] in ['png','jpg','jpeg']:  #if path isn't a image file,return
  return
 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 批量壓縮圖片的腳本的詳細內(nèi)容,更多關(guān)于python 批量壓縮圖片的資料請關(guān)注本站其它相關(guān)文章!

海外服務(wù)器租用

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部