本文整理匯總了Python中PIL.Image.BICUBIC屬性的典型用法代碼示例。如果您正苦於以下問題:Python Image.BICUBIC屬性的具體用法?Python Image.BICUBIC怎麽用?Python Image.BICUBIC使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類PIL.Image
的用法示例。
在下文中一共展示了Image.BICUBIC屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: resolve
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [as 別名]
def resolve(ctx):
from PIL import Image
if isinstance(ctx, list):
ctx = [ctx[0]]
net.load_parameters('superres.params', ctx=ctx)
img = Image.open(opt.resolve_img).convert('YCbCr')
y, cb, cr = img.split()
data = mx.nd.expand_dims(mx.nd.expand_dims(mx.nd.array(y), axis=0), axis=0)
out_img_y = mx.nd.reshape(net(data), shape=(-3, -2)).asnumpy()
out_img_y = out_img_y.clip(0, 255)
out_img_y = Image.fromarray(np.uint8(out_img_y[0]), mode='L')
out_img_cb = cb.resize(out_img_y.size, Image.BICUBIC)
out_img_cr = cr.resize(out_img_y.size, Image.BICUBIC)
out_img = Image.merge('YCbCr', [out_img_y, out_img_cb, out_img_cr]).convert('RGB')
out_img.save('resolved.png')
示例2: load_img
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [as 別名]
def load_img(filepath, scale):
list=os.listdir(filepath)
list.sort()
rate = 1
#for vimeo90k-setuplet (multiple temporal scale)
#if random.random() < 0.5:
# rate = 2
index = randrange(0, len(list)-(2*rate))
target = [modcrop(Image.open(filepath+'/'+list[i]).convert('RGB'), scale) for i in range(index, index+3*rate, rate)]
h,w = target[0].size
h_in,w_in = int(h//scale), int(w//scale)
target_l = target[1].resize((h_in,w_in), Image.BICUBIC)
input = [target[j].resize((h_in,w_in), Image.BICUBIC) for j in [0,2]]
return input, target, target_l, list
示例3: main
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [as 別名]
def main():
# location of depth module, config and parameters
module_fn = 'models/depth.py'
config_fn = 'models/depth.conf'#網絡結構
params_dir = 'weights/depth'#網絡相關參數
# load depth network
machine = net.create_machine(module_fn, config_fn, params_dir)
# demo image
rgb = Image.open('demo_nyud_rgb.jpg')
rgb = rgb.resize((320, 240), Image.BICUBIC)
# build depth inference function and run
rgb_imgs = np.asarray(rgb).reshape((1, 240, 320, 3))
pred_depths = machine.infer_depth(rgb_imgs)
# save prediction
(m, M) = (pred_depths.min(), pred_depths.max())
depth_img_np = (pred_depths[0] - m) / (M - m)
depth_img = Image.fromarray((255*depth_img_np).astype(np.uint8))
depth_img.save('demo_nyud_depth_prediction.png')
示例4: _load_and_resize
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [as 別名]
def _load_and_resize(self, input_image_path):
"""Load an image from the specified path and resize it to the input resolution.
Return the input image before resizing as a PIL Image (required for visualization),
and the resized image as a NumPy float array.
Keyword arguments:
input_image_path -- string path of the image to be loaded
"""
image_raw = Image.open(input_image_path)
# Expecting yolo_input_resolution in (height, width) format, adjusting to PIL
# convention (width, height) in PIL:
new_resolution = (
self.yolo_input_resolution[1],
self.yolo_input_resolution[0])
image_resized = image_raw.resize(
new_resolution, resample=Image.BICUBIC)
image_resized = np.array(image_resized, dtype=np.float32, order='C')
return image_raw, image_resized
示例5: save_image
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [as 別名]
def save_image(image_numpy, image_path, aspect_ratio=1.0):
"""Save a numpy image to the disk
Parameters:
image_numpy (numpy array) -- input numpy array
image_path (str) -- the path of the image
"""
image_pil = Image.fromarray(image_numpy)
h, w, _ = image_numpy.shape
if aspect_ratio > 1.0:
image_pil = image_pil.resize((h, int(w * aspect_ratio)), Image.BICUBIC)
if aspect_ratio < 1.0:
image_pil = image_pil.resize((int(h / aspect_ratio), w), Image.BICUBIC)
image_pil.save(image_path)
示例6: pixelize_screenshot
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [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')
示例7: process_images
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [as 別名]
def process_images(self, clean, mask):
i, j, h, w = RandomResizedCrop.get_params(clean, scale=(0.5, 2.0), ratio=(3. / 4., 4. / 3.))
clean_img = resized_crop(clean, i, j, h, w, size=self.img_size, interpolation=Image.BICUBIC)
mask = resized_crop(mask, i, j, h, w, self.img_size, interpolation=Image.BICUBIC)
# get mask before further image augment
# mask = self.get_mask(raw_img, clean_img)
if self.add_random_masks:
mask = random_masks(mask.copy(), size=self.img_size[0], offset=10)
mask = np.where(np.array(mask) > brightness_difference * 255, np.uint8(255), np.uint8(0))
mask = cv2.dilate(mask, np.ones((10, 10), np.uint8), iterations=1)
mask = np.expand_dims(mask, -1)
mask_t = to_tensor(mask)
# mask_t = (mask_t > brightness_difference).float()
# mask_t, _ = torch.max(mask_t, dim=0, keepdim=True)
binary_mask = (1 - mask_t) # valid positions are 1; holes are 0
binary_mask = binary_mask.expand(3, -1, -1)
clean_img = self.transformer(clean_img)
corrupted_img = clean_img * binary_mask
return corrupted_img, binary_mask, clean_img
示例8: resize_pad_tensor
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [as 別名]
def resize_pad_tensor(self, pil_img):
origin = to_tensor(pil_img).unsqueeze(0)
fix_len = self.resize
long = max(pil_img.size)
ratio = fix_len / long
new_size = tuple(map(lambda x: int(x * ratio) // 8 * 8, pil_img.size))
img = pil_img.resize(new_size, Image.BICUBIC)
# img = pil_img
img = self.transformer(img).unsqueeze(0)
_, _, h, w = img.size()
if fix_len > w:
boarder_pad = (0, fix_len - w, 0, 0)
else:
boarder_pad = (0, 0, 0, fix_len - h)
img = pad(img, boarder_pad, value=0)
mask_resizer = self.resize_mask(boarder_pad, pil_img.size)
return img, origin, mask_resizer
示例9: get_transform
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [as 別名]
def get_transform(opt):
transform_list = []
if opt.resize_or_crop == 'resize_and_crop':
osize = [opt.loadSize, opt.loadSize]
transform_list.append(transforms.Scale(osize, Image.BICUBIC))
transform_list.append(transforms.RandomCrop(opt.fineSize))
elif opt.resize_or_crop == 'crop':
transform_list.append(transforms.RandomCrop(opt.fineSize))
elif opt.resize_or_crop == 'scale_width':
transform_list.append(transforms.Lambda(
lambda img: __scale_width(img, opt.fineSize)))
elif opt.resize_or_crop == 'scale_width_and_crop':
transform_list.append(transforms.Lambda(
lambda img: __scale_width(img, opt.loadSize)))
transform_list.append(transforms.RandomCrop(opt.fineSize))
if opt.isTrain and not opt.no_flip:
transform_list.append(transforms.RandomHorizontalFlip())
transform_list += [transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))]
return transforms.Compose(transform_list)
示例10: optimize_image
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [as 別名]
def optimize_image(image_path, output_quality, base_width):
''' Optimizes image and returns a filepath string '''
img = Image.open(image_path)
# Check that it's a supported format
format = str(img.format)
if format == 'PNG' or format == 'JPEG':
if base_width < img.size[0]:
wpercent = (base_width/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((base_width,hsize), Image.BICUBIC)
# The 'quality' option is ignored for PNG files
img.save(image_path, quality=output_quality, optimize=True)
return image_path
#==============================================================================
示例11: image_flow_resize
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [as 別名]
def image_flow_resize(img1, img2, flow, short_size=None, long_size=None):
assert (short_size is None) ^ (long_size is None)
w, h = img1.width, img1.height
if short_size is not None:
if w < h:
neww = short_size
newh = int(short_size / float(w) * h)
else:
neww = int(short_size / float(h) * w)
newh = short_size
else:
if w < h:
neww = int(long_size / float(h) * w)
newh = long_size
else:
neww = long_size
newh = int(long_size / float(w) * h)
img1 = img1.resize((neww, newh), Image.BICUBIC)
img2 = img2.resize((neww, newh), Image.BICUBIC)
ratio = float(newh) / h
flow = cv2.resize(flow.copy(), (neww, newh), interpolation=cv2.INTER_LINEAR) * ratio
return img1, img2, flow, ratio
示例12: image_resize
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [as 別名]
def image_resize(img, short_size=None, long_size=None):
assert (short_size is None) ^ (long_size is None)
w, h = img.width, img.height
if short_size is not None:
if w < h:
neww = short_size
newh = int(short_size / float(w) * h)
else:
neww = int(short_size / float(h) * w)
newh = short_size
else:
if w < h:
neww = int(long_size / float(h) * w)
newh = long_size
else:
neww = long_size
newh = int(long_size / float(w) * h)
img = img.resize((neww, newh), Image.BICUBIC)
return img, [w, h]
示例13: regenerate_cache
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [as 別名]
def regenerate_cache(self):
"""
Resamples the big matrix and resets the counter of the total
number of elements in the returned masks.
"""
low_size = int(self.resolution * self.max_size)
low_pattern = self.rng.uniform(0, 1, size=(low_size, low_size)) * 255
low_pattern = torch.from_numpy(low_pattern.astype('float32'))
pattern = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(self.max_size, Image.BICUBIC),
transforms.ToTensor(),
])(low_pattern[None])[0]
pattern = torch.lt(pattern, self.density).byte()
self.pattern = pattern.byte()
self.points_used = 0
示例14: perform_inference
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [as 別名]
def perform_inference(sym, arg_params, aux_params, input_img, img_cb, img_cr):
"""Perform inference on image using mxnet"""
metadata = onnx_mxnet.get_model_metadata('super_resolution.onnx')
data_names = [input_name[0] for input_name in metadata.get('input_tensor_data')]
# create module
mod = mx.mod.Module(symbol=sym, data_names=data_names, label_names=None)
mod.bind(for_training=False, data_shapes=[(data_names[0], input_img.shape)])
mod.set_params(arg_params=arg_params, aux_params=aux_params)
# run inference
batch = namedtuple('Batch', ['data'])
mod.forward(batch([mx.nd.array(input_img)]))
# Save the result
img_out_y = Image.fromarray(np.uint8(mod.get_outputs()[0][0][0].
asnumpy().clip(0, 255)), mode='L')
result_img = Image.merge(
"YCbCr", [img_out_y,
img_cb.resize(img_out_y.size, Image.BICUBIC),
img_cr.resize(img_out_y.size, Image.BICUBIC)]).convert("RGB")
output_img_dim = 672
assert result_img.size == (output_img_dim, output_img_dim)
LOGGER.info("Super Resolution example success.")
result_img.save("super_res_output.jpg")
return result_img
示例15: resize_image
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import BICUBIC [as 別名]
def resize_image(target_image_path, target_size):
"""
調整圖片大小,缺失的部分用黑色填充
:param target_image_path: 圖片路徑
:param target_size: 分辨率大小
:return:
"""
image = Image.open(target_image_path)
iw, ih = image.size # 原始圖像的尺寸
w, h = target_size # 目標圖像的尺寸
scale = min(w / iw, h / ih) # 轉換的最小比例
# 保證長或寬,至少一個符合目標圖像的尺寸
nw = int(iw * scale)
nh = int(ih * scale)
image = image.resize((nw, nh), Image.BICUBIC) # 縮小圖像
# image.show()
new_image = Image.new('RGB', target_size, (0, 0, 0, 0)) # 生成黑色圖像
# // 為整數除法,計算圖像的位置
new_image.paste(image, ((w - nw) // 2, (h - nh) // 2)) # 將圖像填充為中間圖像,兩側為灰色的樣式
# new_image.show()
# 覆蓋原圖片
new_image.save(target_image_path)