9 examples of 'pil image save' in Python

Every line of 'pil image save' 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
this disclaimer
103@staticmethod
104def save(filename, width, height, fmt, pixels, flipped=False):
105 image = PILImage.fromstring(fmt.upper(), (width, height), pixels)
106 if flipped:
107 image = image.transpose(PILImage.FLIP_TOP_BOTTOM)
108 image.save(filename)
109 return True
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

54def save_img(img_pil, subfolder, name):
55 img_pil.save(subfolder+name+'.png')
1001def _pil_convert_(self, image, mode="L"):
1002 """ Convert image. Actually calls ``image.convert(mode)``.
1003
1004 Parameters
1005 ----------
1006 mode : str
1007 Pass 'L' to convert to grayscale
1008 src : str
1009 Component to get images from. Default is 'images'.
1010 dst : str
1011 Component to write images to. Default is 'images'.
1012 p : float
1013 Probability of applying the transform. Default is 1.
1014 """
1015 return image.convert(mode)
122def _save(im, fp, filename, check=0):
123
124 try:
125 version, bits, planes, rawmode = SAVE[im.mode]
126 except KeyError:
127 raise ValueError("Cannot save %s images as PCX" % im.mode)
128
129 if check:
130 return check
131
132 # bytes per plane
133 stride = (im.size[0] * bits + 7) // 8
134 # stride should be even
135 stride += stride % 2
136 # Stride needs to be kept in sync with the PcxEncode.c version.
137 # Ideally it should be passed in in the state, but the bytes value
138 # gets overwritten.
139
140 logger.debug("PcxImagePlugin._save: xwidth: %d, bits: %d, stride: %d",
141 im.size[0], bits, stride)
142
143 # under windows, we could determine the current screen size with
144 # "Image.core.display_mode()[1]", but I think that's overkill...
145
146 screen = im.size
147
148 dpi = 100, 100
149
150 # PCX header
151 fp.write(
152 o8(10) + o8(version) + o8(1) + o8(bits) + o16(0) +
153 o16(0) + o16(im.size[0]-1) + o16(im.size[1]-1) + o16(dpi[0]) +
154 o16(dpi[1]) + b"\0"*24 + b"\xFF"*24 + b"\0" + o8(planes) +
155 o16(stride) + o16(1) + o16(screen[0]) + o16(screen[1]) +
156 b"\0"*54
157 )
158
159 assert fp.tell() == 128
160
161 ImageFile._save(im, fp, [("pcx", (0, 0)+im.size, 0,
162 (rawmode, bits*planes))])
163
164 if im.mode == "P":
165 # colour palette
166 fp.write(o8(12))
167 fp.write(im.im.getpalette("RGB", "RGB")) # 768 bytes
168 elif im.mode == "L":
169 # greyscale palette
170 fp.write(o8(12))
171 for i in range(256):
172 fp.write(o8(i)*3)
322def _save(im, fp, filename, check=0):
323
324 try:
325 image_type, rawmode = SAVE[im.mode]
326 except KeyError:
327 raise ValueError("Cannot save %s images as IM" % im.mode)
328
329 frames = im.encoderinfo.get("frames", 1)
330
331 if check:
332 return check
333
334 fp.write(("Image type: %s image\r\n" % image_type).encode('ascii'))
335 if filename:
336 fp.write(("Name: %s\r\n" % filename).encode('ascii'))
337 fp.write(("Image size (x*y): %d*%d\r\n" % im.size).encode('ascii'))
338 fp.write(("File size (no of images): %d\r\n" % frames).encode('ascii'))
339 if im.mode == "P":
340 fp.write(b"Lut: 1\r\n")
341 fp.write(b"\000" * (511-fp.tell()) + b"\032")
342 if im.mode == "P":
343 fp.write(im.im.getpalette("RGB", "RGB;L")) # 768 bytes
344 ImageFile._save(im, fp, [("raw", (0, 0)+im.size, 0, (rawmode, 0, -1))])
69def _save(im, fp, filename):
70
71 if im.mode != "1":
72 raise IOError("cannot write mode %s as XBM" % im.mode)
73
74 fp.write(("#define im_width %d\n" % im.size[0]).encode('ascii'))
75 fp.write(("#define im_height %d\n" % im.size[1]).encode('ascii'))
76
77 hotspot = im.encoderinfo.get("hotspot")
78 if hotspot:
79 fp.write(("#define im_x_hot %d\n" % hotspot[0]).encode('ascii'))
80 fp.write(("#define im_y_hot %d\n" % hotspot[1]).encode('ascii'))
81
82 fp.write(b"static char im_bits[] = {\n")
83
84 ImageFile._save(im, fp, [("xbm", (0,0)+im.size, 0, None)])
85
86 fp.write(b"};\n")
30def save_im(filename, im):
31 im = np.rollaxis(im, 0, 3) # (c, h, w) -> (h, w, c)
32 im = Image.fromarray(im)
33 im.save(filename)
43def write(self, filename, image):
44 logger.trace("Outputting: (filename: '%s'", filename)
45 filename = self.output_filename(filename)
46 try:
47 with open(filename, "wb") as outfile:
48 outfile.write(image.read())
49 except Exception as err: # pylint: disable=broad-except
50 logger.error("Failed to save image '%s'. Original Error: %s", filename, err)
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)

Related snippets