From 921ffc77c825b01e3225afc6ba019b70dd42cf8e Mon Sep 17 00:00:00 2001 From: Eelco van Vliet Date: Thu, 12 Feb 2015 16:02:04 +0100 Subject: [PATCH 1/4] made vapory compatible with python under windows --- .gitignore | 5 +++++ examples/sphere.py | 7 ++++++- vapory/config.py | 2 +- vapory/io.py | 23 +++++++++++++++++------ 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 5a2209e..912417d 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,8 @@ nosetests.xml # Pipy codes .pypirc + +# povray output +*.pov +*.ppm +*.png diff --git a/examples/sphere.py b/examples/sphere.py index 19458e8..7b874da 100644 --- a/examples/sphere.py +++ b/examples/sphere.py @@ -11,4 +11,9 @@ Sphere( [0, 1, 2] , 2, Texture( Pigment( 'color', [1,0,1])))] ) -scene.render("sphere.png", width = 600, height=400, remove_temp=0) \ No newline at end of file +print(("start rendering")) + +scene.render("sphere1.png", width = 600, height=400, remove_temp=0) +print(("done.")) + +scene.render("sphere2.png", width =1200, height=800, remove_temp=0) diff --git a/vapory/config.py b/vapory/config.py index 4a45def..c841430 100644 --- a/vapory/config.py +++ b/vapory/config.py @@ -1,6 +1,6 @@ import os -POVRAY_BINARY = ("povray.exe" if os.name=='nt' else "povray") +POVRAY_BINARY = ("pvengine64.exe" if os.name=='nt' else "povray") GLOBAL_SCENE_SETTINGS = { "charset" : "ascii", diff --git a/vapory/io.py b/vapory/io.py index 8b62491..c499ae9 100644 --- a/vapory/io.py +++ b/vapory/io.py @@ -89,12 +89,19 @@ def render_povstring(string, outfile=None, height=None, width=None, format_type = "P" if return_np_array else "N" if return_np_array: - outfile='-' + if os.name=='nt': + outfile='__temp__.ppm' + else : + outfile='-' if display_in_ipython: outfile = '__temp_ipython__.png' - cmd = [POVRAY_BINARY, pov_file] + cmd = [POVRAY_BINARY] + if os.name=='nt': + cmd.append('/EXIT') + cmd.append('/RENDER') + cmd.append(pov_file) if height is not None: cmd.append('+H%d'%height) if width is not None: cmd.append('+W%d'%width) if quality is not None: cmd.append('+Q%d'%quality) @@ -105,12 +112,13 @@ def render_povstring(string, outfile=None, height=None, width=None, cmd.append('+d') cmd.append("Output_File_Type=%s"%format_type) cmd.append("+O%s"%outfile) + process = subprocess.Popen(cmd, stderr=subprocess.PIPE, - stdin=subprocess.PIPE, - stdout=subprocess.PIPE) + stdin=subprocess.PIPE, + stdout=subprocess.PIPE) out, err = process.communicate(string.encode('ascii')) - + if remove_temp: os.remove(pov_file) @@ -119,7 +127,10 @@ def render_povstring(string, outfile=None, height=None, width=None, raise IOError("POVRay rendering failed with the following error: "+err.decode('ascii')) if return_np_array: - return ppm_to_numpy(buffer=out) + if os.name=='nt': + return ppm_to_numpy(filename=outfile) + else: + return ppm_to_numpy(buffer=out) if display_in_ipython: if not ipython_found: From b25a3e2f60730b8f1a0fb187dc36fcdda4b68750 Mon Sep 17 00:00:00 2001 From: Eelco van Vliet Date: Thu, 12 Feb 2015 16:44:29 +0100 Subject: [PATCH 2/4] updated example --- examples/sphere.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/sphere.py b/examples/sphere.py index 7b874da..83196b4 100644 --- a/examples/sphere.py +++ b/examples/sphere.py @@ -1,6 +1,8 @@ """ Just a purple sphere """ from vapory import * +import matplotlib.pyplot as plt + scene = Scene( @@ -11,9 +13,7 @@ Sphere( [0, 1, 2] , 2, Texture( Pigment( 'color', [1,0,1])))] ) -print(("start rendering")) -scene.render("sphere1.png", width = 600, height=400, remove_temp=0) -print(("done.")) +nparray=scene.render(width = 600, height=400, remove_temp=1) -scene.render("sphere2.png", width =1200, height=800, remove_temp=0) +plt.imshow(nparray) From fe88a6ee04e7d09a6f86532207ea284ac73b69a1 Mon Sep 17 00:00:00 2001 From: Eelco van Vliet Date: Fri, 13 Feb 2015 16:48:22 +0100 Subject: [PATCH 3/4] some small changes --- vapory/io.py | 6 ++++-- vapory/vapory.py | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/vapory/io.py b/vapory/io.py index c499ae9..3081d93 100644 --- a/vapory/io.py +++ b/vapory/io.py @@ -54,7 +54,7 @@ def ppm_to_numpy(filename=None, buffer=None, byteorder='>'): def render_povstring(string, outfile=None, height=None, width=None, quality=None, antialiasing=None, remove_temp=True, - show_window=False, tempfile=None): + show_window=False, tempfile=None,exit_when_done=True): """ Renders the provided scene description with POV-Ray. @@ -99,7 +99,8 @@ def render_povstring(string, outfile=None, height=None, width=None, cmd = [POVRAY_BINARY] if os.name=='nt': - cmd.append('/EXIT') + if exit_when_done: + cmd.append('/EXIT') cmd.append('/RENDER') cmd.append(pov_file) if height is not None: cmd.append('+H%d'%height) @@ -118,6 +119,7 @@ def render_povstring(string, outfile=None, height=None, width=None, stdout=subprocess.PIPE) out, err = process.communicate(string.encode('ascii')) + if remove_temp: os.remove(pov_file) diff --git a/vapory/vapory.py b/vapory/vapory.py index 203309b..c3f5479 100644 --- a/vapory/vapory.py +++ b/vapory/vapory.py @@ -54,7 +54,8 @@ def add_objects(self, objs): def render(self, outfile=None, height=None, width=None, quality=None, antialiasing=None, remove_temp=True, - auto_camera_angle=True, show_window=False, tempfile=None): + auto_camera_angle=True, show_window=False, tempfile=None, + exit_when_done=True): """ Renders the scene to a PNG, a numpy array, or the IPython Notebook. @@ -79,7 +80,8 @@ def render(self, outfile=None, height=None, width=None, self.camera = self.camera.add_args(['right', [1.0*width/height, 0,0]]) return render_povstring(str(self), outfile, height, width, - quality, antialiasing, remove_temp, show_window, tempfile) + quality, antialiasing, remove_temp, show_window, tempfile, + exit_when_done) class POVRayElement: From fe0ac688ac3dc22f44ccf900fc06874b2015223b Mon Sep 17 00:00:00 2001 From: Eelco van Vliet Date: Sun, 15 Feb 2015 18:15:39 +0100 Subject: [PATCH 4/4] bug fix for 16 depth images --- .gitignore | 2 ++ vapory/io.py | 23 ++++++++++++++--------- vapory/vapory.py | 4 ++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 912417d..f77deb9 100644 --- a/.gitignore +++ b/.gitignore @@ -47,3 +47,5 @@ nosetests.xml *.pov *.ppm *.png + +Thumbs.db diff --git a/vapory/io.py b/vapory/io.py index 3081d93..2f6716e 100644 --- a/vapory/io.py +++ b/vapory/io.py @@ -19,7 +19,7 @@ except: ipython_found=False -def ppm_to_numpy(filename=None, buffer=None, byteorder='>'): +def ppm_to_numpy(filename=None, buffer=None, byteorder='>',bitspercolor=None): """Return image data from a raw PGM/PPM file as numpy array. Format specification: http://netpbm.sourceforge.net/doc/pgm.html @@ -43,7 +43,11 @@ def ppm_to_numpy(filename=None, buffer=None, byteorder='>'): cols_per_pixels = 1 if header.startswith(b"P5") else 3 - dtype = 'uint8' if int(maxval) < 256 else byteorder+'uint16' + + dtype = 'uint8' + if int(maxval) >= 256 or bitspercolor==16: + dtype=byteorder+'u2' + arr = numpy.frombuffer(buffer, dtype=dtype, count=int(width)*int(height)*3, offset=len(header)) @@ -52,7 +56,7 @@ def ppm_to_numpy(filename=None, buffer=None, byteorder='>'): -def render_povstring(string, outfile=None, height=None, width=None, +def render_povstring(string, outfile=None, height=None, width=None,bitspercolor=None, quality=None, antialiasing=None, remove_temp=True, show_window=False, tempfile=None,exit_when_done=True): @@ -103,14 +107,15 @@ def render_povstring(string, outfile=None, height=None, width=None, cmd.append('/EXIT') cmd.append('/RENDER') cmd.append(pov_file) - if height is not None: cmd.append('+H%d'%height) - if width is not None: cmd.append('+W%d'%width) - if quality is not None: cmd.append('+Q%d'%quality) - if antialiasing is not None: cmd.append('+A%f'%antialiasing) + if height is not None: cmd.append('+H{}'.format(height)) + if width is not None: cmd.append('+W{}'.format(width)) + if quality is not None: cmd.append('+Q{}'.format(quality)) + if antialiasing is not None: cmd.append('+A{}'.format(antialiasing)) if not show_window: cmd.append('-d') else: cmd.append('+d') + if bitspercolor is not None: cmd.append("Bits_per_color={}".format(bitspercolor)) cmd.append("Output_File_Type=%s"%format_type) cmd.append("+O%s"%outfile) @@ -130,9 +135,9 @@ def render_povstring(string, outfile=None, height=None, width=None, if return_np_array: if os.name=='nt': - return ppm_to_numpy(filename=outfile) + return ppm_to_numpy(filename=outfile,bitspercolor=bitspercolor) else: - return ppm_to_numpy(buffer=out) + return ppm_to_numpy(buffer=out,bitspercolor=bitspercolor) if display_in_ipython: if not ipython_found: diff --git a/vapory/vapory.py b/vapory/vapory.py index c3f5479..33616ca 100644 --- a/vapory/vapory.py +++ b/vapory/vapory.py @@ -52,7 +52,7 @@ def add_objects(self, objs): new.objects += objs return new - def render(self, outfile=None, height=None, width=None, + def render(self, outfile=None, height=None, width=None,bitspercolor=None, quality=None, antialiasing=None, remove_temp=True, auto_camera_angle=True, show_window=False, tempfile=None, exit_when_done=True): @@ -79,7 +79,7 @@ def render(self, outfile=None, height=None, width=None, if auto_camera_angle and width is not None: self.camera = self.camera.add_args(['right', [1.0*width/height, 0,0]]) - return render_povstring(str(self), outfile, height, width, + return render_povstring(str(self), outfile, height, width,bitspercolor, quality, antialiasing, remove_temp, show_window, tempfile, exit_when_done)