本文整理汇总了Python中PIL.Image.NEAREST属性的典型用法代码示例。如果您正苦于以下问题:Python Image.NEAREST属性的具体用法?Python Image.NEAREST怎么用?Python Image.NEAREST使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类PIL.Image
的用法示例。
在下文中一共展示了Image.NEAREST属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def __call__(self, sample):
img = sample['image']
mask = sample['label']
assert img.size == mask.size
w, h = img.size
# if one side is 512
if (w >= h and w == self.size[1]) or (h >= w and h == self.size[0]):
return {'image': img,
'label': mask}
# if both sides is not equal to 512, resize to 512 * 512
oh, ow = self.size
img = img.resize((ow, oh), Image.BILINEAR)
mask = mask.resize((ow, oh), Image.NEAREST)
return {'image': img,
'label': mask}
示例2: __call__
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def __call__(self, sample):
img = sample['image']
mask = sample['label']
w, h = img.size
if w > h:
oh = self.crop_size
ow = int(1.0 * w * oh / h)
else:
ow = self.crop_size
oh = int(1.0 * h * ow / w)
img = img.resize((ow, oh), Image.BILINEAR)
mask = mask.resize((ow, oh), Image.NEAREST)
# center crop
w, h = img.size
x1 = int(round((w - self.crop_size) / 2.))
y1 = int(round((h - self.crop_size) / 2.))
img = img.crop((x1, y1, x1 + self.crop_size, y1 + self.crop_size))
mask = mask.crop((x1, y1, x1 + self.crop_size, y1 + self.crop_size))
return {'image': img,
'label': mask}
示例3: preprocess_image
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def preprocess_image(image_path, inp_dims):
ppm_image = Image.open(image_path)
# resize image
new_h = 224
new_w = 224
size = (new_w, new_h)
# resize image
img = ppm_image.resize(size, Image.NEAREST)
# convert to numpy array
img = np.array(img)
# hwc2chw
img = img.transpose(2, 0, 1)
# convert image to 1D array
img = img.ravel()
# convert image to float
img = img.astype(np.float32)
# normalize image data
img = normalize_data(img, inp_dims)
return img
示例4: update_preview
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def update_preview(self, psize):
# Safety check: Ignore calls during construction/destruction.
if not self.init_done: return
# Copy latest user settings to the lens object.
self.lens.fov_deg = self.f.get()
self.lens.radius_px = self.r.get()
self.lens.center_px[0] = self.x.get()
self.lens.center_px[1] = self.y.get()
# Re-scale the image to match the canvas size.
# Note: Make a copy first, because thumbnail() operates in-place.
self.img_sc = self.img.copy()
self.img_sc.thumbnail(psize, Image.NEAREST)
self.img_tk = ImageTk.PhotoImage(self.img_sc)
# Re-scale the x/y/r parameters to match the preview scale.
pre_scale = float(psize[0]) / float(self.img.size[0])
x = self.x.get() * pre_scale
y = self.y.get() * pre_scale
r = self.r.get() * pre_scale
# Clear and redraw the canvas.
self.preview.delete('all')
self.preview.create_image(0, 0, anchor=tk.NW, image=self.img_tk)
self.preview.create_oval(x-r, y-r, x+r, y+r,
outline='#C00000', width=3)
# Make a combined label/textbox/slider for a given variable:
示例5: __call__
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def __call__(self, img, mask):
if self.padding > 0:
img = ImageOps.expand(img, border=self.padding, fill=0)
mask = ImageOps.expand(mask, border=self.padding, fill=0)
assert img.size == mask.size
w, h = img.size
th, tw = self.size
if w == tw and h == th:
return img, mask
if w < tw or h < th:
return img.resize((tw, th), Image.BILINEAR), mask.resize((tw, th), Image.NEAREST)
x1 = random.randint(0, w - tw)
y1 = random.randint(0, h - th)
return img.crop((x1, y1, x1 + tw, y1 + th)), mask.crop((x1, y1, x1 + tw, y1 + th))
示例6: __call__
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def __call__(self, rgb_img, label_img=None):
label1 = label_img
label2 = label_img
if self.scale1 != 1:
w, h = label_img.size
label1 = label1.resize((w//self.scale1, h//self.scale1), Image.NEAREST)
if self.scale2 != 1:
w, h = label_img.size
label2 = label2.resize((w//self.scale2, h//self.scale2), Image.NEAREST)
rgb_img = F.to_tensor(rgb_img) # convert to tensor (values between 0 and 1)
rgb_img = F.normalize(rgb_img, self.mean, self.std) # normalize the tensor
label1 = torch.LongTensor(np.array(label1).astype(np.int64))
label2 = torch.LongTensor(np.array(label2).astype(np.int64))
return rgb_img, label1, label2
示例7: get_transform2
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def get_transform2(dataset_name, net_transform, downscale):
"Returns image and label transform to downscale, crop and prepare for net."
orig_size = get_orig_size(dataset_name)
transform = []
target_transform = []
if downscale is not None:
transform.append(transforms.Resize(orig_size // downscale))
target_transform.append(
transforms.Resize(orig_size // downscale,
interpolation=Image.NEAREST))
transform.extend([transforms.Resize(orig_size), net_transform])
target_transform.extend([transforms.Resize(orig_size, interpolation=Image.NEAREST),
to_tensor_raw])
transform = transforms.Compose(transform)
target_transform = transforms.Compose(target_transform)
return transform, target_transform
示例8: __call__
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def __call__(self, input, target):
# do something to both images and labels
if self.reshape_size is not None:
input = input.resize(self.reshape_size,Image.BILINEAR)
target = target.resize(self.reshape_size,Image.NEAREST)
if self.augment :
input, target = RandomCrop(self.crop_size)(input,target) # RandomCrop for image and label in the same area
input, target = self.flip(input,target) # RandomFlip for both croped image and label
input, target = self.rotate(input,target)
else:
input, target = CenterCrop(self.crop_size)(input, target) # CenterCrop for the validation data
input = ToTensor()(input)
Normalize([.485, .456, .406], [.229, .224, .225])(input) #normalize with the params of imagenet
target = torch.from_numpy(np.array(target)).long().unsqueeze(0)
return input, target
示例9: _val_sync_transform
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def _val_sync_transform(self, img, mask):
outsize = self.crop_size
short_size = outsize
w, h = img.size
if w > h:
oh = short_size
ow = int(1.0 * w * oh / h)
else:
ow = short_size
oh = int(1.0 * h * ow / w)
img = img.resize((ow, oh), Image.BILINEAR)
mask = mask.resize((ow, oh), Image.NEAREST)
# center crop
w, h = img.size
x1 = int(round((w - outsize) / 2.))
y1 = int(round((h - outsize) / 2.))
img = img.crop((x1, y1, x1 + outsize, y1 + outsize))
mask = mask.crop((x1, y1, x1 + outsize, y1 + outsize))
# final transform
img, mask = self._img_transform(img), self._mask_transform(mask)
return img, mask
示例10: _val_sync_transform
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def _val_sync_transform(self, img, mask):
outsize = self.crop_size
short_size = min(outsize)
w, h = img.size
if w > h:
oh = short_size
ow = int(1.0 * w * oh / h)
else:
ow = short_size
oh = int(1.0 * h * ow / w)
img = img.resize((ow, oh), Image.BILINEAR)
mask = mask.resize((ow, oh), Image.NEAREST)
# center crop
w, h = img.size
x1 = int(round((w - outsize[1]) / 2.))
y1 = int(round((h - outsize[0]) / 2.))
img = img.crop((x1, y1, x1 + outsize[1], y1 + outsize[0]))
mask = mask.crop((x1, y1, x1 + outsize[1], y1 + outsize[0]))
# final transform
img, mask = self._img_transform(img), self._mask_transform(mask)
return img, mask
示例11: pixelize_screenshot
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def pixelize_screenshot(screenshot, screenshot_pixelized, target_width=390, pixelsize=3):
"""
Thumbnail a screenshot to `target_width` and pixelize it.
:param screenshot: Screenshot to be thumbnailed in pixelized
:param screenshot_pixelized: File to which the result should be written
:param target_width: Width of the final thumbnail
:param pixelsize: Size of the final pixels
:return: None
"""
if target_width % pixelsize != 0:
raise ValueError("pixelsize must divide target_width")
img = Image.open(screenshot)
width, height = img.size
if height > width:
img = img.crop((0, 0, width, width))
height = width
undersampling_width = target_width // pixelsize
ratio = width / height
new_height = int(undersampling_width / ratio)
img = img.resize((undersampling_width, new_height), Image.BICUBIC)
img = img.resize((target_width, new_height * pixelsize), Image.NEAREST)
img.save(screenshot_pixelized, format='png')
示例12: _val_sync_transform
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def _val_sync_transform(self, img, mask):
outsize = self.crop_size
short_size = outsize
w, h = img.size
if w > h:
oh = short_size
ow = int(1.0 * w * oh / h)
else:
ow = short_size
oh = int(1.0 * h * ow / w)
img = img.resize((ow, oh), Image.BILINEAR)
mask = mask.resize((ow, oh), Image.NEAREST)
# center crop
w, h = img.size
x1 = int(round((w - outsize) / 2.))
y1 = int(round((h - outsize) / 2.))
img = img.crop((x1, y1, x1+outsize, y1+outsize))
mask = mask.crop((x1, y1, x1+outsize, y1+outsize))
# final transform
img, mask = self._img_transform(img), self._mask_transform(mask)
return img, mask
示例13: _val_sync_transform
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def _val_sync_transform(self, img, mask):
outsize = self.crop_size
short_size = outsize
w, h = img.size
if w > h:
oh = short_size
ow = int(1.0 * w * oh / h)
else:
ow = short_size
oh = int(1.0 * h * ow / w)
img = img.resize((ow, oh), Image.BILINEAR)
mask = mask.resize((ow, oh), Image.NEAREST)
# center crop
w, h = img.size
x1 = int(round((w - outsize) / 2.))
y1 = int(round((h - outsize) / 2.))
img = img.crop((x1, y1, x1+outsize, y1+outsize))
mask = mask.crop((x1, y1, x1+outsize, y1+outsize))
# final transform
return img, self._mask_transform(mask)
示例14: _val_sync_transform
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def _val_sync_transform(self, img, mask):
"""
synchronized transformation
"""
outsize = 720
short = outsize
w, h = img.size
if w > h:
oh = short
ow = int(1.0 * w * oh / h)
else:
ow = short
oh = int(1.0 * h * ow / w)
img = img.resize((ow, oh), Image.BILINEAR)
mask = mask.resize((ow, oh), Image.NEAREST)
# center crop
w, h = img.size
x1 = int(round((w - outsize) / 2.))
y1 = int(round((h - outsize) / 2.))
img = img.crop((x1, y1, x1+outsize, y1+outsize))
mask = mask.crop((x1, y1, x1+outsize, y1+outsize))
return img, mask
示例15: write
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import NEAREST [as 别名]
def write(self, s):
global lcd
image = Image.frombuffer('L', P_SIZE, s, "raw", 'L', 0, 1)
image = image.crop((self.x, 0, self.x+1, P_HEIGHT))
self.image_scan.paste(image,(self.x, 0))
if self.x < P_WIDTH-1:
self.x += 1
image = ImageOps.invert(self.image_scan)
image.thumbnail(S_SIZE, Image.NEAREST)
image = image.convert('1')
lcd.write(image.tobytes())