Scripts: flatimage.py

File flatimage.py, 2.8 KB (added by goddard, 16 years ago)
Line 
1# -----------------------------------------------------------------------------
2# Script to make flattened icosahedron virus images for VIPERdb.
3#
4
5vdb_dir = '/usr/local/src/staff/goddard/vdbfiles'
6image_dir = '/usr/local/src/staff/goddard/vdbimages'
7
8def flattened_icosahedron_images(
9 vdb_dir, # directory containing *.vdb files.
10 image_dir, # directory to place images in.
11 image_height = 2000, # screen pixels
12 image_format = 'JPEG', # 'JPEG', 'PNG', 'TIFF'
13 image_suffix = '.jpg',
14 mesh_color = (.5,.5,.5,1), # red, green, blue, opacity
15 mesh_offset = 0, # Can make mesh appear in front of molecules.
16 edge_pad = .05, # 5% edge padding
17 supersample = 3, # Smooth jagged edges in images.
18 background_color = 'white',
19 ):
20
21 from chimera import viewer, runCommand, openModels
22
23 # Find all vdb files in vdb_dir directory.
24 import os
25 vdb_filenames = [f for f in os.listdir(vdb_dir) if f.endswith('.vdb')]
26
27 # Set window to have correct aspect ratio.
28 runCommand('windowsize 400 800')
29
30 # Set background color.
31 runCommand('set bg_color %s' % background_color)
32
33 # Set silhouette edges
34 runCommand('set silhouette')
35
36 for vdb_filename in vdb_filenames:
37
38 import os.path
39 vdb_path = os.path.join(vdb_dir, vdb_filename)
40
41 # Open VIPERdb file
42 openModels.open(vdb_path)
43
44 # Flatten multiscale model.
45 import FlattenIcosahedron as FI
46 mlist = FI.multiscale_models()
47 r = FI.model_radius(mlist)
48 FI.flatten_icosahedron(mlist, r)
49
50 # Show triangle outlines.
51 varray, tarray = FI.flattened_icosahedron_geometry(r)
52 varray[:,2] = mesh_offset
53 model = mlist[0].surface_model()
54 p = model.addPiece(varray, tarray, mesh_color)
55 p.displayStyle = p.Mesh
56
57 # Zoom to fit in window.
58 runCommand('turn z 90') # Orient long side vertically.
59 viewer.viewAll() # Fit in window.
60 viewer.scaleFactor *= (1-edge_pad) # Provide a bit of edge padding.
61
62 # Save image.
63 image_filename = os.path.splitext(vdb_filename)[0] + image_suffix
64 image_path = os.path.join(image_dir, image_filename)
65 image_aspect = 0.5
66 image_width = image_aspect * image_height
67 from chimera.printer import saveImage
68 saveImage(image_path, image_width, image_height,
69 format = image_format, supersample = supersample)
70
71 # Close models
72 openModels.close(openModels.list())
73
74
75# Make low-res images.
76flattened_icosahedron_images(vdb_dir, image_dir,
77 image_height = 600)
78
79# Make high-res images.
80flattened_icosahedron_images(vdb_dir, image_dir,
81 image_height = 2000,
82 image_suffix = '_big.jpg')
83
84# Quit Chimera
85#raise SystemExit('Script finished')