Coverage for /builds/ase/ase/ase/io/png.py: 100.00%

16 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-08-02 00:12 +0000

1# fmt: off 

2 

3import numpy as np 

4 

5from ase.io.eps import EPS 

6 

7 

8class PNG(EPS): 

9 def write_header(self, fd): 

10 pass 

11 

12 def _renderer(self, fd): 

13 from matplotlib.backends.backend_agg import RendererAgg 

14 dpi = 72 

15 return RendererAgg(self.w, self.h, dpi) 

16 

17 def write_trailer(self, fd, renderer): 

18 # The array conversion magic is necessary to make things work with 

19 # matplotlib 2.0.0, 3.2.x, and 3.3.0 at the same time. 

20 import matplotlib.image 

21 buf = renderer.buffer_rgba() 

22 # Buf is of type bytes (matplotlib < 3.3.0) or memoryview. 

23 # That might be an implementation detail. 

24 array = np.frombuffer(buf, dtype=np.uint8).reshape( 

25 int(self.h), int(self.w), 4) 

26 matplotlib.image.imsave( 

27 fd, array, format="png") 

28 

29 

30def write_png(filename, atoms, **parameters): 

31 PNG(atoms, **parameters).write(filename)