我就廢話不多說了,直接上代碼吧!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import Image from datetime import datetime import os str = '/home/dltest/caffe/examples/sgg_datas/images/result_test/zutest/' + datetime.now().strftime( "%Y%m%d_%H%M%S" ) while True = = os.path.exists( str ): str = str + datetime.now().strftime( "%Y%m%d_%H%M%S" ) os.makedirs( str ) #創(chuàng)建文件夾 imageFile = '/home/dltest/caffe/examples/sgg_datas/images/result_test/zutest/20form1.bmp' #圖片路徑 import imghdr imgType = imghdr.what(imageFile) #獲取圖像類型,返回bmp,jpg等 type1 = cmp (imgType, 'bmp' ) #判斷是否為bmp,jpg類型,若字符相等則返回值為 0 type2 = cmp (imgType, 'jpeg' ) type3 = cmp (imgType, 'jpg' ) type = type1 * type2 * type3 #判斷是否為三種類型中的一類 if type ! = 0 : #進(jìn)行圖像類型轉(zhuǎn)換,轉(zhuǎn)換為 jpg格式 im = Image. open (imageFile) str2 = str + '/' + '1.jpg' #會(huì)自動(dòng)替換原來的1.jpg im.save(str2) print ( '1' ) |
以上這篇Python 實(shí)現(xiàn)判斷圖片格式并轉(zhuǎn)換,將轉(zhuǎn)換的圖像存到生成的文件夾中就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/u010417185/article/details/52293497