Python 實現(xiàn)判斷圖片格式并轉換,將轉換的圖像存到生成的文件夾中
更新時間:2020年01月13日 15:18:02 作者:修煉打怪的小烏龜
今天小編就為大家分享一篇Python判斷圖片格式并轉換,將轉換的圖像存到生成的文件夾中,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說了,直接上代碼吧!
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 : #進行圖像類型轉換,轉換為 jpg格式 im = Image.open(imageFile) str2 = str + '/' + '1.jpg' #會自動替換原來的1.jpg im.save(str2) print('1')
以上這篇Python 實現(xiàn)判斷圖片格式并轉換,將轉換的圖像存到生成的文件夾中就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
jupyter notebook使用argparse傳入list參數(shù)
這篇文章主要介紹了jupyter notebook使用argparse傳入list參數(shù),jupyter notebook其實是可以使用 argparse來調用參數(shù)的,只要把參數(shù)轉為list即可,下面來看看具體的實現(xiàn)過程吧2022-01-01