changeset 1526:71b9467cfaa2

add combineImages for use by solver algorithms Ignore-this: 2add0d7c039abd9e442d11308425d5d9
author drewp@bigasterisk.com
date Sun, 30 Apr 2017 17:23:55 +0000
parents 1849713b0d73
children 951fc2051045
files light9/paint/solve.py light9/paint/solve_test.py
diffstat 2 files changed, 34 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/light9/paint/solve.py	Sun Apr 30 06:36:28 2017 +0000
+++ b/light9/paint/solve.py	Sun Apr 30 17:23:55 2017 +0000
@@ -45,10 +45,16 @@
         ret = float(ret)
     return ret
 
+def loadNumpy(path, thumb=(100, 100)):
+    img = Image.open(path)
+    img.thumbnail(thumb)
+    return numpyFromPil(img)
+    
 class Solver(object):
     def __init__(self, graph):
         self.graph = graph
         self.samples = {} # uri: Image array
+        self.fromPath = {} # basename: image array
         self.blurredSamples = {}
         self.sampleSettings = {} # (uri, path): { dev: { attr: val } }
         
@@ -57,10 +63,9 @@
 
         with self.graph.currentState() as g:
             for samp in g.subjects(RDF.type, L9['LightSample']):
-                path = 'show/dance2017/cam/test/%s' % g.value(samp, L9['path'])
-                img = Image.open(path)
-                img.thumbnail((100, 100))
-                self.samples[samp] = numpyFromPil(img)
+                base = g.value(samp, L9['path']).toPython()
+                path = 'show/dance2017/cam/test/%s' % base
+                self.samples[samp] = self.fromPath[base] = loadNumpy(path)
                 self.blurredSamples[samp] = self._blur(self.samples[samp])
 
                 for s in g.objects(samp, L9['setting']):
@@ -133,6 +138,15 @@
                            
         return out
 
+    def combineImages(self, layers):
+        """make a result image from our self.samples images"""
+        out = (self.fromPath.itervalues().next() * 0).astype(numpy.uint16)
+        for layer in layers:
+            colorScaled = self.fromPath[layer['path']] * layer['color']
+            out += colorScaled.astype(numpy.uint16)
+        numpy.clip(out, 0, 255, out)
+        return out.astype(numpy.uint8)
+        
     def simulationLayers(self, settings):
         """
         how should a simulation preview approximate the light settings
--- a/light9/paint/solve_test.py	Sun Apr 30 06:36:28 2017 +0000
+++ b/light9/paint/solve_test.py	Sun Apr 30 17:23:55 2017 +0000
@@ -1,4 +1,5 @@
 import unittest
+import numpy.testing
 import solve
 from light9.namespaces import RDF, L9, DEV
 from light9.rdfdb.localsyncedgraph import LocalSyncedGraph
@@ -60,3 +61,18 @@
             {'path': 'bg2-d.jpg', 'color': (1, 1, 1)},
             {'path': 'bg2-f.jpg', 'color': (1, 1, 1)},
                       ], layers)
+
+class TestCombineImages(unittest.TestCase):
+    def setUp(self):
+        graph = LocalSyncedGraph(files=['show/dance2017/cam/test/bg.n3'])
+        self.solver = solve.Solver(graph)
+        self.solver.loadSamples()
+    def test(self):
+        out = self.solver.combineImages(layers=[
+            {'path': 'bg2-d.jpg', 'color': (.2, .2, .3)},
+            {'path': 'bg2-a.jpg', 'color': (.888, 0, .3)},
+        ])
+        solve.saveNumpy('/tmp/t.png', out)
+        golden = solve.loadNumpy('show/dance2017/cam/test/layers_out1.png')
+        numpy.testing.assert_array_equal(golden, out)
+