Sage Wrapper for Bitmap Images

Some computations in Sage return bitmap images, for example matrices can be turned into bitmaps directly. Note that this is different from all plotting functionality, the latter can equally produce vector graphics. This module is about bitmaps only, and a shallow wrapper around PIL.Image. The only difference is that Image is displayed as graphics by the Sage if the UI can.

EXAMPLES:

sage: from sage.repl.image import Image
sage: img = Image('RGB', (256, 256), 'white')
sage: pixels = img.pixels()
sage: for x in range(img.width()):
....:     for y in range(img.height()):
....:         pixels[x, y] = (x, y, 100)
sage: img
256x256px 24-bit RGB image
sage: type(img)
<class 'sage.repl.image.Image'>
class sage.repl.image.Image(mode, size, color='white')

Bases: sage.structure.sage_object.SageObject

Creates a new image with the given mode and size.

INPUT:

  • mode – string. The mode to use for the new image. Valid options are:

    • '1' (1-bit pixels, black and white, stored with one pixel per byte)
    • 'L' (8-bit pixels, black and white)
    • 'P' (8-bit pixels, mapped to any other mode using a color palette)
    • 'RGB' (3x8-bit pixels, true color)
    • 'RGBA' (4x8-bit pixels, true color with transparency mask)
    • 'CMYK' (4x8-bit pixels, color separation)
    • 'YCbCr' (3x8-bit pixels, color video format)
    • 'LAB' (3x8-bit pixels, the L*a*b color space)
    • 'HSV' (3x8-bit pixels, Hue, Saturation, Value color space)
    • 'I' (32-bit signed integer pixels)
    • 'F' (32-bit floating point pixels)
  • size – 2-tuple, containing (width, height) in pixels.

  • color – string or tuple of numeric. What colour to use for the image. Default is black. If given, this should be a a tuple with one value per band. When creating RGB images, you can also use colour strings as supported by the ImageColor module. If the colour is None, the image is not initialised.

OUTPUT:

A new Image object.

EXAMPLES:

sage: from sage.repl.image import Image
sage: Image('P', (16, 16), (13,))
16x16px 8-bit Color image
height()

Return the vertical dimension in pixels

OUTPUT:

Integer.

EXAMPLES:

sage: from sage.repl.image import Image
sage: img = Image('1', (12, 34), 'white')
sage: img.width()
12
sage: img.height()
34
mode()

Return the color mode

OUTPUT:

String. As given when constructing the image.

EXAMPLES:

sage: from sage.repl.image import Image
sage: img = Image('YCbCr', (16, 16), 'white')
sage: img.mode()
'YCbCr'
pil

Access the wrapped PIL(low) Image

OUTPUT:

The underlying PIL.Image.Image object.

EXAMPLES:

sage: from sage.repl.image import Image
sage: img = Image('RGB', (16, 16), 'white')
sage: img.pil
<PIL.Image.Image image mode=RGB size=16x16 at 0x...>
pixels()

Return the pixel map

OUTPUT:

The PIL PixelAccess object that allows you to get/set the pixel data.

EXAMPLES:

sage: from sage.repl.image import Image
sage: img = Image('RGB', (16, 16), 'white')
sage: img.pixels()
<PixelAccess object at 0x...>
save(filename)

Save the bitmap image

INPUT:

  • filename – string. The filename to save as. The given extension automatically determines the image file type.

EXAMPLES:

sage: from sage.repl.image import Image
sage: img = Image('P', (12, 34), (13,))
sage: filename = tmp_filename(ext='.png')
sage: img.save(filename)
sage: with open(filename, 'rb') as f:
....:     f.read(4) == b'\x89PNG'
True
show()

Show this image immediately.

This method attempts to display the graphics immediately, without waiting for the currently running code (if any) to return to the command line. Be careful, calling it from within a loop will potentially launch a large number of external viewer programs.

OUTPUT:

This method does not return anything. Use save() if you want to save the figure as an image.

EXAMPLES:

sage: from sage.repl.image import Image
sage: img = Image('1', (12, 34), 'white')
sage: img.show()
width()

Return the horizontal dimension in pixels

OUTPUT:

Integer.

EXAMPLES:

sage: from sage.repl.image import Image
sage: img = Image('1', (12, 34), 'white')
sage: img.width()
12
sage: img.height()
34