10 examples of 'numpy array to pil image' in Python

Every line of 'numpy array to pil image' 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
60def pil_image_to_ndarray(pil_image, grayscale, num_channels):
61 ret = convert_pil(pil_image, grayscale, num_channels)
62 return np.asarray(ret, dtype=np.uint8)
290def numpy_to_pil(nparray):
291 """
292 Numpy matrix to PIL Image.
293 """
294 return Image.fromarray(nparray)
14def numpy_to_image(image):
15
16 image = image*1.0
17 # Remove VGG optimization
18 # if image.shape == (1,400,400,3):
19 if image.ndim == 4:
20 image += np.array([123.68, 116.779, 103.939]).reshape((1,1,1,3))
21 # Cut unneeded axis
22 image = image[0]
23 elif image.ndim == 3:
24 # elif image.shape == (400,400,3):
25 image += np.array([123.68, 116.779, 103.939]).reshape((1,1,3))
26
27 image = np.clip(image, 0,255).astype("uint8")
28
29 # Make grayscale
30 image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
31 return image_gray
320def convert_ndarray2image(image):
321 """ convert ndarray to PIL image if it not already
322
323 :param ndarray image: input image
324 :return Image: output image
325
326 >>> img = np.random.random((50, 50, 3))
327 >>> image = convert_ndarray2image(img)
328 >>> isinstance(image, Image.Image)
329 True
330 """
331 if isinstance(image, np.ndarray):
332 if np.max(image) <= 1.5:
333 image = image * 255
334 np.clip(image, a_min=0, a_max=255, out=image)
335 if image.ndim == 3 and image.shape[-1] < 3:
336 image = image[:, :, 0]
337 image = Image.fromarray(image.astype(np.uint8))
338 return image
9def getImageArr(im):
10
11 img = im.astype(np.float32)
12
13 img[:, :, 0] -= 103.939
14 img[:, :, 1] -= 116.779
15 img[:, :, 2] -= 123.68
16
17 return img
24def load_image_into_numpy_array(image):
25 image = preprocess_image(image)
26 (im_width, im_height) = image.size
27 return np.array(image.getdata()).reshape(
28 (im_height, im_width, 3)).astype(np.uint8)
107def image_to_data(image):
108 """Generator function to convert a PIL image to 16-bit 565 RGB bytes."""
109 #NumPy is much faster at doing this. NumPy code provided by:
110 #Keith (https://www.blogger.com/profile/02555547344016007163)
111 pb = np.array(image.convert('RGB')).astype('uint16')
112 color = ((pb[:,:,0] & 0xF8) << 8) | ((pb[:,:,1] & 0xFC) << 3) | (pb[:,:,2] >> 3)
113 return np.dstack(((color >> 8) & 0xFF, color & 0xFF)).flatten().tolist()
37def PILToRGB(pil_array):
38 """ convert a PIL-compatible integer into an RGB % array """
39 pil_color = tuple(pil_array)
40 hexstr = '%06x' % pil_color
41 # reverse byte order
42 r, g, b = hexstr[4:], hexstr[2:4], hexstr[:2]
43 r, g, b = [int(n, 16) for n in (r, g, b)]
44 return [r, g, b]
12def load_image_into_numpy_array(img):
13 (im_width, im_height) = img.size
14 return np.array(img.getdata()).reshape(
15 (im_height, im_width, 3)).astype(np.uint8)
48def img_to_np(img):
49 img = img.cpu().numpy()
50 img = np.copy(img)
51 img = np.swapaxes(img, 0, 1)
52 img = np.swapaxes(img, 1, 2)
53
54 return img

Related snippets