python批量壓縮圖像的完整步驟
背景
今天在工作中,同事遇到一個上傳圖片的問題:系統(tǒng)要求的圖片大小不能超過512KB。但是同事又有很多照片。這要是每一個照片都用ps壓縮的話,那豈不是很崩潰。于是我寫了一個腳本,可以批量壓縮圖片到指定大小。直接造福同事、提高同事的工作效率。
解決方案
其實也不用賣關(guān)子了,就是使用python的pillow包就可以對圖片進(jìn)行壓縮,如果一個圖片已經(jīng)壓縮到指定大小了,那就停止壓縮,如果沒有達(dá)到指定大小,那就對壓縮后的圖片再進(jìn)行壓縮,直到壓縮到自定范圍內(nèi)。
可是為什么不在網(wǎng)上找代碼呢?我也是找過,但是發(fā)現(xiàn)很多代碼質(zhì)量參差不齊,都達(dá)不到我想要的效果,而且很不優(yōu)雅。于是我隨手就寫了一個代碼,不僅僅代碼寫的簡單,而且邏輯清楚,最后為了效率,我還做了一個并行,同時使用10個進(jìn)程處理。說實話,那可是真的飛快。
操作步驟
要求
- 默認(rèn)是使用的是Anaconda的環(huán)境。
- 將所有要壓縮的圖片都放在一個文件夾下,然后每個圖片的格式只能是下面三種:png,jpg, jpeg。如果是PNG也不行。因為PNG是png的大寫。
- 代碼中設(shè)置的圖像的壓縮后的大小是512KB,那么你可以設(shè)置代碼中的target_size為500,只要比512KB小就行了。
- 然后把我的代碼從GitHub上下載下來。代碼鏈接為:https://github.com/yuanzhoulvpi2017/tiny_python/blob/main/image_compression/ic.py
步驟
我這里把所有圖片都放在了一個文件夾里面,文件夾名稱為歷史截圖。然后我的這個歷史截圖和ic.py代碼都放在了little_code文件夾中。
在little_code文件夾下,打開終端。
直接運行的腳本:
python ic.py xxx_文件夾
等待一會,就會將整個文件夾下的所有圖片都轉(zhuǎn)化好了。
完整代碼:
如果上不去GitHub的話,我直接把代碼放在這里,保存為一個python文件即可。比如保存的文件名為:ic.py
from PIL import Image from glob import glob import os from tqdm import tqdm import shutil import sys from itertools import chain from multiprocessing import Pool # image_dir = "image_dir" template_dir = 'template' output_dir = 'output' error_dir = 'error' def clean_dir(dir_name): if os.path.exists(dir_name): shutil.rmtree(dir_name) os.makedirs(dir_name) else: os.makedirs(dir_name) # image_file_list = glob(f"{image_dir}/*") # image_file_list def imagesize(filepath): """ 獲得文件的磁盤大小 :param filepath: :return: """ return os.path.getsize(filepath) / 1024 def compress_image(image_path): raw_image = Image.open(image_path) temp_image_name = image_path.split(os.sep)[-1] template_image = os.path.join(template_dir, temp_image_name) output_image = os.path.join(output_dir, temp_image_name) error_image = os.path.join(error_dir, temp_image_name) target_size = 500 # kb try: if imagesize(image_path) < target_size: shutil.copyfile(image_path, output_image) else: width, height = raw_image.size raw_image.resize((int(width * 0.9), int(height * 0.9)), Image.ANTIALIAS).save(template_image) while imagesize(template_image) > target_size: template_iamge2 = Image.open(template_image) width_2, height_2 = template_iamge2.size template_iamge2.resize((int(width_2 * 0.9), int(height_2 * 0.9)), Image.ANTIALIAS).save(template_image) shutil.copyfile(template_image, output_image) except Exception as e: shutil.copyfile(image_path, error_image) print(f'文件保存失敗: {image_path}') # print(e) if __name__ == '__main__': # 批量創(chuàng)建文件夾 [clean_dir(i) for i in [template_dir, output_dir, error_dir]] image_dir = sys.argv[1] image_file_list = list(chain(*[glob(os.path.join(image_dir, i)) for i in ['*.png', '*.jpg', '*.jpeg']])) # for temp_image_path in tqdm(image_file_list): # compress_image(temp_image_path) print(f'\n\n文件保存父目錄: {os.getcwd()}\n' f'輸出文件位置:{os.path.join(os.getcwd(), output_dir)}\n\n') # parallel P = Pool(processes=10) pbar = tqdm(total=len(image_file_list)) res_temp = [P.apply_async(func=compress_image, args=(i,), callback=lambda _: pbar.update(1)) for i in image_file_list] _ = [res.get() for res in res_temp]
附:批量將圖片的大小設(shè)置為指定大小
import os from PIL import Image # 源目錄 project_dir = os.path.dirname(os.path.abspath(__file__)) input = os.path.join(project_dir, 'src') # 輸出目錄 output = os.path.join(project_dir, 'dest') def modify(): # 切換目錄 os.chdir(input) # 遍歷目錄下所有的文件 for image_name in os.listdir(os.getcwd()): print(image_name) im = Image.open(os.path.join(input, image_name)) im.thumbnail((128, 128)) im.save(os.path.join(output, image_name)) if __name__ == '__main__': modify()
寫在后面
這個代碼說起來難,說起來也不難,如果認(rèn)真看我歷史的文章的話,上面代碼中遇到的知識點都就會了。像是所謂的圖像壓縮、并行處理之類的,其實并不難。
到此這篇關(guān)于python批量壓縮圖像的文章就介紹到這了,更多相關(guān)python批量壓縮圖像內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Python實現(xiàn)sqlite3增刪改查的封裝
在一些小的應(yīng)用中,難免會用到數(shù)據(jù)庫,Sqlite數(shù)據(jù)庫以其小巧輕便,無需安裝,移植性好著稱,下面這篇文章主要給大家介紹了關(guān)于利用Python實現(xiàn)sqlite3增刪改查的封裝,需要的朋友可以參考下2021-12-12Virtualenv 搭建 Py項目運行環(huán)境的教程詳解
這篇文章主要介紹了Virtualenv 搭建 Py項目運行環(huán)境的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06Python批量創(chuàng)建迅雷任務(wù)及創(chuàng)建多個文件
其實不是真的創(chuàng)建了批量任務(wù),而是用python創(chuàng)建一個文本文件,每行一個要下載的鏈接,然后打開迅雷,復(fù)制文本文件的內(nèi)容,迅雷監(jiān)測到剪切板變化,彈出下載全部鏈接的對話框2016-02-02基于opencv和pillow實現(xiàn)人臉識別系統(tǒng)(附demo)
人臉識別就是一個程序能識別給定圖像或視頻中的人臉,本文主要介紹了opencv和pillow實現(xiàn)人臉識別系統(tǒng),本文不涉及分類器、訓(xùn)練識別器等算法原理,感興趣的可以了解一下2021-11-11