Python 实现判断图片格式并转换,将转换的图像存到生成的文件夹中
作者:修炼打怪的小乌龟 时间:2023-07-19 04:13:23
我就废话不多说了,直接上代码吧!
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) #创建文件夹
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')
来源:https://blog.csdn.net/u010417185/article/details/52293497
标签:Python,图片格式,转换,文件夹
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
09年QQ登录Banner之转变
2009-02-16 12:53:00
![](https://img.aspxhome.com/file/UploadPic/20092/16/09qqbanner_5-69.jpg)
python中的decimal类型转换实例详解
2022-05-01 15:05:21
Python使用smtp和pop简单收发邮件完整实例
2022-01-07 05:48:40
SQL语句学习
2024-01-18 15:07:00
js前端实现登录拼图验证功能
2024-04-19 10:45:29
![](https://img.aspxhome.com/file/2023/9/135779_0s.jpg)
Golang开发gRPC服务入门介绍
2024-02-14 09:56:20
![](https://img.aspxhome.com/file/2023/7/105127_0s.jpg)
Python安全获取域管理员权限几种方式操作示例
2022-10-04 01:14:15
![](https://img.aspxhome.com/file/2023/6/89236_0s.png)
Python读取配置文件-ConfigParser的二次封装方法
2023-06-15 09:19:58
![](https://img.aspxhome.com/file/2023/1/59651_0s.png)
python 中使用yagmail 发送邮件功能
2022-10-27 11:48:36
![](https://img.aspxhome.com/file/2023/4/121774_0s.png)
原生js实现移动小球(碰撞检测)
2024-04-19 09:56:01
![](https://img.aspxhome.com/file/2023/5/136005_0s.jpg)
浅谈javascript 函数表达式和函数声明的区别
2024-04-27 15:19:39
![](https://img.aspxhome.com/file/2023/9/135509_0s.jpg)
python实现上传样本到virustotal并查询扫描信息的方法
2023-06-30 19:59:45
使用Python+Flask开发博客项目并实现内网穿透
2021-11-03 14:23:37
![](https://img.aspxhome.com/file/2023/4/127084_0s.jpg)
python实现简单socket程序在两台电脑之间传输消息的方法
2021-04-25 21:14:26
用 Python 定义 Schema 并生成 Parquet 文件详情
2021-09-24 14:14:18
python打印9宫格、25宫格等奇数格 满足横竖斜相加和相等
2023-08-27 07:55:11
![](https://img.aspxhome.com/file/2023/7/72977_0s.jpg)
服务器响应HTTP的类型ContentType大全
2007-10-23 10:21:00
如何使用SQLServer数据库查询累计值
2009-03-16 14:43:00
jQuery实现同一点击,两个不同链接,指向两个不同的iframe
2010-06-21 10:52:00
Python Web框架Flask中使用新浪SAE云存储实例
2022-11-03 06:27:16