欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python PIL庫讀取文件批量處理圖片大小實現(xiàn)

 更新時間:2023年07月21日 11:45:06   作者:BEARZPY  
這篇文章主要為大家介紹了使用Python PIL庫讀取文件批量處理圖片大小實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

python 批量處理圖片大小

寫博客,文檔的時候常常要附上很多圖片,但是圖片的尺寸往往不符合我們的要求,手動一個一個修改尺寸再保存,太浪費時間了,既然學(xué)了 Python,當(dāng)然就要物盡其用。本文實現(xiàn)了批量修改同級目錄下的 image\src 文件夾下的所有圖片,并輸出到 image\output 文件夾下,可以設(shè)定具體像素,也可以只設(shè)定水平像素或者垂直像素, 另一邊按原圖比例進行縮放。使用 Python 處理圖片大小需要使用 PIL 庫,PIL:Python Imaging Library,是一個圖片處理庫,需要自行安裝。

PIL 讀取圖片大小

下面是修改單張圖片尺寸的函數(shù)。

from PIL import Image
set_img_type = ""
def resize_image(filein, fileout, set_horizontal_length, set_vertical_length):
        if set_vertical_length == 0 and set_horizontal_length == 0:
            return
        img = Image.open(filein)
        img_size = img.size
        img_horizontal_length = img_size[0]
        img_vertical_length = img_size[1]
        if set_vertical_length == 0:
            set_vertical_length = set_horizontal_length * img_vertical_length / img_horizontal_length
        if set_horizontal_length == 0:
            set_horizontal_length = set_vertical_length * img_horizontal_length / img_vertical_length
        print img_horizontal_length, img_vertical_length
        print set_horizontal_length, set_vertical_length
        # resize image with high-quality
        out = img.resize((set_horizontal_length, set_vertical_length), Image.ANTIALIAS)
        if set_img_type == "":
            out.save(fileout)
        else:
            out.save(fileout, set_img_type)

代碼簡介

  • filein 圖片輸入文件名(帶路徑)。
  • fileout 圖片輸出文件名(帶路徑)。
  • set_horizontal_length 希望修改成的水平長度,如果為 0,自動使用原圖比例和設(shè)定的豎直長度計算該值。
  • set_vertical_length 希望修改成的豎直長度,如果為 0,自動使用原圖比例和設(shè)定的水平長度計算該值。
  • img.size 包含了兩個值,比如(1080, 1920),第一個值是寬度(水平長度),第二值是高度(豎直長度)。
  • set_img_type 如果為空,保持原圖片類型,如果非空則保存成對應(yīng)圖片類型,比如 set_img_type = "png",會把圖片以 png 數(shù)據(jù)格式進行保存。

PIL 批量處理圖片

上面已經(jīng)介紹了處理單張圖片的方法,想要處理文件夾下的所有圖片就要能遍歷文件夾里所有的文件。這里使用遞歸遍歷出所有的文件,再分別進行處理。

使用遞歸遍歷文件夾

def check_image(root_dir):
    if not os.path.isdir(root_dir):
        return
    for lists in os.listdir(root_dir):
        path = os.path.join(root_dir, lists)
        if os.path.isfile(path):
            # TODO: resize image.
        if os.path.isdir(path):
            check_image(path)

代碼簡介:

判斷當(dāng)前文件夾中的每個元素,如果是文件夾則進入該文件夾遞歸調(diào)用,并重復(fù)讀取判斷操作。如果是文件,則在代碼中的 TODO 標(biāo)簽處進行相應(yīng)處理。注:這里默認(rèn) image\src 文件夾下不會出現(xiàn)非圖片文件。

準(zhǔn)備輸出工作

目標(biāo)是把修改后的圖片輸出到 image\output 文件夾下,這里需要做三件事,在 output 目錄下創(chuàng)建對應(yīng) src 目錄下的子文件夾,生成圖片保存的文件名(帶路徑),修改生成圖片的后綴名。將遞歸代碼修改如下,增加了 find_last_point 函數(shù)用于找到后綴名前的小數(shù)點來修改后綴名。

import os
import os.path
def check_image(root_dir):
    if not os.path.isdir(root_dir):
        return
    for lists in os.listdir(root_dir):
        path = os.path.join(root_dir, lists)
        # handle output path of dir and file
        out_path = image_output_dir + path[9:]
        print path
        if os.path.isfile(path):
            # handle output file name
            point_position = find_last_point(out_path)
            if not set_img_type == "":
                out_path = out_path[0:point_position + 1] + set_img_type
                print out_path
            # resize image.
            resize_image(path, out_path, horizontal_length, vertical_length)
        if os.path.isdir(path):
            # make dir in image\output
            if not os.path.exists(out_path):
                os.mkdir(out_path)
                print out_path
            check_image(path)
def find_last_point(file_path):
    position = 0
    temp = 0
    while temp != -1:
        temp = file_path.find(".")
        if temp != -1:
            position = temp
            file_path = file_path[ temp + 1:]
    return position

清理 output 文件夾

如果只生成不清理,output 文件夾會越來越臃腫,想要找到轉(zhuǎn)換的圖片還需要花額外的時間,所以這里選擇程序剛開始就刪除整個 output 文件夾,并新建一個空的 output 文件夾。

import os
import os.path
import shutil
def clear():
    if os.path.exists(image_output_dir):
        shutil.rmtree(image_output_dir)
    if not os.path.exists(image_output_dir):
        os.mkdir(image_output_dir)

完整代碼

PIL 不支持修改為 jpg 后綴,所以如果設(shè)置生成 jpg 文件,程序會自動修改成 jpeg 文件。

import os
import os.path
import shutil
from PIL import Image
horizontal_length = 260
vertical_length = 0
image_src_dir = r"image\src"
image_output_dir = r"image\output"
set_img_type = ""
# set_img_type = "bmp"
# set_img_type = "jpeg"
# set_img_type = "png"
def test_run():
    if os.path.exists(image_output_dir):
        shutil.rmtree(image_output_dir)
    if not os.path.exists(image_output_dir):
        os.mkdir(image_output_dir)
    global set_img_type
    if set_img_type == "jpg":
        set_img_type = "jpeg"
    check_image(image_src_dir)
    print "finish."
def resize_image(filein, fileout, set_horizontal_length, set_vertical_length):
        if set_vertical_length == 0 and set_horizontal_length == 0:
            return
        img = Image.open(filein)
        img_size = img.size
        img_horizontal_length = img_size[0]
        img_vertical_length = img_size[1]
        if set_vertical_length == 0:
            set_vertical_length = set_horizontal_length * img_vertical_length / img_horizontal_length
        if set_horizontal_length == 0:
            set_horizontal_length = set_vertical_length * img_horizontal_length / img_vertical_length
        print img_horizontal_length, img_vertical_length
        print set_horizontal_length, set_vertical_length
        # resize image with high-quality
        out = img.resize((set_horizontal_length, set_vertical_length), Image.ANTIALIAS)
        if set_img_type == "":
            out.save(fileout)
        else:
            out.save(fileout, set_img_type)
def check_image(root_dir):
    if not os.path.isdir(root_dir):
        return
    for lists in os.listdir(root_dir):
        path = os.path.join(root_dir, lists)
        # handle output path of dir and file
        out_path = image_output_dir + path[9:]
        print path
        if os.path.isfile(path):
            # handle output file name
            point_position = find_last_point(out_path)
            if not set_img_type == "":
                out_path = out_path[0:point_position + 1] + set_img_type
                print out_path
            # resize image.
            resize_image(path, out_path, horizontal_length, vertical_length)
        if os.path.isdir(path):
            # make dir in image\output
            if not os.path.exists(out_path):
                os.mkdir(out_path)
                print out_path
            check_image(path)
