File: matplotlib_as_texture.py

package info (click to toggle)
python-moderngl 5.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,700 kB
  • sloc: python: 15,758; cpp: 14,665; makefile: 14
file content (41 lines) | stat: -rw-r--r-- 1,054 bytes parent folder | download | duplicates (2)
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
import io

import numpy as np
from PIL import Image

from basic_colors_and_texture import ColorsAndTexture

import matplotlib
matplotlib.use('svg')
import matplotlib.pyplot as plt


class MatplotlibTexture(ColorsAndTexture):
    title = "Matplotlib as Texture"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        figure_size = (640, 360)

        temp = io.BytesIO()
        plt.figure(0, figsize=(figure_size[0] / 72, figure_size[1] / 72))

        mu, sigma = 100, 15
        x = mu + sigma * np.random.randn(10000)
        n, bins, patches = plt.hist(x, 50, density=True, facecolor='r', alpha=0.75)

        plt.axis([40, 160, 0, 0.03])
        plt.grid(True)
        plt.show()

        plt.savefig(temp, format='raw', dpi=72)
        temp.seek(0)

        img = Image.frombytes('RGBA', figure_size, temp.read()).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB')
        self.texture = self.ctx.texture(img.size, 3, img.tobytes())
        self.texture.build_mipmaps()


if __name__ == '__main__':
    MatplotlibTexture.run()