Commit f693c11c6f6e8bc327b520c31bae46f384903c17

Authored by Anthex
1 parent d1b47d4a

Implement color autoscaling, improvements to imaging process, change output type from GIF to WebP

Showing 4 changed files with 52 additions and 27 deletions   Show diff stats
@@ -5,3 +5,4 @@ __pycache__/ @@ -5,3 +5,4 @@ __pycache__/
5 coverage.xml 5 coverage.xml
6 .coverage 6 .coverage
7 out.gif 7 out.gif
  8 +out.webp
1 from operator import itemgetter as ig 1 from operator import itemgetter as ig
2 -from math import floor, sqrt, ceil, exp 2 +from math import floor, sqrt, ceil
3 from numpy import arange 3 from numpy import arange
4 class RSSVector(): 4 class RSSVector():
5 distances = [] 5 distances = []
@@ -269,7 +269,7 @@ def resolve_barycenter(nC, d): @@ -269,7 +269,7 @@ def resolve_barycenter(nC, d):
269 + 1 / (1+d[3]/d[1]+d[3]/d[2]+d[3]/d[0])*nC[3].location 269 + 1 / (1+d[3]/d[1]+d[3]/d[2]+d[3]/d[0])*nC[3].location
270 270
271 271
272 -def NLateration(data, step=.1, xSize=0.0, ySize=0.0, zSize=0.0): 272 +def NLateration(data, step=.1, xSize=0.0, ySize=0.0, zSize=0.0, md=.0, dmax=10):
273 ''' 273 '''
274 Returns a tuple containing (Computed Location, Total distance) 274 Returns a tuple containing (Computed Location, Total distance)
275 :param data: Array of tuples containing (Location, distance) of known emitters 275 :param data: Array of tuples containing (Location, distance) of known emitters
@@ -281,8 +281,8 @@ def NLateration(data, step=.1, xSize=0.0, ySize=0.0, zSize=0.0): @@ -281,8 +281,8 @@ def NLateration(data, step=.1, xSize=0.0, ySize=0.0, zSize=0.0):
281 ''' 281 '''
282 minLoc = Location() 282 minLoc = Location()
283 minDist = 0.0 283 minDist = 0.0
  284 + maxDist= .0
284 revStep = ceil(1/step) 285 revStep = ceil(1/step)
285 - #gifArray = []  
286 for k in data: 286 for k in data:
287 minDist += abs(k[0].distanceTo(Location()) - k[1]) 287 minDist += abs(k[0].distanceTo(Location()) - k[1])
288 xSize = k[0].x if k[0].x > xSize else xSize 288 xSize = k[0].x if k[0].x > xSize else xSize
@@ -298,6 +298,17 @@ def NLateration(data, step=.1, xSize=0.0, ySize=0.0, zSize=0.0): @@ -298,6 +298,17 @@ def NLateration(data, step=.1, xSize=0.0, ySize=0.0, zSize=0.0):
298 if d < minDist: 298 if d < minDist:
299 minDist = d 299 minDist = d
300 minLoc = Location(round(k,2),round(l,2),round(m,2)) 300 minLoc = Location(round(k,2),round(l,2),round(m,2))
301 - d = (max(1-d/10.0, 0))**2  
302 - imageArray[floor(m*revStep)].append((265-floor(d*360), 255, floor(50+d*200)))  
303 - return (minLoc, minDist, imageArray[0], floor(xSize*revStep), floor(ySize*revStep), imageArray) 301 + if d > maxDist:
  302 + maxDist = d
  303 +
  304 + pd=d
  305 + d = (max(1-d/dmax, 0))
  306 +
  307 + if pd > md:
  308 + imageArray[floor(m*revStep)].append((260-floor(360-d*360), 200, floor(50+d*200)))
  309 + else: #mark the computed location with a while pixel
  310 + imageArray[floor(m*revStep)].append((100, 0, 255))
  311 + #print(minDist,pd)
  312 +
  313 +
  314 + return (minLoc, minDist, imageArray[0], floor(xSize*revStep), floor(ySize*revStep), imageArray, maxDist)
@@ -3,7 +3,7 @@ from visuals import createFrame, exportGif @@ -3,7 +3,7 @@ from visuals import createFrame, exportGif
3 from PIL import Image 3 from PIL import Image
4 4
5 def test_createFrame(): 5 def test_createFrame():
6 - result = createFrame(100,100,1) 6 + result = createFrame(100,100,[(0,0,0)],0)
7 assert isinstance(result, Image.Image) 7 assert isinstance(result, Image.Image)
8 8
9 def test_exportGif(): 9 def test_exportGif():
1 from structure import NLateration, Location 1 from structure import NLateration, Location
2 -from math import floor, sqrt 2 +from math import floor
3 from PIL import Image, ImageDraw 3 from PIL import Image, ImageDraw
4 4
5 -global_step=.1  
6 -dataset = [(Location(.5,.5,.5), 3.0), (Location(4.0,.0,.0), 2.0), (Location(4.0,5.0,5.0), 4.2)]  
7 -NLat_result = NLateration(dataset, step=global_step)  
8 -W,H = NLat_result[3], NLat_result[4] 5 +global_step=.2
  6 +upscale_factor=10
  7 +upscale_method = Image.BILINEAR
  8 +dataset = [(Location(.5,.5,0), 3.0), (Location(5.0,7.0,.0), 2.0), (Location(8.0,.0,10.0), 8), (Location(5.0,3.0,9), 4)]
  9 +colorRange=1.0 # [0.0, 1.0]
  10 +#NLat_result = NLateration(dataset, step=global_step)
  11 +
9 frames = [] 12 frames = []
10 -dummy = [0 for _ in range(len(NLat_result[5][0]))] 13 +max = NLateration(dataset, step=global_step)
11 14
12 -def createFrame(x,y,nbr): 15 +def createFrame(x,y,arr,nbr):
13 img = Image.new("HSV",(y,x)) 16 img = Image.new("HSV",(y,x))
14 - img.putdata(NLat_result[5][nbr]) 17 + img.putdata(arr[nbr])
15 return img 18 return img
16 19
17 20
18 -def exportGif(): 21 +def exportGif(includeReverse=False):
  22 + print("Calculating Array")
  23 + NLat_result = NLateration(dataset, step=global_step, md=max[1], dmax=max[6]*colorRange)
  24 + W,H = NLat_result[3], NLat_result[4]
  25 + print("Generating gif/video")
  26 +
19 for i in range(len(NLat_result[5])): 27 for i in range(len(NLat_result[5])):
20 - newFrame = createFrame(W,H,i)  
21 - newFrame = newFrame.resize((W*10,H*10), Image.BICUBIC) 28 + print(".", end="")
  29 + newFrame = createFrame(W,H,NLat_result[5],i)
  30 + newFrame = newFrame.resize((W*upscale_factor,H*upscale_factor), upscale_method)
22 draw = ImageDraw.Draw(newFrame) 31 draw = ImageDraw.Draw(newFrame)
23 draw.text(text="z="+str(round(i*global_step,2)), xy=(0,0)) 32 draw.text(text="z="+str(round(i*global_step,2)), xy=(0,0))
24 frames.append(newFrame) 33 frames.append(newFrame)
25 34
26 - for i in range(len(NLat_result[5])):  
27 - newFrame = createFrame(W,H,len(NLat_result[5])-i-1)  
28 - newFrame = newFrame.resize((W*10,H*10), Image.ANTIALIAS)  
29 - draw = ImageDraw.Draw(newFrame)  
30 - draw.text(text="z="+str(round(((len(NLat_result[5])-i-1)*global_step),2)), xy=(0,0))  
31 - frames.append(newFrame)  
32 -  
33 - frames[0].save("out.gif", format="GIF", append_images=frames[1:], save_all=True, duration=20, loop=0) 35 + if includeReverse:
  36 + for i in range(len(NLat_result[5])):
  37 + print(".", end="")
  38 + newFrame = createFrame(W,H,NLat_result[5],len(NLat_result[5])-i-1)
  39 + newFrame = newFrame.resize((W*upscale_factor,H*upscale_factor), upscale_method)
  40 + draw = ImageDraw.Draw(newFrame)
  41 + draw.text(text="z="+str(round(((len(NLat_result[5])-i-1)*global_step),2)), xy=(0,0))
  42 + frames.append(newFrame)
  43 +
  44 + print("\r\nsaving gif/video")
  45 + #frames[0].save("out.gif", format="GIF", append_images=frames[1:], save_all=True, duration=20, loop=0)
  46 + frames[0].save("out.webp", format="WebP", append_images=frames[1:], save_all=True, duration=40, lossless=True)
34 print("gif exported") 47 print("gif exported")
35 48
36 -exportGif()  
37 \ No newline at end of file 49 \ No newline at end of file
  50 +exportGif(True)
38 \ No newline at end of file 51 \ No newline at end of file