-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageCls.py
More file actions
351 lines (316 loc) · 10.8 KB
/
Copy pathimageCls.py
File metadata and controls
351 lines (316 loc) · 10.8 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
import os
from sys import argv
import numpy
from PIL import Image, ImageOps, ImageDraw
from pillow_heif import register_heif_opener
import colorsys
from mediaCls import MediaFile, MediaFolder
import mediaCls as media
from kmeans import Kmeans, KmeansTablesNb
import loggerFct as log
imgExtensions =[ 'jpg', 'bmp', 'gif', 'png', 'mp4', 'avi', 'heic', 'heif' ]
numpy.seterr (all='warn')
rgb_to_hsv = numpy.vectorize (colorsys.rgb_to_hsv)
hsv_to_rgb = numpy.vectorize (colorsys.hsv_to_rgb)
register_heif_opener()
def creerGif (nomGif, gifFolder, imageNameList):
if gifFolder[-1] != os.sep: gifFolder = gifFolder + os.sep
nomGif = gifFolder + nomGif + '.gif'
pilImageList =[]
for imageName in imageNameList:
pilImageList.append (Image.open (gifFolder + imageName))
pilImageList[-1] = pilImageList[-1].convert ('RGB')
imageRef = pilImageList.pop(0)
imageRef.save (nomGif, save_all=True, append_images=pilImageList, duration=500, loop=0)
def blurrOne (table, y,x):
# calculer la valeur moyenne
neighbours =[]
if y>0: neighbours.append (table[y-1][x])
if y< (len (table) -1): neighbours.append (table[y+1][x])
if x>0: neighbours.append (table[y][x-1])
if x< (len (table[0]) -1): neighbours.append (table[y][x+1])
n=0
lenbours = len (neighbours)
while n< lenbours and neighbours.count (neighbours[n]) <2: n+=1
if n== lenbours: return neighbours[1]
else: return neighbours[n]
def blurr (table):
rangeX = range (len (table[0]))
rangeY = range (len (table))
for y in rangeY:
for x in rangeX: table[y][x] = blurrOne (table, y,x)
return table
# ------------------------ o ------------------------
class ImageFile (MediaFile):
def __init__ (self, image=None):
MediaFile.__init__ (self)
self.image = None
self.array =[]
self.width =0
self.height =0
if image:
self.path = image
self.fromPath()
def test (self):
hue, saturation, brightness = self.toHsv()
"""
hue = 80
saturation = 60
brightness = 50
"""
hue = blurr (hue)
saturation = 60
brightness = 50
hueKmeans = KmeansTablesCanal (hue)
hueT = hue.T
for group in hueKmeans.groups:
for color in group[1:]:
colorArea = (hueT == color)
hue[hueT.T] = group[0]
print (hue)
self.fromHsv (hue, saturation, brightness)
def heifToPng (self, nameSpace, addHour=False):
# l'image fera 1000 pixel de haut
dateCreation = media.getDateCreation (self.title +'.'+ self.extension, nameSpace, addHour)
self.open()
self.resizeHeight1000()
self.correctContrast()
nameCreation = media.createDatedName (self.pathRoot, 'png', dateCreation)
if nameCreation:
self.image.save (nameCreation)
self.toPath()
os.remove (self.path)
def renameDate (self, nameSpace, addHour=False):
""" renommer un fichier en prenant en compte la date
la fonction utilise les métadonnées de window
"""
nameCreation = self.renameDateEtape1 (nameSpace, addHour);
if nameCreation:
self.resizeHeight1000()
self.correctContrast()
self.draw()
os.rename (self.path, nameCreation)
def resizeHeight1000 (self):
heightNew =1000
if self.image.size[1] > heightNew:
percentScale = float (heightNew / float (self.image.size[1]))
widthNew = int (self.image.size[0] * percentScale)
self.image = self.image.resize ((widthNew, heightNew), Image.Resampling.LANCZOS)
def eraseColors (self, referImg):
# éffacer certaines couleurs d'une image à partir d'une image de référence qui les contient
colors = referImg.getColors()
self.array = numpy.array (self.image)
red, green, blue = self.array.T
"""
self.array is a height x width x (r,g,b) numpy array
red is a height x width x (0.0 ... 100) numpy array
"""
# éffacer les couleurs
for r,g,b in colors:
colorArea = (red == r) & (green == g) & (blue == b)
self.array[colorArea.T] = (255, 255, 255)
# dessiner la nouvelle image
self.image = Image.fromarray (self.array)
def reverseColors (self):
self.image = ImageOps.invert (self.image)
def reverseLumins (self):
hue, saturation, brightness = self.toHsv()
brightness = 100.0 - brightness
self.fromHsv (hue, saturation, brightness)
def reverseImage (self):
self.reverseLumins()
self.image = ImageOps.invert (self.image)
def to16bits (self):
self.image = self.image.convert ('P', colors=16)
# self.array = numpy.array (self.image)
def tobw (self):
self.image = ImageOps.grayscale (self.image)
def swapColors (self, colOldStr, colNewStr):
# colXStr = "30 67 23"
colOld = colOldStr.split (" ")
colOld[0] = int (colOld[0])
colOld[1] = int (colOld[1])
colOld[2] = int (colOld[2])
colNew = colNewStr.split (" ")
colNew[0] = int (colNew[0])
colNew[1] = int (colNew[1])
colNew[2] = int (colNew[2])
self.array = numpy.array (self.image)
red, green, blue = self.array.T
colorArea = (red == colOld[0]) & (green == colOld[1]) & (blue == colOld[2])
self.array[colorArea.T] = colNew
self.image = Image.fromarray (self.array)
def correctContrast (self):
# calculer le contraste des couleurs. une valeur pas canal rvb
colors = self.getColors()
colMin =[ colors[0][0], colors[0][1], colors[0][2] ]
colMax =[ colors[0][0], colors[0][1], colors[0][2] ]
for color in colors:
if color[0] < colMin[0]: colMin[0] = color[0]
elif color[0] > colMax[0]: colMax[0] = color[0]
if color[1] < colMin[1]: colMin[1] = color[1]
elif color[1] > colMax[1]: colMax[1] = color[1]
if color[2] < colMin[2]: colMin[2] = color[2]
elif color[2] > colMax[2]: colMax[2] = color[2]
colSpan =[ colMax[0] - colMin[0], colMax[1] - colMin[1], colMax[2] - colMin[2] ]
self.array = numpy.array (self.image).astype ('float')
rangeY = range (len (self.array))
rangeX = range (len (self.array[0]))
if colSpan[0] <200:
factorA = 255.0 / colSpan[0]
factorB = colMin[0] * factorA
for y in rangeY:
for x in rangeX: self.array[y][x][0] = factorA * self.array[y][x][0] - factorB
if colSpan[1] <200:
factorA = 255.0 / colSpan[1]
factorB = colMin[1] * factorA
for y in rangeY:
for x in rangeX: self.array[y][x][1] = factorA * self.array[y][x][1] - factorB
if colSpan[2] <200:
factorA = 255.0 / colSpan[2]
factorB = colMin[2] * factorA
for y in rangeY:
for x in rangeX: self.array[y][x][2] = factorA * self.array[y][x][2] - factorB
self.array = self.array.astype ('uint8')
self.image = Image.fromarray (self.array)
def getColors (self):
colorsOriginal = self.image.getcolors (self.image.size[0] * self.image.size[1])
colorsSet = set (colorsOriginal)
colors =[]
for nb, color in colorsSet: colors.append (color)
colors.sort()
return colors
def toHsv (self):
"""
hue is a height x width x (0.0 ... 1) numpy array
saturation is a height x width x (0.0 ... 1) numpy array
brightness is a height x width x (0.0 ... 256) numpy array
je les normalise à 100
"""
self.array = numpy.array (self.image).astype ('float')
red, green, blue = self.array.T
hue, saturation, brightness = rgb_to_hsv (red, green, blue)
hue *= 100.0
saturation *= 100.0
brightness *= 100.0
brightness /= 255.0
return hue, saturation, brightness
def fromHsv (self, hue, saturation, brightness):
hue /= 100.0
saturation /= 100.0
brightness *= 2.55
red, green, blue = hsv_to_rgb (hue, saturation, brightness)
red = red.T
green = green.T
blue = blue.T
self.array = numpy.dstack ((red, green, blue))
self.array = self.array.astype ('uint8')
self.image = Image.fromarray (self.array)
def fromArray (self):
self.image = Image.fromarray (self.array)
def toArray (self):
self.array = numpy.array (self.image)
def fromMedia (self, mediaFile):
self.path = mediaFile.path
self.title = mediaFile.title
self.extension = mediaFile.extension
self.pathRoot = mediaFile.pathRoot
self.open()
def setImage (self, imageNew):
self.image = imageNew.convert ('RGB')
self.width = self.image.size[0]
self.height = self.image.size[1]
def open (self):
self.toPath()
if not os.path.exists (self.path): return
self.setImage (Image.open (self.path))
def draw (self):
isDrawable = MediaFile.draw (self)
if isDrawable:
if self.extension == 'heic' or self.extension == 'heif':
print ('attention, vous avez faillit enregistrer une image heic')
else: self.image.save (self.path)
class ImageFolder (MediaFolder):
def test (self):
self.path = 'b/cgi'
self.get ('deborah')
self.insta()
def insta (self, drawBgfonc='average'):
self.open()
ratio = self.list[0].width / self.list[0].height
ratioMin = ratio
ratioMax = ratio
for image in self.list:
ratio = image.width / image.height
if ratio < ratioMin: ratioMin = ratio
elif ratio > ratioMax: ratioMax = ratio
imageRange = range (len (self.list))
if ratioMax <1.0:
for i in imageRange:
regrowMade = self.list[i].insta (drawBgfonc, ratioMax)
if regrowMade:
self.list[i].title = self.list[i].title +' insta'
self.list[i].draw()
elif ratioMin >1.0:
for i in imageRange:
regrowMade = self.list[i].insta (drawBgfonc, ratioMin)
if regrowMade:
self.list[i].title = self.list[i].title +' insta'
self.list[i].draw()
else:
for i in imageRange:
regrowMade = self.list[i].insta (drawBgfonc, 1.0)
if regrowMade:
self.list[i].title = self.list[i].title +' insta'
self.list[i].draw()
def heicToPng (self, addHour=False):
nameSpace = media.getNameSpace (self.path[:-1])
self.get ('heic')
for image in self.list:
if os.sep not in image.path:
image.path = self.path + image.path
image.heifToPng (nameSpace, addHour)
def heifToPng (self, addHour=False):
nameSpace = media.getNameSpace (self.path[:-1])
self.get ('heif')
for image in self.list:
if os.sep not in image.path:
image.path = self.path + image.path
image.heifToPng (nameSpace, addHour)
def open (self):
imageRange = range (len (self.list))
for i in imageRange: self.list[i].open()
def get (self, detail=""):
if detail == 'heic':
for dirpath, SousListDossiers, subList in os.walk (self.path):
if not subList: continue
range_tag = range (len (subList) -1, -1, -1)
for i in range_tag:
if subList[i][-5:] != '.heic': trash = subList.pop(i)
if subList:
for image in subList:
fileTmp = ImageFile (os.path.join (dirpath, image))
self.list.append (fileTmp)
elif detail == 'heif':
for dirpath, SousListDossiers, subList in os.walk (self.path):
if not subList: continue
range_tag = range (len (subList) -1, -1, -1)
for i in range_tag:
if subList[i][-5:] != '.heif': trash = subList.pop(i)
if subList:
for image in subList:
fileTmp = ImageFile (os.path.join (dirpath, image))
self.list.append (fileTmp)
else:
MediaFolder.get (self, detail)
imageRange = range (len (self.list))
for i in imageRange:
imageNew = ImageFile (self.list[i])
self.list[i] = imageNew
self.list.sort()
""" sources
https://www.geeksforgeeks.org/python-pil-image-point-method/
https://pillow.readthedocs.io/en/stable/reference/ImageOps.html
"""