10 examples of 'scale and convert images using pil help' in Python

Every line of 'scale and convert images using pil help' 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
51def scale_image(image, scale_ratio):
52 # type: (Image.Image, float) -> Image.Image
53 if scale_ratio == 1:
54 return image
55
56 image_ratio = float(image.height) / float(image.width)
57 scale_width = int(math.ceil(image.width * scale_ratio))
58 scale_height = int(math.ceil(scale_width * image_ratio))
59 image = image.convert('RGBA')
60 scaled_image = image.resize((scale_width, scale_height), resample=Image.BICUBIC)
61 return scaled_image
48def scale_image(self, image, scale):
49
50 if self.settings.opencv_or_pil == 'PIL':
51 ow, oh = image.size
52 nw = ow * scale
53 nh = oh * scale
54 return image.resize((int(nw), int(nh)), Image.ANTIALIAS)
55
56 else:
57 oh, ow, channels = image.shape
58
59 nw = ow * scale
60 nh = oh * scale
61
62 # PERF return cv2.resize(image, (int(nw), int(nh)), interpolation=cv2.INTER_NEAREST)
63 return cv2.resize(image, (int(nw), int(nh)), interpolation=cv2.INTER_CUBIC)
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)
160@staticmethod
161def scale_image(img, max_width=0, max_height=0, keep_ratio=True, ratio=1):
162 """
163 缩放图像
164 :param numpy.ndarray img:
165 :param int max_width:
166 :param int max_height:
167 :param bool keep_ratio:
168 :param int ratio:
169 :return int:
170 """
171 if ratio != 1:
172 height, width, _ = img.shape
173 return ratio, cv2.resize(img, (int(width / ratio), int(height / ratio)))
174 elif keep_ratio and max_width > 0 and max_height > 0:
175 height, width, _ = img.shape
176 width_ratio = width / max_width
177 height_ratio = height / max_height
178 ratio = max(width_ratio, height_ratio)
179 return ratio, cv2.resize(img, (int(width / ratio), int(height / ratio)))
180 else:
181 return 0, cv2.resize(img, (max_width, max_height))
63def Quick_Scale_To_im(path=None, im=None, source_dpi=600, target_dpi=150,
64 scale=None):
65
66 if im is None:
67
68 try:
69
70 im = load_image_to_numpy(path, dtype=np.uint8)
71
72 except:
73
74 _logger.error("Could not open source")
75
76 return -1
77
78 if scale is None:
79 scale = target_dpi / float(source_dpi)
80
81 small_im = zoom(im, scale, order=1)
82
83 return small_im
174def rescale_images(images, scale=8):
175 num_iter = len(images)
176 dim = np.array(list(images[0].shape[:2]))
177 new_dims = tuple(dim*scale + [3])
178 resized_images = []
179 for i in range(num_iter):
180 resized = cv2.resize(images[i], new_dims, interpolation = cv2.INTER_AREA)
181 resized_images.append(resized)
182 return resized_images
33def cli_convert_image (self, img_bgr, img_landmarks, debug):
34 #img_landmarks not None, if input image is png with embedded data
35 #return float32 image
36 #if debug , return tuple ( images of any size and channels, ...)
37 return image
61def _colorspace(self, im, bw=False, replace_alpha=False):
62 """
63 A utility method taken from SmileyChris' easy-thumbnails that a lot of
64 processors will probably want to use.
65
66 Convert images to the correct color space.
67
68 bw
69 Make the thumbnail grayscale (not really just black & white).
70
71 replace_alpha
72 Replace any transparency layer with a solid color. For example,
73 ``replace_alpha='#fff'`` would replace the transparency layer with
74 white.
75
76 """
77 if bw and im.mode != 'L':
78 return im.convert('L')
79
80 if im.mode in ('L', 'RGB'):
81 return im
82
83 if im.mode == 'RGBA' or (im.mode == 'P' and 'transparency' in im.info):
84 if im.mode != 'RGBA':
85 im = im.convert('RGBA')
86 if not replace_alpha:
87 return im
88 base = Image.new('RGBA', im.size, replace_alpha)
89 base.paste(im)
90 im = base
91
92 return im.convert('RGB')
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
21def engine_scale(self, image, width, height):
22 image.resize(width, height)
23 return image

Related snippets