File: isurface_create_for_data2.py

package info (click to toggle)
py3cairo 1.10.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,308 kB
  • sloc: python: 12,287; ansic: 4,349; makefile: 141; sh: 32
file content (30 lines) | stat: -rwxr-xr-x 962 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python
"""test cairo.ImageSurface.create_for_data() with a numpy array
"""
import tempfile

import cairo
import numpy

if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS):
  raise SystemExit ('cairo was not compiled with ImageSurface and PNG support')

h, fileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
width, height = 255, 255
data = numpy.ndarray (shape=(height,width,4), dtype=numpy.uint8)

for x in range(width):
  for y in range(height):
    alpha = y

    # cairo.FORMAT_ARGB32 uses pre-multiplied alpha
    data[y][x][0] = int(x * alpha/255.0) # B
    data[y][x][1] = int(y * alpha/255.0) # G
    data[y][x][2] = 0                    # R
    data[y][x][3] = alpha                # A

surface = cairo.ImageSurface.create_for_data (data, cairo.FORMAT_ARGB32,
                                              width, height)
ctx = cairo.Context(surface)
surface.write_to_png(fileName)
print "see %s output file" % fileName