本文整理匯總了Python中PIL.ImageOps.expand方法的典型用法代碼示例。如果您正苦於以下問題:Python ImageOps.expand方法的具體用法?Python ImageOps.expand怎麽用?Python ImageOps.expand使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類PIL.ImageOps
的用法示例。
在下文中一共展示了ImageOps.expand方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __call__
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [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))
示例2: __call__
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [as 別名]
def __call__(self, img):
"""
Args:
img (PIL.Image): Image to be cropped.
Returns:
PIL.Image: Cropped image.
"""
if self.padding > 0:
img = ImageOps.expand(img, border=self.padding, fill=0)
w, h = img.size
th, tw = self.size
if w == tw and h == th:
return img
x1 = random.randint(0, w - tw)
y1 = random.randint(0, h - th)
return img.crop((x1, y1, x1 + tw, y1 + th))
示例3: add_timestamp
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [as 別名]
def add_timestamp(image, timestamp, margin=2):
"""
Return an image object with timestamp bar added at the bottom.
param image: pillow image object
param timestamp: timestamp in seconds since the Epoch
param margin: timestamp margin, default is 2
"""
width, height = image.size
font = ImageFont.load_default()
watermark = time.strftime('%c', time.localtime(timestamp))
# bar height = text height + top margin + bottom margin
bar_height = font.getsize(watermark)[1] + 2 * margin
# place bar at the bottom
new_image = ImageOps.expand(image, border=(0, 0, 0, bar_height),
fill='lightgrey')
draw = ImageDraw.Draw(new_image)
# place timestamp at the left side of the bar
x, y = margin, height + margin
draw.text((x, y), watermark, font=font, fill='black')
return new_image
示例4: image_flow_crop
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [as 別名]
def image_flow_crop(img1, img2, flow, crop_size, phase):
assert len(crop_size) == 2
pad_h = max(crop_size[0] - img1.height, 0)
pad_w = max(crop_size[1] - img1.width, 0)
pad_h_half = int(pad_h / 2)
pad_w_half = int(pad_w / 2)
if pad_h > 0 or pad_w > 0:
flow_expand = np.zeros((img1.height + pad_h, img1.width + pad_w, 2), dtype=np.float32)
flow_expand[pad_h_half:pad_h_half+img1.height, pad_w_half:pad_w_half+img1.width, :] = flow
flow = flow_expand
border = (pad_w_half, pad_h_half, pad_w - pad_w_half, pad_h - pad_h_half)
img1 = ImageOps.expand(img1, border=border, fill=(0,0,0))
img2 = ImageOps.expand(img2, border=border, fill=(0,0,0))
if phase == 'train':
hoff = int(np.random.rand() * (img1.height - crop_size[0]))
woff = int(np.random.rand() * (img1.width - crop_size[1]))
else:
hoff = (img1.height - crop_size[0]) // 2
woff = (img1.width - crop_size[1]) // 2
img1 = img1.crop((woff, hoff, woff+crop_size[1], hoff+crop_size[0]))
img2 = img2.crop((woff, hoff, woff+crop_size[1], hoff+crop_size[0]))
flow = flow[hoff:hoff+crop_size[0], woff:woff+crop_size[1], :]
offset = (hoff, woff)
return img1, img2, flow, offset
示例5: __call__
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [as 別名]
def __call__(self, img, labelmap=None, maskmap=None):
assert isinstance(img, Image.Image)
assert labelmap is None or isinstance(labelmap, Image.Image)
assert maskmap is None or isinstance(maskmap, Image.Image)
if random.random() > self.ratio:
return img, labelmap, maskmap
width, height = img.size
left_pad, up_pad, right_pad, down_pad = self.pad
target_size = [width + left_pad + right_pad, height + up_pad + down_pad]
offset_left = -left_pad
offset_up = -up_pad
img = ImageOps.expand(img, border=tuple(self.pad), fill=tuple(self.mean))
if maskmap is not None:
maskmap = ImageOps.expand(maskmap, border=tuple(self.pad), fill=1)
if labelmap is not None:
labelmap = ImageOps.expand(labelmap, border=tuple(self.pad), fill=255)
return img, labelmap, maskmap
示例6: __call__
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [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)),
)
示例7: rotate
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [as 別名]
def rotate(img, angle, resample=False, expand=False, center=None):
"""Rotate the image by angle and then (optionally) translate it by (n_columns, n_rows)
Args:
img (PIL Image): PIL Image to be rotated.
angle ({float, int}): In degrees degrees counter clockwise order.
resample ({PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC}, optional):
An optional resampling filter.
See http://pillow.readthedocs.io/en/3.4.x/handbook/concepts.html#filters
If omitted, or if the image has mode "1" or "P", it is set to PIL.Image.NEAREST.
expand (bool, optional): Optional expansion flag.
If true, expands the output image to make it large enough to hold the entire rotated image.
If false or omitted, make the output image the same size as the input image.
Note that the expand flag assumes rotation around the center and no translation.
center (2-tuple, optional): Optional center of rotation.
Origin is the upper left corner.
Default is the center of the image.
"""
if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
return img.rotate(angle, resample, expand, center)
示例8: __call__
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [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))
示例9: pad_to_target
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [as 別名]
def pad_to_target(img, target_height, target_width, label=0):
# Pad image with zeros to the specified height and width if needed
# This op does nothing if the image already has size bigger than target_height and target_width.
w, h = img.size
left = top = right = bottom = 0
doit = False
if target_width > w:
delta = target_width - w
left = delta // 2
right = delta - left
doit = True
if target_height > h:
delta = target_height - h
top = delta // 2
bottom = delta - top
doit = True
if doit:
img = ImageOps.expand(img, border=(left, top, right, bottom), fill=label)
assert img.size[0] >= target_width
assert img.size[1] >= target_height
return img
示例10: __call__
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [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))
示例11: __call__
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [as 別名]
def __call__(self, img):
"""
Args:
img (PIL.Image): Image to be cropped.
Returns:
PIL.Image: Cropped image.
"""
if self.padding > 0:
img = ImageOps.expand(img, border=self.padding, fill=0)
w, h = img.size
th, tw = self.size
if w == tw and h == th:
return img
if w < tw or h < th:
return img.resize((tw, th), Image.BILINEAR)
x1 = random.randint(0, w - tw)
y1 = random.randint(0, h - th)
return img.crop((x1, y1, x1 + tw, y1 + th))
示例12: __call__
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [as 別名]
def __call__(self, img, mask, ins, depth):
if self.padding > 0:
img = ImageOps.expand(img, border=self.padding, fill=0)
mask = ImageOps.expand(mask, border=self.padding, fill=0)
ins = ImageOps.expand(ins, border=self.padding, fill=0)
depth = ImageOps.expand(depth, border=self.padding, fill=0)
assert img.size == mask.size
assert img.size == ins.size
assert img.size == depth.size
w, h = img.size
th, tw = self.size
if w == tw and h == th:
return img, mask, ins, depth
if w < tw or h < th:
return img.resize((tw, th), Image.BILINEAR), mask.resize((tw, th), Image.NEAREST), ins.resize((tw,th), Image.NEAREST), depth.resize((tw, th), Image.NEAREST)
_sysrand = random.SystemRandom()
x1 = _sysrand.randint(0, w - tw)
y1 = _sysrand.randint(0, h - th)
return img.crop((x1, y1, x1 + tw, y1 + th)), mask.crop((x1, y1, x1 + tw, y1 + th)), ins.crop((x1, y1, x1 + tw, y1 + th)), depth.crop((x1, y1, x1 + tw, y1 + th))
示例13: __call__
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [as 別名]
def __call__(self, inp):
img = F.grab_img(inp)
padl = padt = 0
if self.padding > 0:
if F.is_pil_image(img):
img = ImageOps.expand(img, border=self.padding, fill=0)
else:
assert isinstance(img, F.DummyImg)
img = img.expand(border=self.padding)
if isinstance(self.padding, int):
padl = padt = self.padding
else:
padl, padt = self.padding[0:2]
i, j, tw, th = self.get_params(img, self.size)
img = img.crop((i, j, i+tw, j+th))
return F.update_img_and_labels(inp, img, aff=(1,0,padl-i,0,1,padt-j))
示例14: __call__
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [as 別名]
def __call__(self, sample):
"""call method"""
image, label = sample['image'], sample['label']
width, height = image.size
pad_width, pad_height = max(width, self.crop_width), max(height, self.crop_height)
pad_width = self.crop_width - width if width < self.crop_width else 0
pad_height = self.crop_height - height if height < self.crop_height else 0
# pad the image with constant
image = ImageOps.expand(image, border=(0, 0, pad_width, pad_height), fill=self.mean)
label = ImageOps.expand(label, border=(0, 0, pad_width, pad_height), fill=self.ignore_label)
# random crop image to crop_size
new_w, new_h = image.size
x1 = random.randint(0, new_w - self.crop_width)
y1 = random.randint(0, new_h - self.crop_height)
image = image.crop((x1, y1, x1 + self.crop_width, y1 + self.crop_height))
label = label.crop((x1, y1, x1 + self.crop_width, y1 + self.crop_height))
return {'image': image,
'label': label}
示例15: __call__
# 需要導入模塊: from PIL import ImageOps [as 別名]
# 或者: from PIL.ImageOps import expand [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
tw, th = 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)),
)