Skip to content

Instantly share code, notes, and snippets.

@somada141
somada141 / vtkimagedata_view_orthogonal.py
Last active August 29, 2015 14:08
Create an orthogonal slice view through a 3D vtkImageData object #python #vtk #imagedata #dicom #visualization
# Assuming that we have an object of type 'vtkImageData' under the name of
# 'image'
# Create a new vtkImageSliceMapper
mapper = vtk.vtkImageSliceMapper()
# Set 'image' as the input dataset
mapper.SetInput(image)
# Set the orthogonal slice orientation to be perpendicular to the 'X' axis. Use
# 'SetOrientationToY' and 'SetOrientationToZ' for the Y and Z axes.
# Alternatively you can use the 'SetOrientation' method with a 'int' parameter
@somada141
somada141 / tree_directory_ascii.md
Last active August 29, 2015 14:08
ASCII representation of the current directory structure #shell #os #unix #osx #bash

Firstly, define the tree command as such:

alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"

The cd to the directory in question and run tree. You should get an output such as this:

@somada141
somada141 / build_opencv_anaconda.txt
Last active August 29, 2015 14:07
How to build OpenCV against Anaconda Python on Mac OSX #osx #build #cmake #opencv
sources:
http://smh84.wordpress.com/2014/04/23/install-opencv-in-anaconda-1-9-2-on-mac-os-x-mavericks/
https://gist.github.com/welch/6468594
It is a rite of passage to post one's successful build instructions for OpenCV on a Mac
after you've tried all the other conflicting instructions out there and still failed.
brew failed for me (was this because I could never get a happy brew doctor situation?
I'll never know). macports? nope. build-from-source recipes? I didn't find one that
worked for me.
@somada141
somada141 / volume_rendering_vtk.py
Last active May 1, 2021 05:24
Volume rendering in Python using VTK-SimpleITK #python #visualization #vtk #simpleitk #itk
#!/usr/bin/python
import SimpleITK as sitk
import vtk
import numpy as np
from vtk.util.vtkConstants import *
def numpy2VTK(img,spacing=[1.0,1.0,1.0]):
# evolved from code from Stou S.,
@somada141
somada141 / vtk_glyphs_custom.py
Last active August 29, 2015 14:06
Create custom glyphs in VTK #python #visualization #vtk
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from vtk import *
# Let's create a surface with 3 points unconnected.
points = vtkPoints()
points.InsertNextPoint(1, 0, 0)
points.InsertNextPoint(0, 0, 0)
points.InsertNextPoint(0, 1, 0)
@somada141
somada141 / vtk_texture_sphere.py
Last active February 22, 2021 13:29
How to apply a texture to a sphere in VTK #python #visualization #vtk
#!/usr/bin/env python
##
# This example shows how to apply an vtkImageData texture to an sphere
# vtkPolyData object.
# Note: Input jpg file can be located in the VTKData repository.
#
# @author JBallesteros
##
@somada141
somada141 / vtk_triangle_normals_glyph.tcl
Last active August 29, 2015 14:06
How to render glyphs on triangle normals with VTK #tcl #vtk #visualization
# This example shows how to manually construct triangle cell using unstructured grids
# and display its surface normal.
#
package require vtk
package require vtkinteraction
# Create an unstructured grids containing a triangle cell.
vtkPoints trianglePoints
trianglePoints SetNumberOfPoints 3
@somada141
somada141 / python_download_requests.py
Last active December 31, 2016 09:36
Download files with Python and the requests package #python #fileIO #web #requests
import requests
def download_file(url):
local_filename = url.split('/')[-1]
# NOTE the stream=True parameter
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
@somada141
somada141 / python_get_directory_of_file.md
Last active August 29, 2015 14:06
Get the file directory in Python #python #os
@somada141
somada141 / ipython_notebook_vtk.md
Last active January 31, 2022 20:36
Display VTK renders into IPython Notebook #python #visualization #vtk #ipython #ipythonnotebook

This function takes a renderer and displays the output directly into IPython

def vtk_show(renderer, w=100, h=100):
    """
    Takes vtkRenderer instance and returns an IPython Image with the rendering.
    """
    renderWindow = vtkRenderWindow()
    renderWindow.SetOffScreenRendering(1)
 renderWindow.AddRenderer(renderer)