From f693c11c6f6e8bc327b520c31bae46f384903c17 Mon Sep 17 00:00:00 2001 From: Anthex Date: Sat, 27 Apr 2019 14:14:19 +0200 Subject: [PATCH] Implement color autoscaling, improvements to imaging process, change output type from GIF to WebP --- .gitignore | 1 + structure.py | 23 +++++++++++++++++------ test_visuals.py | 2 +- visuals.py | 53 +++++++++++++++++++++++++++++++++-------------------- 4 files changed, 52 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 021e161..020e9d2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ __pycache__/ coverage.xml .coverage out.gif +out.webp diff --git a/structure.py b/structure.py index ce798a5..da686c7 100644 --- a/structure.py +++ b/structure.py @@ -1,5 +1,5 @@ from operator import itemgetter as ig -from math import floor, sqrt, ceil, exp +from math import floor, sqrt, ceil from numpy import arange class RSSVector(): distances = [] @@ -269,7 +269,7 @@ def resolve_barycenter(nC, d): + 1 / (1+d[3]/d[1]+d[3]/d[2]+d[3]/d[0])*nC[3].location -def NLateration(data, step=.1, xSize=0.0, ySize=0.0, zSize=0.0): +def NLateration(data, step=.1, xSize=0.0, ySize=0.0, zSize=0.0, md=.0, dmax=10): ''' Returns a tuple containing (Computed Location, Total distance) :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): ''' minLoc = Location() minDist = 0.0 + maxDist= .0 revStep = ceil(1/step) - #gifArray = [] for k in data: minDist += abs(k[0].distanceTo(Location()) - k[1]) 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): if d < minDist: minDist = d minLoc = Location(round(k,2),round(l,2),round(m,2)) - d = (max(1-d/10.0, 0))**2 - imageArray[floor(m*revStep)].append((265-floor(d*360), 255, floor(50+d*200))) - return (minLoc, minDist, imageArray[0], floor(xSize*revStep), floor(ySize*revStep), imageArray) + if d > maxDist: + maxDist = d + + pd=d + d = (max(1-d/dmax, 0)) + + if pd > md: + imageArray[floor(m*revStep)].append((260-floor(360-d*360), 200, floor(50+d*200))) + else: #mark the computed location with a while pixel + imageArray[floor(m*revStep)].append((100, 0, 255)) + #print(minDist,pd) + + + return (minLoc, minDist, imageArray[0], floor(xSize*revStep), floor(ySize*revStep), imageArray, maxDist) diff --git a/test_visuals.py b/test_visuals.py index bd369ab..cc42ead 100644 --- a/test_visuals.py +++ b/test_visuals.py @@ -3,7 +3,7 @@ from visuals import createFrame, exportGif from PIL import Image def test_createFrame(): - result = createFrame(100,100,1) + result = createFrame(100,100,[(0,0,0)],0) assert isinstance(result, Image.Image) def test_exportGif(): diff --git a/visuals.py b/visuals.py index 98e6524..088ef88 100644 --- a/visuals.py +++ b/visuals.py @@ -1,36 +1,49 @@ from structure import NLateration, Location -from math import floor, sqrt +from math import floor from PIL import Image, ImageDraw -global_step=.1 -dataset = [(Location(.5,.5,.5), 3.0), (Location(4.0,.0,.0), 2.0), (Location(4.0,5.0,5.0), 4.2)] -NLat_result = NLateration(dataset, step=global_step) -W,H = NLat_result[3], NLat_result[4] +global_step=.2 +upscale_factor=10 +upscale_method = Image.BILINEAR +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)] +colorRange=1.0 # [0.0, 1.0] +#NLat_result = NLateration(dataset, step=global_step) + frames = [] -dummy = [0 for _ in range(len(NLat_result[5][0]))] +max = NLateration(dataset, step=global_step) -def createFrame(x,y,nbr): +def createFrame(x,y,arr,nbr): img = Image.new("HSV",(y,x)) - img.putdata(NLat_result[5][nbr]) + img.putdata(arr[nbr]) return img -def exportGif(): +def exportGif(includeReverse=False): + print("Calculating Array") + NLat_result = NLateration(dataset, step=global_step, md=max[1], dmax=max[6]*colorRange) + W,H = NLat_result[3], NLat_result[4] + print("Generating gif/video") + for i in range(len(NLat_result[5])): - newFrame = createFrame(W,H,i) - newFrame = newFrame.resize((W*10,H*10), Image.BICUBIC) + print(".", end="") + newFrame = createFrame(W,H,NLat_result[5],i) + newFrame = newFrame.resize((W*upscale_factor,H*upscale_factor), upscale_method) draw = ImageDraw.Draw(newFrame) draw.text(text="z="+str(round(i*global_step,2)), xy=(0,0)) frames.append(newFrame) - for i in range(len(NLat_result[5])): - newFrame = createFrame(W,H,len(NLat_result[5])-i-1) - newFrame = newFrame.resize((W*10,H*10), Image.ANTIALIAS) - draw = ImageDraw.Draw(newFrame) - draw.text(text="z="+str(round(((len(NLat_result[5])-i-1)*global_step),2)), xy=(0,0)) - frames.append(newFrame) - - frames[0].save("out.gif", format="GIF", append_images=frames[1:], save_all=True, duration=20, loop=0) + if includeReverse: + for i in range(len(NLat_result[5])): + print(".", end="") + newFrame = createFrame(W,H,NLat_result[5],len(NLat_result[5])-i-1) + newFrame = newFrame.resize((W*upscale_factor,H*upscale_factor), upscale_method) + draw = ImageDraw.Draw(newFrame) + draw.text(text="z="+str(round(((len(NLat_result[5])-i-1)*global_step),2)), xy=(0,0)) + frames.append(newFrame) + + print("\r\nsaving gif/video") + #frames[0].save("out.gif", format="GIF", append_images=frames[1:], save_all=True, duration=20, loop=0) + frames[0].save("out.webp", format="WebP", append_images=frames[1:], save_all=True, duration=40, lossless=True) print("gif exported") -exportGif() \ No newline at end of file +exportGif(True) \ No newline at end of file -- libgit2 0.21.4