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

16 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1import numpy as np 

2 

3from ase.io.eps import EPS 

4 

5 

6class PNG(EPS): 

7 def write_header(self, fd): 

8 pass 

9 

10 def _renderer(self, fd): 

11 from matplotlib.backends.backend_agg import RendererAgg 

12 

13 dpi = 72 

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

15 

16 def write_trailer(self, fd, renderer): 

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

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

19 import matplotlib.image 

20 

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 ) 

27 matplotlib.image.imsave(fd, array, format='png') 

28 

29 

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

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