-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImagem.py
More file actions
148 lines (124 loc) · 3.34 KB
/
Imagem.py
File metadata and controls
148 lines (124 loc) · 3.34 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
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
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-
'''
Autor: Leonardo de Abreu Schmidt
Data inicio: 10/02/2017
Versão: 1.0
'''
import PIL
from PIL import Image
import PIL.ImageOps
class Imagem:
def __init__(self):
self.img = None
self.pixels2 = []
self.matrix = []
self.size = (0,0)
def new(self,w,h):
self.img = Image.new('RGB', (w,h), "white") # create a new black image
self.size = (w,h)
def resize(self,basewidth,hsize):
self.img = self.img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
self.size = (basewidth,hsize)
def save(self,name):
self.img.save(name)
def open(self,name):
self.img = Image.open(name)
self.size = self.img.size
def show(self):
self.img.show()
def getSize(self):
return self.img.size
def load(self):
self.pixels2 = self.img.load()
def greyScale(self):
self.img = self.img.convert('L')
def vectorize(self):
pixels = []
for w in range(self.img.size[0]):
for h in range(self.img.size[1]):
g = self.img.getpixel((w,h))
pixels.append(g)
return pixels
def inverse(self):
self.img = PIL.ImageOps.invert(self.img)
def toMatrix(self):
px = []
for w in range(self.img.size[0]):
px = []
for h in range(self.img.size[1]):
g = self.img.getpixel((w,h))
px.append(g)
self.matrix.append(px)
return self.matrix
def setMatrix(self,matrix):
for i in range(self.img.size[0]):
for j in range(self.img.size[1]):
self.pixels2[i,j] = matrix[i][j]
def vectorToMatrix(self,vector):
for i in range(self.img.size[0]):
for j in range(self.img.size[1]):
self.pixels2[i,j] = (vector[i * self.img.size[1] + j], vector[i * self.img.size[1] + j], vector[i * self.img.size[1] + j])
def clear(self):
self.load()
for i in range(self.img.size[1]):
for j in range(self.img.size[0]):
if(self.pixels2[j,i] > 150):
self.pixels2[j,i] = 255
else:
self.pixels2[j,i] = 0
def cropHeight(self,val):
b = False
pos = 0
i = 0
for h in range(self.img.size[1]):
for w in range(self.img.size[0]):
if(self.img.getpixel((w,h)) > val and b == False):
b = True
pos = i
i+=1
self.img = self.img.crop((0,pos,self.img.size[0],self.img.size[1]))
b = False
i = self.size[1] - pos
pos = 0
for h in reversed(range(self.img.size[1])):
for w in range(self.img.size[0]):
if(self.img.getpixel((w,h)) > val and b == False):
b = True
pos = i
i-=1
self.img = self.img.crop((0,0,self.img.size[0],pos))
def cropWidth(self,val):
b = False
pos = 0
i = 0
for w in range(self.img.size[0]):
for h in range(self.img.size[1]):
if(self.img.getpixel((w,h)) > val and b == False):
b = True
pos = i
i+=1
self.img = self.img.crop((pos,0,self.img.size[0],self.img.size[1]))
b = False
i = self.size[0] - pos
pos = 0
for w in reversed(range(self.img.size[0])):
for h in range(self.img.size[1]):
if(self.img.getpixel((w,h)) > val and b == False):
b = True
pos = i
i-=1
self.img = self.img.crop((0,0,pos,self.img.size[1]))
def cropMargin(self):
val = 20
self.cropWidth(val)
self.cropHeight(val)
'''img = cv2.imread('000041.jpg')
#blur = cv2.bilateralFilter(img,1,75,50)
#blur = cv2.GaussianBlur(img,(3,3),0)
#blur = cv2.blur(img,(9,9))
#kernel = np.ones((5,5),np.float32)/25
#blur = cv2.filter2D(img,-1,kernel)
blur = cv2.medianBlur(img,5)
cv2.imshow('',blur)
cv2.waitKey(0)
cv2.destroyAllWindows()'''