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

16 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-12-10 11:04 +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 dpi = 72 

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

14 

15 def write_trailer(self, fd, renderer): 

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

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

18 import matplotlib.image 

19 buf = renderer.buffer_rgba() 

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

21 # That might be an implementation detail. 

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

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

24 matplotlib.image.imsave( 

25 fd, array, format="png") 

26 

27 

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

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