-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
141 lines (121 loc) · 3.43 KB
/
utils.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
operators = ['+', '-', '/', '*', '^', '(', ')']
def isOperand(c):
if c not in operators:
return True
return False
def hasLessOrEqualPriority(a, b):
if a == '(':
return False
if a == '^':
if b in ['(', '^']:
return True
if a in ['/', '*']:
if b in ['(', '/', '*']:
return True
if a in ['+', '-']:
return True
if b in ['emp', ')']:
return True
return False
def peek(stack):
if len(stack) == 0:
return 'emp'
return stack[len(stack)-1]
def evaluate(a, b, operator, imflag=False):
if operator == '+':
return a+b
elif operator == '-':
return a-b
elif operator == '/':
if imflag:
cv2.imwrite('./temp.jpg', a/b)
im = cv2.imread('./temp.jpg')
os.remove('./temp.jpg')
return im
else:
return a/b
elif operator == '*':
return a*b
def toPostfix(infix):
stack = []
infix = Wrap(infix)
postfix = ''
operator = ''
opflag = False
for c in infix:
if isOperand(c):
if opflag == False:
postfix += '@'
postfix += c
opflag = True
else:
if opflag == True:
postfix += '@'
opflag = False
if c == '(' or len(stack) == 0:
stack.append(c)
elif c == ')':
operator = stack.pop()
while not operator == '(':
postfix += operator
operator = stack.pop()
else:
while stack and not hasLessOrEqualPriority(c, peek(stack)):
postfix += stack.pop()
stack.append(c)
while (not (len(stack) == 0)):
postfix += stack.pop()
return postfix
def Wrap(exp):
if exp[0] == '(' and exp[len(exp)-1] == ')':
return exp
else:
return '(' + exp + ')'
def transform_Image(Image, transform, pixelrange=[]):
transform = toPostfix(transform)
stack = []
opflag = False
dig_holder = ''
for c in transform:
print(stack)
if isOperand(c):
if c == '@':
opflag = not opflag
if opflag == False:
stack.append(dig_holder)
dig_holder = ''
if opflag == True:
if c != '@':
dig_holder += c
else:
op1 = stack.pop()
op2 = stack.pop()
if (op1 == 'X' or op1 == 'x') and (op2 == 'x' or op2 == 'X'):
op1 = Image
op2 = Image
Image = evaluate(op2, op1, c, True)
stack.append('x')
elif op1 == 'x' or op1 == 'X':
op1 = Image
op2 = int(op2)
Image = evaluate(op2, op1, c, True)
stack.append('x')
elif op2 == 'x' or op2 == 'X':
op2 = Image
op1 = int(op1)
Image = evaluate(op2, op1, c, True)
stack.append('x')
else:
op1 = int(op1)
op2 = int(op2)
stack.append(evaluate(op2, op1, c))
return Image
# transform='x/20'
# Image=cv2.imread('./Test.jpg')
# Image=transform_Image(Image,transform)
# cv2.imshow('lol',Image)
# cv2.waitKey(0)