diff --git a/.gitignore b/.gitignore index 5a2209e..f77deb9 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,10 @@ nosetests.xml # Pipy codes .pypirc + +# povray output +*.pov +*.ppm +*.png + +Thumbs.db diff --git a/examples/sphere.py b/examples/sphere.py index 19458e8..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,4 +13,7 @@ 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 + +nparray=scene.render(width = 600, height=400, remove_temp=1) + +plt.imshow(nparray) 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..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,9 +56,9 @@ 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): + show_window=False, tempfile=None,exit_when_done=True): """ Renders the provided scene description with POV-Ray. @@ -89,28 +93,39 @@ 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] - 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) + cmd = [POVRAY_BINARY] + if os.name=='nt': + if exit_when_done: + cmd.append('/EXIT') + cmd.append('/RENDER') + cmd.append(pov_file) + 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) + 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 +134,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,bitspercolor=bitspercolor) + else: + 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 203309b..33616ca 100644 --- a/vapory/vapory.py +++ b/vapory/vapory.py @@ -52,9 +52,10 @@ 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): + 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. @@ -78,8 +79,9 @@ 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, - quality, antialiasing, remove_temp, show_window, tempfile) + return render_povstring(str(self), outfile, height, width,bitspercolor, + quality, antialiasing, remove_temp, show_window, tempfile, + exit_when_done) class POVRayElement: