7 examples of 'pillow resize image' in Python

Every line of 'pillow resize 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
10def _resize_pillow(src, height, width, interpolation):
11 if interpolation == "linear":
12 interpolation = PIL.Image.LINEAR
13 elif interpolation == "nearest":
14 interpolation = PIL.Image.NEAREST
15 else:
16 raise ValueError("unsupported interpolation: {}".format(interpolation))
17
18 if np.issubdtype(src.dtype, np.integer):
19 dst = PIL.Image.fromarray(src)
20 dst = dst.resize((width, height), resample=interpolation)
21 dst = np.array(dst)
22 else:
23 assert np.issubdtype(src.dtype, np.floating)
24 ndim = src.ndim
25 if ndim == 2:
26 src = src[:, :, None]
27
28 C = src.shape[2]
29 dst = np.zeros((height, width, C), dtype=src.dtype)
30 for c in range(C):
31 src_c = src[:, :, c]
32 src_c = PIL.Image.fromarray(src_c)
33 dst[:, :, c] = src_c.resize(
34 (width, height), resample=interpolation
35 )
36
37 if ndim == 2:
38 dst = dst[:, :, 0]
39 return dst
28def pillow():
29 IN = '../images/bird.jpg'
30 PIXL_OUT = '../bin/bird_scl_pixl.jpg'
31 PILLOW_OUT = '../bin/bird_scl_pillow.jpg'
32
33 TARGET_WIDTH = 500
34 TARGET_HEIGHT = 333
35
36 # pixl
37 start = time.time()
38 image = pixl.Image(IN)
39 #image.resize(TARGET_WIDTH, TARGET_HEIGHT, pixl.ResizeMethod.BILINEAR)
40 image.convolution([0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0], 1.0).invert().grayscale()
41
42 image.save(PIXL_OUT)
43 print('pixl:', time.time()-start, "s")
44
45 # pillow
46 start = time.time()
47 image = Image.open(IN)
48 image = image.resize((TARGET_WIDTH, TARGET_HEIGHT), PIL.Image.BILINEAR)
49 image.save(PILLOW_OUT)
50 print('pillow:', time.time()-start, "s")
33def _resize_pil(img, size, interpolation):
34 C = img.shape[0]
35 H, W = size
36 out = np.empty((C, H, W), dtype=img.dtype)
37 for ch, out_ch in zip(img, out):
38 ch = PIL.Image.fromarray(ch, mode='F')
39 out_ch[:] = ch.resize((W, H), resample=interpolation)
40 return out
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)
29def pilresize(img, size, interpolation=Image.BILINEAR):
30
31 """Resize the input PIL Image to the given size.
32 Args:
33 img (PIL Image): Image to be resized.
34 size (sequence or int): Desired output size. If size is a sequence like
35 (h, w), the output size will be matched to this. If size is an int,
36 the smaller edge of the image will be matched to this number maintaing
37 the aspect ratio. i.e, if height > width, then image will be rescaled to
38 (size * height / width, size)
39 interpolation (int, optional): Desired interpolation. Default is
40 ``PIL.Image.BILINEAR``
41 Returns:
42 PIL Image: Resized image.
43 """
44
45 if not (isinstance(size, int) or (isinstance(size, collections.Iterable) and len(size) == 2)):
46 raise TypeError('Got inappropriate size arg: {}'.format(size))
47
48 if isinstance(size, int):
49 w, h = img.size
50 if (w <= h and w == size) or (h <= w and h == size):
51 return img
52 if w < h:
53 ow = size
54 oh = int(size * float(h) / w)
55 return img.resize((ow, oh), interpolation)
56 else:
57 oh = size
58 ow = int(size * float(w) / h)
59 return img.resize((ow, oh), interpolation)
60 else:
61 return img.resize(size[::-1], interpolation)
28@curry
29def resize(pil_img, size, interpolation=Image.BILINEAR):
30 return pil_img.resize(size, interpolation)
103def __resize_image(self, image):
104 self.input_height = image.shape[0]
105 if self.height is not None:
106 zoom = self.height / float(image.shape[0])
107 width = int(float(image.shape[1]) * zoom) + 1
108 image = cv2.resize(image, (width, self.height), interpolation=cv2.INTER_CUBIC)
109 return image

Related snippets