Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import numpy as np 

2from ase.io.eps import EPS 

3 

4 

5class PNG(EPS): 

6 def write_header(self, fd): 

7 pass 

8 

9 def _renderer(self, fd): 

10 from matplotlib.backends.backend_agg import RendererAgg 

11 dpi = 72 

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

13 

14 def write_trailer(self, fd, renderer): 

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

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

17 import matplotlib.image 

18 buf = renderer.buffer_rgba() 

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

20 # That might be an implementation detail. 

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

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

23 matplotlib.image.imsave( 

24 fd, array, format="png") 

25 

26 

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

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