This tutorial is part of the skills labs within Interactive Data Science and Visualization.

Images and Buffers

While loading external images likely won't be a common tactic for visualizations in this course, I still want to briefly explore how to use them. We will also mention buffers which are a kind of image that you build from within your sketch which can help "copy and paste" a part of your drawing in repetition. These can sometimes provide a performance boost if you have a complex graphic! Finally, we will look at how we can save images from our sketches.
Contents

Load Images

Let's load a simple image and draw it. Go ahead and add reading.png to your sketchbook. Then, add the following code:
import sketchingpy

sketch = sketchingpy.Sketch2DWeb(500, 500)
sketch.clear('#707070')

image = sketch.get_image('reading.png')
image.resize(150, 150)
sketch.set_image_mode('corner')
sketch.draw_image(250, 250, image)

sketch.show()
After giving this a run, try changing the resize dimensions. Finally, try changing the mode specified in set_image_mode. There are two options: center and corner.

Transparency

Some file formats like png can support transparency. Others always have a filled background. To demonstrate how transparency works, try this interactive sketch.
import sketchingpy

sketch = sketchingpy.Sketch2DWeb(500, 500)

image = sketch.get_image('reading.png')
image.resize(150, 150)

def draw(sketch):
    sketch.clear('#707070')

    mouse = sketch.get_mouse()

    sketch.clear_stroke()
    sketch.set_ellipse_mode('radius')
    sketch.set_fill('#A6CEE350')
    sketch.draw_ellipse(250, 250, 100, 100)

    sketch.set_image_mode('center')
    sketch.draw_image(mouse.get_pointer_x(), mouse.get_pointer_y(), image)

sketch.on_step(draw)

sketch.show()
Try changing reading.png to reading.jpg instead. While PNG has a transparency layer, JPEG doesn't support transparency.

Buffers

We won't go too far into buffers but, if you have an "expensive" part of your graphic to draw, it might make sense to save a copy of your work instead of having it recreated from scrach on each step. Buffers are like images that aren't saved to files but, instead, are held on to by the computer in memory for quick re-drawing. For more details, see the buffers example.

All that said, to motivate why you might take this approach, imagine your are drawing the user's location on a map. Let's say that the map is made up of thousands of points and lines. It can be expensive to draw that each time by calling draw_line. Instead, you might save the results of your work after drawing it once, writing to one of these in-memory images and just drawing glyphs on top of that map like, for example, to indicate where the user currently within that space.

Save Images

All this in mind, perhaps most important for us is saving from our sketches. Let's make a simple image file with an ellipse.
import sketchingpy

sketch = sketchingpy.Sketch2D(500, 500)
sketch.draw_ellipse(250, 250, 100, 100)
sketch.save_image('ellipse.png')
sketch.show()
By default, this will have a transparent background (for most platforms). Note that, if you don't want a transparent background, you can specify a background color by calling clear.

Next

I think we have everything we need to make drawings. Let's next take a brief look at interactivity. Continue on to Tutorial 10 to learn about user loops.
Citations