Sam Skillman committed 4688ae1

Adding cookbook on saving images with different background colors.

Comments (0)

Files changed (1)


+from yt.mods import *
+# This shows how to save ImageArray objects, such as those returned from 
+# volume renderings, to pngs with varying backgrounds.
+# Lets make a fake "rendering" that has 4 channels and looks like a linear
+# gradient from the bottom to top.
+im = np.zeros([64,128,4])
+for i in xrange(im.shape[0]):
+    for k in xrange(im.shape[2]):
+        im[i,:,k] = np.linspace(0.,10.*k, im.shape[1])
+im_arr = ImageArray(im)
+# in this case you would have gotten im_arr from something like:
+# im_arr = cam.snapshot() 
+# To save it with the default settings, we can just use write_png, where it 
+# rescales the image and uses a black background.
+# write_png accepts a background keyword argument that defaults to 'black'.
+# Other choices include:
+# black (0.,0.,0.,1.)
+# white (1.,1.,1.,1.)
+# None  (0.,0.,0.,0.) <-- Transparent!
+# any rgba list/array: [r,g,b,a], bounded by 0..1
+im_arr.write_png('black_bg.png', background='black')
+im_arr.write_png('white_bg.png', background='white')
+im_arr.write_png('green_bg.png', background=[0.,1.,0.,1.])
+im_arr.write_png('transparent_bg.png', background=None)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.