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

python批量圖片處理簡單示例

 更新時間:2019年08月06日 12:11:03   作者:沈偉-測試前行者  
這篇文章主要介紹了python批量圖片處理,涉及Python針對圖片文件的遍歷、判斷、縮放等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了python批量圖片處理。分享給大家供大家參考,具體如下:

#!/usr/bin/python
#coding:utf-8
import os
from PIL import Image
#源目錄
MyPath = 'C:/Users/Eric/Desktop/python_text/20161214/test_Image/'
#輸出目錄
OutPath = 'C:/Users/Eric/Desktop/python_text/20161214/outpath/'
def processImage(filesoure, destsoure, name, imgtype):
  '''
  filesoure是存放待轉(zhuǎn)換圖片的目錄
  destsoure是存在輸出轉(zhuǎn)換后圖片的目錄
  name是文件名
  imgtype是文件類型
  '''
  imgtype = 'jpeg' if imgtype == '.jpg' else 'png'
  #打開圖片
  im = Image.open(filesoure + name)
  #縮放比例
  rate =max(im.size[0]/640.0 if im.size[0] > 60 else 0, im.size[1]/1136.0 if im.size[1] > 1136 else 0)
  if rate:
    im.thumbnail((im.size[0]/rate, im.size[1]/rate))
  im.save(destsoure + name, imgtype)
def run():
  #切換到源目錄,遍歷源目錄下所有圖片
  os.chdir(MyPath)
  for i in os.listdir(os.getcwd()):
    #檢查后綴
    postfix = os.path.splitext(i)[1]
    if postfix == '.jpg' or postfix == '.png':
      processImage(MyPath, OutPath, i, postfix)
if __name__ == '__main__':
  run()

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

最新評論