forked from crisschan/emma_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_change.py
More file actions
56 lines (53 loc) · 1.57 KB
/
image_change.py
File metadata and controls
56 lines (53 loc) · 1.57 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/9/2 4:38 PM
# @Author : Criss Chan
# @Site : https://blog.csdn.net/crisschan
# @File : image_chage.py
# @Software: PyCharm
# @instruction:所有图片的转换类
from PIL import Image
import os
class ImageChange(object):
def __init__(self,image_inpath,image_outpath):
'''
:param image_path: 图片的路径
'''
self.image_inpath=image_inpath
self.image_outpath=image_outpath
def IsValidImage(self,img_path):
'''
判断文件是否为有效(完整)的图片
:param img_path:图片路径
:return:True:有效 False:无效
'''
bValid = True
try:
Image.open(img_path).verify()
except:
bValid = False
return bValid
def ChangeImage(self):
'''
转换图片格式
:param img_path:图片路径
:return: True:成功 False:失败
'''
if self.IsValidImage(self.image_inpath):
try:
str = self.image_inpath.rsplit(".", 1)
output_img_path = str[0] + ".jpg"
im = Image.open(self.image_inpath)
im.save(self.image_outpath)
return True
except:
return False
else:
return False
#
# if __name__ == '__main__':
# ff = os.getcwd()
# for root, dirs, files in os.walk(ff+'/in/'):
# for afile in files:
# ic = ImageChange(ff+'/in/'+afile,ff+'/out/')
# ic.ChangeImage()