File: test_plsmem.py.in

package info (click to toggle)
plplot 5.15.0%2Bdfsg-19
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 31,312 kB
  • sloc: ansic: 79,707; xml: 28,583; cpp: 20,033; ada: 19,456; tcl: 12,081; f90: 11,431; ml: 7,276; java: 6,863; python: 6,792; sh: 3,274; perl: 828; lisp: 75; makefile: 50; sed: 34; fortran: 5
file content (127 lines) | stat: -rwxr-xr-x 4,098 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3

#  Copyright (C) 2010 Simon Wood
#  Copyright (C) 2010 Hazen Babcock
#  Copyright (C) 2010-2016 Alan W. Irwin

#  Simple amalgamated demo of plsmem and plsmema under Python for the
#  mem, memcairo, and memqt devices.
#
#  This file is part of PLplot.
#
#  PLplot is free software; you can redistribute it and/or modify
#  it under the terms of the GNU Library General Public License as published
#  by the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  PLplot is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Library General Public License for more details.
#
#  You should have received a copy of the GNU Library General Public License
#  along with PLplot; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#

from PIL import Image
import math
import numpy.numarray

from plplot_python_start import *

import plplot as w

def plot(w, device_name, ifRGBA, width, height, my_buffer, x, y, data, max_val):
    if ifRGBA:
        colour_code = "RGBA"
        # initialise for mem-style RGBA device
        w.plsmema(width,height,my_buffer)
    else:
        colour_code = "RGB"
        # initialise for mem-style RGB device
        w.plsmem(width,height,my_buffer)

    w.plstart (device_name, 1, 1);

    w.plcol0(2)
    w.plenv(0, 360, 0, 90, 0, 2)
    w.plcol0(2)
    w.pllab("Azimuth", "Elevation", "Rectangular Sky View with " + colour_code + " " + device_name + " device")

    # plot binned density
    w.plimage(data, 0, 360, 0, 90, 0, max_val, 0, 360, 0, 90)

    # plot points
    w.plpoin(x,y,5)

    # have to finish plotting so that the plot data gets transferred
    # back from the cairo working surface to the user supplied memory.
    w.plend1()

    # Use fromstring as frombuffer will invert the image
    my_image = Image.fromstring(colour_code, (width,height), my_buffer)
    my_image.save(device_name + "_" + colour_code + ".png")

# ---------------------------------------------------
# Build random array (aka fake data)

w.plseed(1234)
x=[]
y=[]

for i in range(500) :
    x.append(w.plrandd() * 360)
    y.append(w.plrandd() * 90)

# compute binned density on 15' boundaries
# 360' gives 24 divisions
# 90' gives 6 divisions

width = 24
height = 6

max_val = 0
data = numpy.numarray.zeros((width,height))

for i in range(len(x)):
    data[int(x[i]/(360/width))][int(y[i]/(90/height))] += 1
    if data[int(x[i]/(360/width))][int(y[i]/(90/height))] > max_val:
        max_val +=1

# ---------------------------------------------------
# Initialise buffer

# Start from a blank canvas
width = 480
height = 320

# Dark gray background (necessarily opaque for RGB format).
background = numpy.numarray.zeros(3, numpy.uint8) + 30
my_buffer = numpy.numarray.zeros((height,width,3), numpy.uint8) + background

# Dark gray semi-transparent background.
background = numpy.numarray.zeros(4, numpy.uint8) + 30
background[3] = 200
my_buffera = numpy.numarray.zeros((height,width,4), numpy.uint8) + background

'''
# Or open an existing image
# (note 'asarray' will fail as it sees PIL image as an array already and
# does not perform a copy to make a writable array)
src_img = Image.open("input.png")
my_buffer = numpy.array(src_img.convert('RGB'), numpy.uint8)
(width, height) = src_img.size
'''

# Configured device_list depends on what w.plsmem and w.plsmema devices
# are available from the PLplot build.
device_list = @PLSMEM_DEVICE_LIST@
for device_name in device_list:
    # plot writes on top of buffer each time so must use an independent (deep) copy.
    buffer = numpy.array(my_buffer,copy=True)
    plot(w, device_name, False, width, height, buffer, x, y, data, max_val)
    # "mem" device does not (yet) have RGBA capability.
    if device_name != "mem":
        buffer = numpy.array(my_buffera,copy=True)
        plot(w, device_name, True, width, height, buffer, x, y, data, max_val)