-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyImg.py
More file actions
73 lines (62 loc) · 1.52 KB
/
MyImg.py
File metadata and controls
73 lines (62 loc) · 1.52 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import imghdr
from PIL import Image
def is_img(f):
'''
根据后缀判断文件是否为图片
:param f: 图片路径
:return: bool
'''
return imghdr.what(f)
def wh_type(f):
'''
判断图片类型
:param f: 图片路径
:return: int
'''
im = Image.open(f)
if im.width == im.height: # 方图
return 0
elif im.width > im.height: # 横图
return 1
elif im.width < im.height: # 竖图
return 2
else: # 未知
return 3
def hist(f):
'''
图片像素直方图
:param f: 图片路径
:return: List
'''
im = Image.open(f)
return im.histogram()
def wh_limitByPor(f, max_scale: float, min_scale: float):
'''
根据长宽比例限制
:param f: 图片路径
:param type: 图片类型
:param max_scale,min_scale: 最大/最小宽高比例
:return: bool
'''
im = Image.open(f)
w = float(im.width)
h = float(im.height)
if max_scale >= (w / h) >= min_scale or max_scale >= float(h / w) >= min_scale:
return True
else:
return False
def wh_limitByWH(f, limit: dict):
'''
根据纯数字宽高限制
:param f: 图片路径
:param limit: 最大最小宽高限制
:return: bool
'''
im = Image.open(f)
w = float(im.width)
h = float(im.height)
if float(limit['maxW'].get()) >= w >= float(limit['minW'].get()) and float(limit['maxH'].get()) >= h >= float(
limit['minH'].get()):
return True
else:
return False