10 examples of 'python save image as png' in Python

Every line of 'python save image as png' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
112def save(self, path, fileformat='png'):
113 if fileformat == 'png':
114 self.surface.write_to_png(path)
204def save(self, filename):
205 self.image.write(filename)
179def save_image(ax=None):
180 image_data = StringIO()
181 save_html(plt.gcf(), image_data)
182 image_data.seek(0)
183 return image_data
11def png(self, width=700, height=500):
12 """ Build an Ipython "Image" (requires iPython). """
13 return image_png(self, width=width, height=height)
14def to_png(data, size, level=6, output=None):
15 # type: (bytes, Tuple[int, int], int, Optional[str]) -> Optional[bytes]
16 """
17 Dump data to a PNG file. If `output` is `None`, create no file but return
18 the whole PNG data.
19
20 :param bytes data: RGBRGB...RGB data.
21 :param tuple size: The (width, height) pair.
22 :param int level: PNG compression level.
23 :param str output: Output file name.
24 """
25
26 width, height = size
27 line = width * 3
28 png_filter = struct.pack(">B", 0)
29 scanlines = b"".join(
30 [png_filter + data[y * line : y * line + line] for y in range(height)]
31 )
32
33 magic = struct.pack(">8B", 137, 80, 78, 71, 13, 10, 26, 10)
34
35 # Header: size, marker, data, CRC32
36 ihdr = [b"", b"IHDR", b"", b""]
37 ihdr[2] = struct.pack(">2I5B", width, height, 8, 2, 0, 0, 0)
38 ihdr[3] = struct.pack(">I", zlib.crc32(b"".join(ihdr[1:3])) & 0xFFFFFFFF)
39 ihdr[0] = struct.pack(">I", len(ihdr[2]))
40
41 # Data: size, marker, data, CRC32
42 idat = [b"", b"IDAT", zlib.compress(scanlines, level), b""]
43 idat[3] = struct.pack(">I", zlib.crc32(b"".join(idat[1:3])) & 0xFFFFFFFF)
44 idat[0] = struct.pack(">I", len(idat[2]))
45
46 # Footer: size, marker, None, CRC32
47 iend = [b"", b"IEND", b"", b""]
48 iend[3] = struct.pack(">I", zlib.crc32(iend[1]) & 0xFFFFFFFF)
49 iend[0] = struct.pack(">I", len(iend[2]))
50
51 if not output:
52 # Returns raw bytes of the whole PNG data
53 return magic + b"".join(ihdr + idat + iend)
54
55 with open(output, "wb") as fileh:
56 fileh.write(magic)
57 fileh.write(b"".join(ihdr))
58 fileh.write(b"".join(idat))
59 fileh.write(b"".join(iend))
60
61 return None
12def save_image(filename, data):
13 img= data[:,:,[2,1,0]]
14 # img = data.clone().clamp(0, 255).numpy()
15 # img = img.transpose(1, 2, 0).astype("uint8")
16 img=img.astype("uint8")
17 img = Image.fromarray(img)
18 img.save(filename)
58def save(self, filename):
59 self.img.write_to_png(filename)
73def _repr_png_(self, _crop_box=None):
74 """Hook for IPython Notebook
75
76 See: http://ipython.org/ipython-doc/stable/config/integrating.html
77 """
78 if _crop_box:
79 image = self.pil_image.crop(_crop_box)
80 else:
81 image = self.pil_image
82 buf = BytesIO()
83 image.save(buf, "PNG")
84 return buf.getvalue()
57def save(self, filename, output):
58 filename = '{0}.{1}'.format(filename, self.format.lower())
59 output.save(filename, self.format.upper())
60 return filename
510def print_png(self, filename_or_obj, *args, **kwargs):
511 FigureCanvasAgg.draw(self)
512 renderer = self.get_renderer()
513 original_dpi = renderer.dpi
514 renderer.dpi = self.figure.dpi
515 if is_string_like(filename_or_obj):
516 filename_or_obj = open(filename_or_obj, 'wb')
517 close = True
518 else:
519 close = False
520 try:
521 _png.write_png(renderer._renderer.buffer_rgba(),
522 renderer.width, renderer.height,
523 filename_or_obj, self.figure.dpi)
524 finally:
525 if close:
526 filename_or_obj.close()
527 renderer.dpi = original_dpi

Related snippets