Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ nosetests.xml
# Pipy codes

.pypirc

# povray output
*.pov
*.ppm
*.png

Thumbs.db
7 changes: 6 additions & 1 deletion examples/sphere.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
""" Just a purple sphere """

from vapory import *
import matplotlib.pyplot as plt


scene = Scene(

Expand All @@ -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)

nparray=scene.render(width = 600, height=400, remove_temp=1)

plt.imshow(nparray)
2 changes: 1 addition & 1 deletion vapory/config.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
44 changes: 31 additions & 13 deletions vapory/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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.

Expand Down Expand Up @@ -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)

Expand All @@ -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:
Expand Down
10 changes: 6 additions & 4 deletions vapory/vapory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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:
Expand Down