def find_last_point(file_path):
    position = 0
    temp = 0
    i = 0
    while temp != -1:
        temp = file_path.find(".")
        if temp != -1:
            if i == 0:
              position = position + temp
            else:
              position = position + temp + 1
            file_path = file_path[temp+1:]
            i = 1
    return position
if __name__ == "__main__":
    test_run()

以上就是使用Python PIL庫讀取文件批量處理圖片大小實現(xiàn)的詳細內(nèi)容,更多關(guān)于Python PIL處理圖片大小的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Django部署到服務(wù)器后無法獲取到靜態(tài)元素 The requested resource was not found on this server(問題及解決方案)

    Django部署到服務(wù)器后無法獲取到靜態(tài)元素 The requested resource

    寫了一個Django項目,部署到云主機后,訪問發(fā)現(xiàn)圖片無法訪問,報錯The requested resource was not found on this server,下面給大家介紹Django部署到服務(wù)器后無法獲取到靜態(tài)元素The requested resource was not found on this server(問題及解決方案),需要的朋友可以參考下
    2024-02-02
  • python實現(xiàn)Excel多行多列的轉(zhuǎn)換的示例

    python實現(xiàn)Excel多行多列的轉(zhuǎn)換的示例

    本文主要介紹了python實現(xiàn)Excel多行多列的轉(zhuǎn)換的示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 使用Python操作Jenkins的過程詳解

    使用Python操作Jenkins的過程詳解

    借助于Python中的python-jenkins模塊,我們可以輕松地編寫腳本來連接到Jenkins服務(wù)器,并執(zhí)行各種操作,如創(chuàng)建、刪除、構(gòu)建Jobs等,這種自動化的方式不僅提高了效率,還使得CI/CD流程更加靈活和可控,本文介紹如何使用Python操作Jenkins的相關(guān)資料,需要的朋友可以參考下
    2024-05-05
  • Python跑循環(huán)時內(nèi)存泄露的解決方法

    Python跑循環(huán)時內(nèi)存泄露的解決方法

    這篇文章主要介紹了Python跑循環(huán)時內(nèi)存泄露的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • Python對象的生命周期源碼學(xué)習(xí)

    Python對象的生命周期源碼學(xué)習(xí)

    這篇文章主要為大家介紹了Python對象的生命周期源碼學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • 用Python編寫一個簡單的Lisp解釋器的教程

    用Python編寫一個簡單的Lisp解釋器的教程

    這篇文章主要介紹了用Python編寫一個簡單的Lisp解釋器的教程,Lisp是一種源碼簡單的函數(shù)式編程語言,本文主要介紹對其中的一個子集Scheme的解釋器開發(fā),需要的朋友可以參考下
    2015-04-04
  • Python設(shè)計模式結(jié)構(gòu)型組合模式

    Python設(shè)計模式結(jié)構(gòu)型組合模式

    這篇文章主要介紹了Python設(shè)計模式結(jié)構(gòu)型組合模式,組合模式即Composite?Pattern,將對象組合成成樹形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu),組合模式使得用戶對單個對象和組合對象的使用具有一致性,下文具有一定的參考價值,需要的小伙伴可以參考一下
    2022-02-02
  • Django和websocket實現(xiàn)簡單的多人聊天的示例代碼

    Django和websocket實現(xiàn)簡單的多人聊天的示例代碼

    本文主要介紹了使用Django和WebSocket實現(xiàn)一個簡單的多人聊天應(yīng)用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • tensorflow實現(xiàn)簡單的卷積神經(jīng)網(wǎng)絡(luò)

    tensorflow實現(xiàn)簡單的卷積神經(jīng)網(wǎng)絡(luò)

    這篇文章主要為大家詳細介紹了tensorflow實現(xiàn)簡單的卷積神經(jīng)網(wǎng)絡(luò),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • python 實現(xiàn)循環(huán)定義、賦值多個變量的操作

    python 實現(xiàn)循環(huán)定義、賦值多個變量的操作

    這篇文章主要介紹了python 實現(xiàn)循環(huán)定義、賦值多個變量的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評論