Difference between revisions of "FocalBlur"

From PyMOLWiki
Jump to: navigation, search
Line 27: Line 27:
<gallery perrow=3 widths=300 heights=240>
|[[Image:FocalBlur_a1.0_r1.png|thumb|400px|left|FocalBlur aperture=1,samples=100,ray=1]]
Image:FocalBlur_a1.0_r1.png|FocalBlur aperture=1,samples=100,ray=1
|[[Image:FocalBlur_a2.0_r1.png|thumb|400px|left|FocalBlur aperture=2,samples=100,ray=1]]
Image:FocalBlur_a2.0_r1.png|FocalBlur aperture=2,samples=100,ray=1
Image:FocalBlur_a4.0_r1.png|FocalBlur aperture=4,samples=400,ray=1
|[[Image:FocalBlur_a4.0_r1.png|thumb|400px|left|FocalBlur aperture=4,samples=400,ray=1]]
Image:FocalBlur_a4.0_r0.png|FocalBlur aperture=4,samples=400,ray=0
|[[Image:FocalBlur_a4.0_r0.png|thumb|400px|left|FocalBlur aperture=4,samples=400,ray=0]]
Image:Focal_blur _ex6.png
Image:Focal blur_ex_ap3.png
Image:Focal blur_ex_ap3_mode1.png
Image:Focal blur_ex_ap3_mode2.png
Image:Focal blur_ex_ap3_mode3.png

Revision as of 20:27, 15 June 2011


This script creates fancy figures by introducing a focal blur to the image. The object at the origin will be in focus.


Load the script using the run command. Execute the script using PyMOL syntax:

FocalBlur aperture=2.0,samples=100,ray=1

or using python syntax:


For additional options, see the script comments.


  • When using raytracing, the image creation will take n times longer than normal, where n is the number of samples.
  • The script uses ImageMagick for creating the blured image. It has only been tested on Linux
  • The aperture is a purely arbitrary value and not related to f stops on a camera.
  • There is a bug preventing custom image sizes when not using raytracing.



Load the script using the run command

from pymol import cmd
from os import system
from tempfile import mkdtemp
from shutil import rmtree
from math import sin,cos,pi,sqrt
import subprocess
def FocalBlur(aperture=2.0,samples=10,ray=0,width=0,height=0):
    Creates fancy figures by introducing a focal blur to the image. The object
    at the origin will be in focus. 
    Jarl Underhaug
    University of Bergen

    Updates by Jason Vertrees and Thomas Holder
    FocalBlur aperture=float, samples=int, ray=0/1, width=int, height=int
    FocalBlur aperture=1, samples=100
    FocalBlur aperture=2, samples=100, ray=1, width=600, height=400
    # Formalize the parameter types
    ray = (ray in ("True", "true", 1, "1"))
    aperture, samples = float(aperture), int(samples)
    width, height = int(width), int(height)
    # Because of a bug, only custom sizes when raytracing
    #if not ray:
    #    width=0
    #    height=0
    # Create a temporary directory
    tmpdir = mkdtemp()
    # Get the orientation of the protein and the light
    light = cmd.get('light')[1:-1]
    light = [float(s) for s in light.split(',')]
    view = cmd.get_view()
    # Rotate the protein and the light in order to create the blur
    for frame in range(samples):
        # Angles to rotate protein and light
        # Populate angles as Fermat's spiral
        theta = frame * pi * 110.0/144.0
        radius = 0.5 * aperture * sqrt(frame/float(samples-1))
        x = cos(theta) * radius
        y = sin(theta) * radius
        xr = x/180.0*pi
        yr = y/180.0*pi
        # Rotate the protein
        # Rotate the light
        ly = light[1]*cos(xr)-light[2]*sin(xr)
        lz = light[2]*cos(xr)+light[1]*sin(xr)
        lx = light[0]*cos(yr)+lz*sin(yr)
        lz = lz*cos(yr)-lx*sin(yr)
        curFile = "%s/frame-%04d.png" % (tmpdir,frame)

        # Save the image
	if ray:
        # Return the protein and the light to the original orientation
    # Create a blured image of all the frames
    r = subprocess.call('convert %s/frame-*.png +matte -average %s/blur.png' % (tmpdir,tmpdir),shell=True)

    # Load the blured image
    print "load %s/blur.png" % tmpdir 
    cmd.load('%s/blur.png' % tmpdir)
    # Delete the temporary files
cmd.extend('FocalBlur', FocalBlur)