-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcvt2Jpg
executable file
·56 lines (46 loc) · 1.27 KB
/
cvt2Jpg
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
#!/Users/user/opt/anaconda3/bin/python3
# -*- coding: UTF-8 -*-
from PIL import Image
import sys
def IsValidImage(img_path):
"""
判断文件是否为有效(完整)的图片
:param img_path:图片路径
:return:True:有效 False:无效
"""
bValid = True
try:
Image.open(img_path).verify()
except:
print("{} not valid image".format(img_path))
bValid = False
return bValid
def transimg(img_path):
"""
转换图片格式
:param img_path:图片路径
:return: True:成功 False:失败
"""
if IsValidImage(img_path):
try:
str = img_path.rsplit(".", 1)
output_img_path = str[0] + ".jpg"
print(output_img_path)
im = Image.open(img_path)
if im.mode in ("RGBA", "P"):
im = im.convert("RGB")
im.save(output_img_path)
return True
except Exception as e:
print(e)
return False
else:
return False
if __name__ == '__main__':
# img_path = 'wjk.png'
if len(sys.argv) < 2:
print('Params error!! Such as: cvt2Jpg [image1] [image2] ...')
exit(0)
img_list = sys.argv[1:]
for img_path in img_list:
print(transimg(img_path))