本文整理匯總了Python中PIL.ImageFilter.GaussianBlur方法的典型用法代碼示例。如果您正苦於以下問題:Python ImageFilter.GaussianBlur方法的具體用法?Python ImageFilter.GaussianBlur怎麽用?Python ImageFilter.GaussianBlur使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類PIL.ImageFilter
的用法示例。
在下文中一共展示了ImageFilter.GaussianBlur方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_blur_accuracy
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def test_blur_accuracy(self):
i = snakes.filter(ImageFilter.GaussianBlur(.4))
# These pixels surrounded with pixels with 255 intensity.
# They must be very close to 255.
for x, y, c in [(1, 0, 1), (2, 0, 1), (7, 8, 1), (8, 8, 1), (2, 9, 1),
(7, 3, 0), (8, 3, 0), (5, 8, 0), (5, 9, 0), (1, 3, 0),
(4, 3, 2), (4, 2, 2)]:
self.assertGreaterEqual(i.im.getpixel((x, y))[c], 250)
# Fuzzy match.
def gp(x, y):
return i.im.getpixel((x, y))
self.assertTrue(236 <= gp(7, 4)[0] <= 239)
self.assertTrue(236 <= gp(7, 5)[2] <= 239)
self.assertTrue(236 <= gp(7, 6)[2] <= 239)
self.assertTrue(236 <= gp(7, 7)[1] <= 239)
self.assertTrue(236 <= gp(8, 4)[0] <= 239)
self.assertTrue(236 <= gp(8, 5)[2] <= 239)
self.assertTrue(236 <= gp(8, 6)[2] <= 239)
self.assertTrue(236 <= gp(8, 7)[1] <= 239)
示例2: blur
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def blur(self, source, radius=20):
filename = '{}{}.png'.format(utils.md5hash(source), radius)
destination = self.save_path + filename
try:
if xbmcvfs.exists(destination):
os.utime(destination, None)
else:
img = _openimage(source, self.save_path, filename)
img.thumbnail((256, 256))
img = img.convert('RGB')
img = img.filter(ImageFilter.GaussianBlur(radius))
img.save(destination)
img.close()
return destination
except Exception:
return ''
示例3: gaussian_blur_transform
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def gaussian_blur_transform(AdvSample, radius, oriDataset):
if oriDataset.upper() == 'CIFAR10':
assert AdvSample.shape == (3, 32, 32)
sample = np.transpose(np.round(AdvSample * 255), (1, 2, 0))
image = Image.fromarray(np.uint8(sample))
gb_image = image.filter(ImageFilter.GaussianBlur(radius=radius))
gb_image = np.transpose(np.array(gb_image), (2, 0, 1)).astype('float32') / 255.0
return gb_image
if oriDataset.upper() == 'MNIST':
assert AdvSample.shape == (1, 28, 28)
sample = np.transpose(np.round(AdvSample * 255), (1, 2, 0))
# for MNIST, there is no RGB
sample = np.squeeze(sample, axis=2)
image = Image.fromarray(np.uint8(sample))
gb_image = image.filter(ImageFilter.GaussianBlur(radius=radius))
gb_image = np.expand_dims(np.array(gb_image).astype('float32'), axis=0) / 255.0
return gb_image
# help function for the image compression transformation of images
示例4: __getitem__
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def __getitem__(self, index):
path, target = self.imgs[index]
img = self.loader(path)
img_size = torch.LongTensor(img.size)
# img = img.filter(ImageFilter.GaussianBlur(7))
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
output = [img, target]
if self.out_name:
output.append(os.path.split(path)[1])
if self.out_image_size:
output.append(img_size)
return tuple(output)
示例5: blur_img
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def blur_img(fp, filename):
try:
img = Image.open(fp)
if img.mode == 'P':
img = img.convert('RGB')
except OSError as e:
log.warning(e)
return {}
img = img.filter(ImageFilter.GaussianBlur(radius=25))
buf = io.BytesIO()
ext = os.path.splitext(filename)[1].lower()
img.save(buf, Image.EXTENSION.get(ext, 'jpeg'))
buf.seek(0)
return {'fp': buf,
'id': 'blur'}
示例6: get_verify_code
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def get_verify_code():
'''生成驗證碼圖形'''
code = gene_text()
# 圖片大小120×50
width, height = 120, 50
# 新圖片對象
im = Image.new('RGB',(width, height),'white')
# 字體
font = ImageFont.truetype('app/static/arial.ttf', 40)
# draw對象
draw = ImageDraw.Draw(im)
# 繪製字符串
for item in range(4):
draw.text((5+random.randint(-3,3)+23*item, 5+random.randint(-3,3)),
text=code[item], fill=rndColor(),font=font )
# 劃線
draw_lines(draw, 2, width, height)
# 高斯模糊
im = im.filter(ImageFilter.GaussianBlur(radius=1.5))
return im, code
# 進貨表格
# 進貨量
示例7: __call__
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def __call__(self, sample):
img = sample['image']
mask = sample['label']
if random.random() < 0.5:
img = img.filter(ImageFilter.GaussianBlur(
radius=random.random()))
return {'image': img,
'label': mask}
示例8: NbyNGradient
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def NbyNGradient(side):
base_color = "#00ffff"
img = Image.new("RGB", (side,side), base_color)
draw = ImageDraw.Draw(img)
n_boxes = 5
boxes_size = side//n_boxes
xmin, xmax = 0, boxes_size
ymin, ymax = 0, boxes_size
for i in range(n_boxes):
for j in range(n_boxes):
r, g, b = [randint(0, 255),randint(0, 255), randint(0, 255)]
dr = (randint(0, 255) - r)/boxes_size
dg = (randint(0, 255) - g)/boxes_size
db = (randint(0, 255) - b)/boxes_size
for k in range(xmin, xmax):
draw.line([k, ymin, k, ymax], fill=(int(r), int(g), int(b)))
r += dr
g += dg
b += db
xmin += boxes_size
xmax += boxes_size
xmin = 0
xmax = boxes_size
ymin += boxes_size
ymax += boxes_size
img = img.filter(ImageFilter.GaussianBlur(radius=boxes_size//n_boxes))
return img
示例9: _sync_transform
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def _sync_transform(self, img, mask):
# random mirror
if random.random() < 0.5:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
mask = mask.transpose(Image.FLIP_LEFT_RIGHT)
crop_size = self.crop_size
# random scale (short edge)
short_size = random.randint(int(self.base_size * 0.5), int(self.base_size * 2.0))
w, h = img.size
if h > w:
ow = short_size
oh = int(1.0 * h * ow / w)
else:
oh = short_size
ow = int(1.0 * w * oh / h)
img = img.resize((ow, oh), Image.BILINEAR)
mask = mask.resize((ow, oh), Image.NEAREST)
# pad crop
if short_size < crop_size:
padh = crop_size - oh if oh < crop_size else 0
padw = crop_size - ow if ow < crop_size else 0
img = ImageOps.expand(img, border=(0, 0, padw, padh), fill=0)
mask = ImageOps.expand(mask, border=(0, 0, padw, padh), fill=0)
# random crop crop_size
w, h = img.size
x1 = random.randint(0, w - crop_size)
y1 = random.randint(0, h - crop_size)
img = img.crop((x1, y1, x1 + crop_size, y1 + crop_size))
mask = mask.crop((x1, y1, x1 + crop_size, y1 + crop_size))
# gaussian blur as in PSP
if random.random() < 0.5:
img = img.filter(ImageFilter.GaussianBlur(
radius=random.random()))
# final transform
img, mask = self._img_transform(img), self._mask_transform(mask)
return img, mask
示例10: _sync_transform
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def _sync_transform(self, img, mask):
# random mirror
if cfg.AUG.MIRROR and random.random() < 0.5:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
mask = mask.transpose(Image.FLIP_LEFT_RIGHT)
crop_size = self.crop_size
# random scale (short edge)
short_size = random.randint(int(self.base_size * 0.5), int(self.base_size * 2.0))
w, h = img.size
if h > w:
ow = short_size
oh = int(1.0 * h * ow / w)
else:
oh = short_size
ow = int(1.0 * w * oh / h)
img = img.resize((ow, oh), Image.BILINEAR)
mask = mask.resize((ow, oh), Image.NEAREST)
# pad crop
if short_size < min(crop_size):
padh = crop_size[0] - oh if oh < crop_size[0] else 0
padw = crop_size[1] - ow if ow < crop_size[1] else 0
img = ImageOps.expand(img, border=(0, 0, padw, padh), fill=0)
mask = ImageOps.expand(mask, border=(0, 0, padw, padh), fill=-1)
# random crop crop_size
w, h = img.size
x1 = random.randint(0, w - crop_size[1])
y1 = random.randint(0, h - crop_size[0])
img = img.crop((x1, y1, x1 + crop_size[1], y1 + crop_size[0]))
mask = mask.crop((x1, y1, x1 + crop_size[1], y1 + crop_size[0]))
# gaussian blur as in PSP
if cfg.AUG.BLUR_PROB > 0 and random.random() < cfg.AUG.BLUR_PROB:
radius = cfg.AUG.BLUR_RADIUS if cfg.AUG.BLUR_RADIUS > 0 else random.random()
img = img.filter(ImageFilter.GaussianBlur(radius=radius))
# color jitter
if self.color_jitter:
img = self.color_jitter(img)
# final transform
img, mask = self._img_transform(img), self._mask_transform(mask)
return img, mask
示例11: findRed
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def findRed(i):
'''Return `clusters`, a list for each red object in `i` that
contains all the pixel coordinates which make up that object'''
i=i.convert('RGB')
width,height=i.size
#Apply gaussian blur
blur = i.filter(ImageFilter.GaussianBlur(radius=max(i.size)/300))
load = i.load()
#Find red regions
redMap = [[0 for _ in range(height)] for _ in range(width)]
for y in range(height):
for x in range(width):
h,s,v=rgb2hsv(load[x,y])
if (h < 20 or h > 330) and s > 100 and v > 100:
redMap[x][y] = 1
#Depth-first search on one pixel in each object to identify separate red objects
visited = []
clusters = []
for y in range(height):
for x in range(width):
if not (x, y) in visited:
cluster = bfs(redMap, (x, y))
if cluster is not None:
clusters.append(cluster)
visited += cluster
#Exclude smaller clusters (accidental red pixels) so that clusters only include objects.
return [c for c in clusters if len(c)>5]
示例12: _train_sync_transform
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def _train_sync_transform(self, img, mask):
'''
:param image: PIL input image
:param gt_image: PIL input gt_image
:return:
'''
# random mirror
if random.random() < 0.5:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
mask = mask.transpose(Image.FLIP_LEFT_RIGHT)
crop_size = self.crop_size
# random scale (short edge)
short_size = random.randint(int(self.base_size * 0.5), int(self.base_size * 2.0))
w, h = img.size
if h > w:
ow = short_size
oh = int(1.0 * h * ow / w)
else:
oh = short_size
ow = int(1.0 * w * oh / h)
img = img.resize((ow, oh), Image.BILINEAR)
mask = mask.resize((ow, oh), Image.NEAREST)
# pad crop
if short_size < crop_size:
padh = crop_size - oh if oh < crop_size else 0
padw = crop_size - ow if ow < crop_size else 0
img = ImageOps.expand(img, border=(0, 0, padw, padh), fill=0)
mask = ImageOps.expand(mask, border=(0, 0, padw, padh), fill=0)
# random crop crop_size
w, h = img.size
x1 = random.randint(0, w - crop_size)
y1 = random.randint(0, h - crop_size)
img = img.crop((x1, y1, x1 + crop_size, y1 + crop_size))
mask = mask.crop((x1, y1, x1 + crop_size, y1 + crop_size))
# gaussian blur as in PSP
if random.random() < 0.5:
img = img.filter(ImageFilter.GaussianBlur(
radius=random.random()))
# final transform
img, mask = self._img_transform(img), self._mask_transform(mask)
return img, mask
示例13: generate_thumb
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def generate_thumb(self, image):
"""
Given a (large) image, generate a 10x10px thumbnail
with blur effect (in order to keep the size small)
"""
image_file = image.file
picture = Image.open(image_file).convert('RGB')
picture.thumbnail((10, 10))
picture.filter(ImageFilter.GaussianBlur(radius=4))
absolute_path = self.build_thumb_path(image)
self.save_thumb(picture, absolute_path)
示例14: _sync_transform
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def _sync_transform(self, img, mask):
# random mirror
if random.random() < 0.5:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
mask = mask.transpose(Image.FLIP_LEFT_RIGHT)
crop_size = self.crop_size
# random scale (short edge)
long_size = random.randint(int(self.base_size*0.5), int(self.base_size*2.0))
w, h = img.size
if h > w:
oh = long_size
ow = int(1.0 * w * long_size / h + 0.5)
short_size = ow
else:
ow = long_size
oh = int(1.0 * h * long_size / w + 0.5)
short_size = oh
img = img.resize((ow, oh), Image.BILINEAR)
mask = mask.resize((ow, oh), Image.NEAREST)
# pad crop
if short_size < crop_size:
padh = crop_size - oh if oh < crop_size else 0
padw = crop_size - ow if ow < crop_size else 0
img = ImageOps.expand(img, border=(0, 0, padw, padh), fill=0)
mask = ImageOps.expand(mask, border=(0, 0, padw, padh), fill=0)
# random crop crop_size
w, h = img.size
x1 = random.randint(0, w - crop_size)
y1 = random.randint(0, h - crop_size)
img = img.crop((x1, y1, x1+crop_size, y1+crop_size))
mask = mask.crop((x1, y1, x1+crop_size, y1+crop_size))
# gaussian blur as in PSP
if random.random() < 0.5:
img = img.filter(ImageFilter.GaussianBlur(
radius=random.random()))
# final transform
img, mask = self._img_transform(img), self._mask_transform(mask)
return img, mask
示例15: _sync_transform
# 需要導入模塊: from PIL import ImageFilter [as 別名]
# 或者: from PIL.ImageFilter import GaussianBlur [as 別名]
def _sync_transform(self, img, mask):
# random mirror
if random.random() < 0.5:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
mask = mask.transpose(Image.FLIP_LEFT_RIGHT)
base_size = 2048
crop_size = 720
# random scale (short edge from 480 to 720)
long_size = random.randint(int(base_size*0.5), int(base_size*2.0))
w, h = img.size
if h > w:
oh = long_size
ow = int(1.0 * w * oh / h)
short_size = ow
else:
ow = long_size
oh = int(1.0 * h * ow / w)
short_size = oh
img = img.resize((ow, oh), Image.BILINEAR)
mask = mask.resize((ow, oh), Image.NEAREST)
# random rotate -10~10, mask using NN rotate
deg = random.uniform(-10,10)
img = img.rotate(deg, resample=Image.BILINEAR)
mask = mask.rotate(deg, resample=Image.NEAREST)
# pad crop
if short_size < crop_size:
padh = crop_size - oh if oh < crop_size else 0
padw = crop_size - ow if ow < crop_size else 0
img = ImageOps.expand(img, border=(0,0,padw,padh), fill=0)
mask = ImageOps.expand(mask, border=(0,0,padw,padh), fill=0)
# random crop 480
w, h = img.size
x1 = random.randint(0, w - crop_size)
y1 = random.randint(0, h - crop_size)
img = img.crop((x1, y1, x1+crop_size, y1+crop_size))
mask = mask.crop((x1, y1, x1+crop_size, y1+crop_size))
# gaussian blur as in PSP ?
if random.random() < 0.5:
img = img.filter(ImageFilter.GaussianBlur(
radius=random.random()))
return img, mask