Python源码示例:PIL.ImageEnhance.Color()
示例1
def adjust_saturation(img, saturation_factor):
"""Adjust color saturation of an image.
Args:
img (PIL Image): PIL Image to be adjusted.
saturation_factor (float): How much to adjust the saturation. 0 will
give a black and white image, 1 will give the original image while
2 will enhance the saturation by a factor of 2.
Returns:
PIL Image: Saturation adjusted image.
"""
if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(saturation_factor)
return img
示例2
def adjust_saturation(img, saturation_factor):
"""Adjust color saturation of an image.
Args:
img (numpy ndarray): numpy ndarray to be adjusted.
saturation_factor (float): How much to adjust the saturation. 0 will
give a black and white image, 1 will give the original image while
2 will enhance the saturation by a factor of 2.
Returns:
numpy ndarray: Saturation adjusted image.
"""
# ~10ms slower than PIL!
if not _is_numpy_image(img):
raise TypeError('img should be numpy Image. Got {}'.format(type(img)))
img = Image.fromarray(img)
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(saturation_factor)
return np.array(img)
示例3
def adjust_saturation(img, saturation_factor):
"""Adjust color saturation of an image.
Args:
img (PIL Image): PIL Image to be adjusted.
saturation_factor (float): How much to adjust the saturation. 0 will
give a black and white image, 1 will give the original image while
2 will enhance the saturation by a factor of 2.
Returns:
PIL Image: Saturation adjusted image.
"""
if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(saturation_factor)
return img
示例4
def adjust_saturation(img, saturation_factor):
"""Adjust color saturation of an image.
Args:
img (PIL Image): PIL Image to be adjusted.
saturation_factor (float): How much to adjust the saturation. 0 will
give a black and white image, 1 will give the original image while
2 will enhance the saturation by a factor of 2.
Returns:
PIL Image: Saturation adjusted image.
"""
if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(saturation_factor)
return img
示例5
def resize_and_bitmap(self, fname, size, enhance_color=False):
"""Take filename of an image and resize and center crop it to size."""
try:
pil = resize_to_fill(Image.open(fname), size, quality="fast")
except UnidentifiedImageError:
msg = ("Opening image '%s' failed with PIL.UnidentifiedImageError."
"It could be corrupted or is of foreign type.") % fname
sp_logging.G_LOGGER.info(msg)
# show_message_dialog(msg)
black_bmp = wx.Bitmap.FromRGBA(size[0], size[1], red=0, green=0, blue=0, alpha=255)
if enhance_color:
return (black_bmp, black_bmp)
return black_bmp
img = wx.Image(pil.size[0], pil.size[1])
img.SetData(pil.convert("RGB").tobytes())
if enhance_color:
converter = ImageEnhance.Color(pil)
pilenh_bw = converter.enhance(0.25)
brightns = ImageEnhance.Brightness(pilenh_bw)
pilenh = brightns.enhance(0.45)
imgenh = wx.Image(pil.size[0], pil.size[1])
imgenh.SetData(pilenh.convert("RGB").tobytes())
return (img.ConvertToBitmap(), imgenh.ConvertToBitmap())
return img.ConvertToBitmap()
示例6
def adjust_saturation(img, saturation_factor):
"""Adjust color saturation of an image.
Args:
img (PIL Image): PIL Image to be adjusted.
saturation_factor (float): How much to adjust the saturation. 0 will
give a black and white image, 1 will give the original image while
2 will enhance the saturation by a factor of 2.
Returns:
PIL Image: Saturation adjusted image.
"""
if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(saturation_factor)
return img
示例7
def image_color(self, factor: int, extension: str = "png"):
"""Change image color
Args:
factor (int): Factor to increase the color by
extension (str, optional): File extension of loaded image. Defaults to "png"
Returns:
Chepy: The Chepy object.
"""
image = Image.open(self._load_as_file())
image = self._force_rgb(image)
fh = io.BytesIO()
enhanced = ImageEnhance.Color(image).enhance(factor)
enhanced.save(fh, extension)
self.state = fh.getvalue()
return self
示例8
def modifyImageBscc(imageData, brightness, sharpness, contrast, color):
"""Update with brightness, sharpness, contrast and color."""
brightnessMod = ImageEnhance.Brightness(imageData)
imageData = brightnessMod.enhance(brightness)
sharpnessMod = ImageEnhance.Sharpness(imageData)
imageData = sharpnessMod.enhance(sharpness)
contrastMod = ImageEnhance.Contrast(imageData)
imageData = contrastMod.enhance(contrast)
colorMod = ImageEnhance.Color(imageData)
imageData = colorMod.enhance(color)
return imageData
示例9
def lomoize (image,darkness,saturation):
(width,height) = image.size
max = width
if height > width:
max = height
mask = Image.open("./lomolive/lomomask.jpg").resize((max,max))
left = round((max - width) / 2)
upper = round((max - height) / 2)
mask = mask.crop((left,upper,left+width,upper + height))
# mask = Image.open('mask_l.png')
darker = ImageEnhance.Brightness(image).enhance(darkness)
saturated = ImageEnhance.Color(image).enhance(saturation)
lomoized = Image.composite(saturated,darker,mask)
return lomoized
示例10
def __getitem__(self, index):
im, xpatch, ypatch, rotation, flip, enhance = np.unravel_index(index, self.shape)
with Image.open(self.names[im]) as img:
extractor = PatchExtractor(img=img, patch_size=PATCH_SIZE, stride=self.stride)
patch = extractor.extract_patch((xpatch, ypatch))
if rotation != 0:
patch = patch.rotate(rotation * 90)
if flip != 0:
patch = patch.transpose(Image.FLIP_LEFT_RIGHT)
if enhance != 0:
factors = np.random.uniform(.5, 1.5, 3)
patch = ImageEnhance.Color(patch).enhance(factors[0])
patch = ImageEnhance.Contrast(patch).enhance(factors[1])
patch = ImageEnhance.Brightness(patch).enhance(factors[2])
label = self.labels[self.names[im]]
return transforms.ToTensor()(patch), label
示例11
def adjust_saturation(img, saturation_factor):
"""Adjust color saturation of an image.
Args:
img (PIL Image): PIL Image to be adjusted.
saturation_factor (float): How much to adjust the saturation. 0 will
give a black and white image, 1 will give the original image while
2 will enhance the saturation by a factor of 2.
Returns:
PIL Image: Saturation adjusted image.
"""
if not is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(saturation_factor)
return img
示例12
def adjust_saturation(img, saturation_factor):
"""Adjust color saturation of an image.
Args:
img (PIL Image): PIL Image to be adjusted.
saturation_factor (float): How much to adjust the saturation. 0 will
give a black and white image, 1 will give the original image while
2 will enhance the saturation by a factor of 2.
Returns:
PIL Image: Saturation adjusted image.
"""
if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(saturation_factor)
return img
示例13
def adjust_saturation(img, saturation_factor):
"""Adjust color saturation of an image.
Args:
img (PIL Image): PIL Image to be adjusted.
saturation_factor (float): How much to adjust the saturation. 0 will
give a black and white image, 1 will give the original image while
2 will enhance the saturation by a factor of 2.
Returns:
PIL Image: Saturation adjusted image.
"""
if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(saturation_factor)
return img
示例14
def adjust_saturation(img, saturation_factor):
"""Adjust color saturation of an image.
Args:
img (PIL Image): PIL Image to be adjusted.
saturation_factor (float): How much to adjust the saturation. 0 will
give a black and white image, 1 will give the original image while
2 will enhance the saturation by a factor of 2.
Returns:
PIL Image: Saturation adjusted image.
"""
if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(saturation_factor)
return img
示例15
def save_img(fname, image, image_enhance=False): # 图像可以增强
image = Image.fromarray(image)
if image_enhance:
# 亮度增强
enh_bri = ImageEnhance.Brightness(image)
brightness = 1.2
image = enh_bri.enhance(brightness)
# 色度增强
enh_col = ImageEnhance.Color(image)
color = 1.2
image = enh_col.enhance(color)
# 锐度增强
enh_sha = ImageEnhance.Sharpness(image)
sharpness = 1.2
image = enh_sha.enhance(sharpness)
imsave(fname, image)
return
示例16
def adjust_saturation(img, saturation_factor):
"""Adjust color saturation of an image.
Args:
img (PIL.Image): PIL Image to be adjusted.
saturation_factor (float): How much to adjust the saturation. 0 will
give a black and white image, 1 will give the original image while
2 will enhance the saturation by a factor of 2.
Returns:
PIL.Image: Saturation adjusted image.
"""
if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(saturation_factor)
return img
示例17
def adjust_saturation(img, saturation_factor):
"""Adjust color saturation of an image.
Args:
img (PIL Image): PIL Image to be adjusted.
saturation_factor (float): How much to adjust the saturation. 0 will
give a black and white image, 1 will give the original image while
2 will enhance the saturation by a factor of 2.
Returns:
PIL Image: Saturation adjusted image.
"""
if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(saturation_factor)
return img
示例18
def adjust_saturation(img, saturation_factor):
"""Adjust color saturation of an image.
Args:
img (PIL Image): PIL Image to be adjusted.
saturation_factor (float): How much to adjust the saturation. 0 will
give a black and white image, 1 will give the original image while
2 will enhance the saturation by a factor of 2.
Returns:
PIL Image: Saturation adjusted image.
"""
if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(saturation_factor)
return img
示例19
def __call__(self, img):
"""
Args:
img (numpy.ndarray (H x W x C)): Input image.
Returns:
img (numpy.ndarray (H x W x C)): Color jittered image.
"""
if not(_is_numpy_image(img)):
raise TypeError('img should be ndarray. Got {}'.format(type(img)))
pil = Image.fromarray(img)
transform = self.get_params(self.brightness, self.contrast,
self.saturation, self.hue)
return np.array(transform(pil))
示例20
def __init__(self):
self.policies = [
['Invert', 0.1, 7, 'Contrast', 0.2, 6],
['Rotate', 0.7, 2, 'TranslateX', 0.3, 9],
['Sharpness', 0.8, 1, 'Sharpness', 0.9, 3],
['ShearY', 0.5, 8, 'TranslateY', 0.7, 9],
['AutoContrast', 0.5, 8, 'Equalize', 0.9, 2],
['ShearY', 0.2, 7, 'Posterize', 0.3, 7],
['Color', 0.4, 3, 'Brightness', 0.6, 7],
['Sharpness', 0.3, 9, 'Brightness', 0.7, 9],
['Equalize', 0.6, 5, 'Equalize', 0.5, 1],
['Contrast', 0.6, 7, 'Sharpness', 0.6, 5],
['Color', 0.7, 7, 'TranslateX', 0.5, 8],
['Equalize', 0.3, 7, 'AutoContrast', 0.4, 8],
['TranslateY', 0.4, 3, 'Sharpness', 0.2, 6],
['Brightness', 0.9, 6, 'Color', 0.2, 8],
['Solarize', 0.5, 2, 'Invert', 0, 0.3],
['Equalize', 0.2, 0, 'AutoContrast', 0.6, 0],
['Equalize', 0.2, 8, 'Equalize', 0.6, 4],
['Color', 0.9, 9, 'Equalize', 0.6, 6],
['AutoContrast', 0.8, 4, 'Solarize', 0.2, 8],
['Brightness', 0.1, 3, 'Color', 0.7, 0],
['Solarize', 0.4, 5, 'AutoContrast', 0.9, 3],
['TranslateY', 0.9, 9, 'TranslateY', 0.7, 9],
['AutoContrast', 0.9, 2, 'Solarize', 0.8, 3],
['Equalize', 0.8, 8, 'Invert', 0.1, 3],
['TranslateY', 0.7, 9, 'AutoContrast', 0.9, 1],
]
示例21
def color(img, magnitude):
magnitudes = np.linspace(0.1, 1.9, 11)
img = ImageEnhance.Color(img).enhance(random.uniform(magnitudes[magnitude], magnitudes[magnitude+1]))
return img
示例22
def __call__(self, image, label):
#aug blur
if random.random() > set_ratio:
select = random.random()
if select < 0.3:
kernalsize = random.choice([3, 5])
image = cv2.GaussianBlur(image, (kernalsize, kernalsize), 0)
elif select < 0.6:
kernalsize = random.choice([3, 5])
image = cv2.medianBlur(image, kernalsize)
else:
kernalsize = random.choice([3, 5])
image = cv2.blur(image, (kernalsize, kernalsize))
# aug noise
if random.random() > set_ratio:
mu = 0
sigma = random.random() * 10.0
image = np.array(image, dtype=np.float32)
image += np.random.normal(mu, sigma, image.shape)
image[image > 255] = 255
image[image < 0] = 0
# aug_color
if random.random() > set_ratio:
random_factor = np.random.randint(4, 17) / 10.
color_image = ImageEnhance.Color(image).enhance(random_factor)
random_factor = np.random.randint(4, 17) / 10.
brightness_image = ImageEnhance.Brightness(color_image).enhance(random_factor)
random_factor = np.random.randint(6, 15) / 10.
contrast_image = ImageEnhance.Contrast(brightness_image).enhance(random_factor)
random_factor = np.random.randint(8, 13) / 10.
image = ImageEnhance.Sharpness(contrast_image).enhance(random_factor)
return np.array(image), label
示例23
def __call__(self, image, label):
if random.random() < set_ratio:
return image, label
image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
random_factor = np.random.randint(4, 17) / 10.
color_image = ImageEnhance.Color(image).enhance(random_factor)
random_factor = np.random.randint(4, 17) / 10.
brightness_image = ImageEnhance.Brightness(color_image).enhance(random_factor)
random_factor = np.random.randint(6, 15) / 10.
contrast_image = ImageEnhance.Contrast(brightness_image).enhance(random_factor)
random_factor = np.random.randint(8, 13) / 10.
image = ImageEnhance.Sharpness(contrast_image).enhance(random_factor)
return np.uint8(np.array(image)[:,:,::-1]), label
示例24
def __init__(self):
self.policies = [
['Invert', 0.1, 7, 'Contrast', 0.2, 6],
['Rotate', 0.7, 2, 'TranslateX', 0.3, 9],
['Sharpness', 0.8, 1, 'Sharpness', 0.9, 3],
['ShearY', 0.5, 8, 'TranslateY', 0.7, 9],
['AutoContrast', 0.5, 8, 'Equalize', 0.9, 2],
['ShearY', 0.2, 7, 'Posterize', 0.3, 7],
['Color', 0.4, 3, 'Brightness', 0.6, 7],
['Sharpness', 0.3, 9, 'Brightness', 0.7, 9],
['Equalize', 0.6, 5, 'Equalize', 0.5, 1],
['Contrast', 0.6, 7, 'Sharpness', 0.6, 5],
['Color', 0.7, 7, 'TranslateX', 0.5, 8],
['Equalize', 0.3, 7, 'AutoContrast', 0.4, 8],
['TranslateY', 0.4, 3, 'Sharpness', 0.2, 6],
['Brightness', 0.9, 6, 'Color', 0.2, 8],
['Solarize', 0.5, 2, 'Invert', 0, 0.3],
['Equalize', 0.2, 0, 'AutoContrast', 0.6, 0],
['Equalize', 0.2, 8, 'Equalize', 0.6, 4],
['Color', 0.9, 9, 'Equalize', 0.6, 6],
['AutoContrast', 0.8, 4, 'Solarize', 0.2, 8],
['Brightness', 0.1, 3, 'Color', 0.7, 0],
['Solarize', 0.4, 5, 'AutoContrast', 0.9, 3],
['TranslateY', 0.9, 9, 'TranslateY', 0.7, 9],
['AutoContrast', 0.9, 2, 'Solarize', 0.8, 3],
['Equalize', 0.8, 8, 'Invert', 0.1, 3],
['TranslateY', 0.7, 9, 'AutoContrast', 0.9, 1],
]
示例25
def color(img, magnitude):
magnitudes = np.linspace(0.1, 1.9, 11)
img = ImageEnhance.Color(img).enhance(random.uniform(magnitudes[magnitude], magnitudes[magnitude+1]))
return img
示例26
def random_perturbation(imgs):
for i in range(imgs.shape[0]):
im=Image.fromarray(imgs[i,...].astype(np.uint8))
en=ImageEnhance.Brightness(im)
im=en.enhance(random.uniform(0.8,1.2))
en=ImageEnhance.Color(im)
im=en.enhance(random.uniform(0.8,1.2))
en=ImageEnhance.Contrast(im)
im=en.enhance(random.uniform(0.8,1.2))
imgs[i,...]= np.asarray(im).astype(np.float32)
return imgs
示例27
def __call__(self, im_lb):
im = im_lb['im']
lb = im_lb['lb']
r_brightness = random.uniform(self.brightness[0], self.brightness[1])
r_contrast = random.uniform(self.contrast[0], self.contrast[1])
r_saturation = random.uniform(self.saturation[0], self.saturation[1])
im = ImageEnhance.Brightness(im).enhance(r_brightness)
im = ImageEnhance.Contrast(im).enhance(r_contrast)
im = ImageEnhance.Color(im).enhance(r_saturation)
return dict(im = im,
lb = lb,
)
示例28
def print_thumbnail(im,width=64):
W,H = im.size
x_step = int(float(W)/width)
y_step = x_step * 2
converter = ImageEnhance.Color(im)
im = converter.enhance(2)
return print_image(im,x_step=x_step,y_step=y_step,calc_average=False)
# get bounding box of html element on screen
示例29
def __call__(self, image, **kwargs):
factor = random.uniform(self.min_value, self.max_value)
img = Image.fromarray(np.uint8(image))
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(factor)
image = np.array(img)
return dict({'image': image}, **kwargs)
示例30
def __call__(self, image, **kwargs):
factor = random.uniform(self.min_value, self.max_value)
img = Image.fromarray(np.uint8(image))
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(factor)
image = np.array(img)
return dict({'image': image}, **kwargs)