File: png.py

package info (click to toggle)
python-ase 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,936 kB
  • sloc: python: 122,428; xml: 946; makefile: 111; javascript: 47
file content (28 lines) | stat: -rw-r--r-- 900 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
import numpy as np
from ase.io.eps import EPS


class PNG(EPS):
    def write_header(self, fd):
        pass

    def _renderer(self, fd):
        from matplotlib.backends.backend_agg import RendererAgg
        dpi = 72
        return RendererAgg(self.w, self.h, dpi)

    def write_trailer(self, fd, renderer):
        # The array conversion magic is necessary to make things work with
        # matplotlib 2.0.0, 3.2.x, and 3.3.0 at the same time.
        import matplotlib.image
        buf = renderer.buffer_rgba()
        # Buf is of type bytes (matplotlib < 3.3.0) or memoryview.
        # That might be an implementation detail.
        array = np.frombuffer(buf, dtype=np.uint8).reshape(
            int(self.h), int(self.w), 4)
        matplotlib.image.imsave(
            fd, array, format="png")


def write_png(filename, atoms, **parameters):
    PNG(atoms, **parameters).write(filename)