日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

如何用python批量处理图片大小_python批量修改图片大小的方法

發布時間:2024/9/18 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何用python批量处理图片大小_python批量修改图片大小的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例為大家分享了python批量修改圖片大小的具體代碼,供大家參考,具體內容如下

引用的模塊

from PIL import Image

Image的使用

def resize_image(img_path):

try:

mPath, ext = os.path.splitext(img_path)

if astrcmp(ext, ".png") or astrcmp(ext, ".jpg"):

img = Image.open(img_path)

(width, height) = img.size

if width != new_width:

new_height = int(height * new_width / width)

out = img.resize((new_width, new_height), Image.ANTIALIAS)

new_file_name = '%s%s' % (mPath, ext)

out.save(new_file_name, quality=100)

Py_Log("圖片尺寸修改為:" + str(new_width))

else:

Py_Log("圖片尺寸正確,未修改")

else:

Py_Log("非圖片格式")

except Exception, e:

print e

def printFile(dirPath):

print "file: " + dirPath

resize_image(dirPath)

return True

引用

if __name__ == '__main__':

path = "E:\pp\icon_setting.png"

new_width = 50

try:

b = printFile(path)

Py_Log("\r\n **********\r\n" + "*********圖片處理完畢*********" + "\r\n **********\r\n")

except:

print "Unexpected error:", sys.exc_info()

上述是修改單一的圖片,若要批量修改文件夾下的所有圖片,則要使用循環,在上面基礎添加 例如:

def BFS_Dir(dirPath, dirCallback=None, fileCallback=None):

queue = []

ret = []

queue.append(dirPath);

while len(queue) > 0:

tmp = queue.pop(0)

if os.path.isdir(tmp):

ret.append(tmp)

for item in os.listdir(tmp):

queue.append(os.path.join(tmp, item))

if dirCallback:

dirCallback(tmp)

elif os.path.isfile(tmp):

ret.append(tmp)

if fileCallback:

fileCallback(tmp)

return ret

第一個參數為圖片的目錄路徑,第二個參數是(目錄路勁的回掉方法),第三個參數是圖片處理回掉方法

源代碼參考:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持萬仟網。

如您對本文有疑問或者有任何想說的,請點擊進行留言回復,萬千網友為您解惑!

總結

以上是生活随笔為你收集整理的如何用python批量处理图片大小_python批量修改图片大小的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。