本文整理汇总了Python中PIL.ImageOps.colorize方法的典型用法代码示例。如果您正苦于以下问题:Python ImageOps.colorize方法的具体用法?Python ImageOps.colorize怎么用?Python ImageOps.colorize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PIL.ImageOps
的用法示例。
在下文中一共展示了ImageOps.colorize方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: colorize_pil_image
# 需要导入模块: from PIL import ImageOps [as 别名]
# 或者: from PIL.ImageOps import colorize [as 别名]
def colorize_pil_image(pil_image, color, bg_color=None):
"""Convert a picto in white to the corresponding color.
:param pil_image: PIL image to be colorized
:type pil_image: :py:class:`PIL.Image`
:param color: RGB color to convert the picto
:type color: tuple
:param bg_color: RGB color to use for the picto's background
:type bg_color: tuple
"""
if not bg_color:
bg_color = (abs(color[0] - 255), abs(color[1] - 255), abs(color[2] - 255))
_, _, _, alpha = pil_image.split()
gray_pil_image = pil_image.convert('L')
new_pil_image = ImageOps.colorize(gray_pil_image, black=bg_color, white=color)
new_pil_image.putalpha(alpha)
return new_pil_image
示例2: annotate_streets
# 需要导入模块: from PIL import ImageOps [as 别名]
# 或者: from PIL.ImageOps import colorize [as 别名]
def annotate_streets(df, img, text_col):
# confirm font file location
if not os.path.exists(FONT_PATH):
print('Error loading default font. Check your FONT_PATH')
return None
unique_sts = df[text_col].unique()
for street in unique_sts:
draw_coords = df.loc[df.ST_NAME == street, 'draw_coords'].tolist()[0]
coords = df.loc[df.ST_NAME == street, 'coords'].tolist()[0]
font = ImageFont.truetype(FONT_PATH, int(25))
imgTxt = Image.new('L', font.getsize(street))
drawTxt = ImageDraw.Draw(imgTxt)
drawTxt.text((0, 0), street, font=font, fill=(10, 10, 12))
angle = angle_bw_points(coords[0], coords[1])
texrot = imgTxt.rotate(angle, expand=1)
mpt = midpoint(draw_coords[0], draw_coords[1])
img.paste(ImageOps.colorize(texrot, (0, 0, 0), (10, 10, 12)), mpt, texrot)
示例3: test_colorize_2color
# 需要导入模块: from PIL import ImageOps [as 别名]
# 或者: from PIL.ImageOps import colorize [as 别名]
def test_colorize_2color(self):
# Test the colorizing function with 2-color functionality
# Open test image (256px by 10px, black to white)
im = Image.open("Tests/images/bw_gradient.png")
im = im.convert("L")
# Create image with original 2-color functionality
im_test = ImageOps.colorize(im, 'red', 'green')
# Test output image (2-color)
left = (0, 1)
middle = (127, 1)
right = (255, 1)
self.assert_tuple_approx_equal(im_test.getpixel(left),
(255, 0, 0),
threshold=1,
msg='black test pixel incorrect')
self.assert_tuple_approx_equal(im_test.getpixel(middle),
(127, 63, 0),
threshold=1,
msg='mid test pixel incorrect')
self.assert_tuple_approx_equal(im_test.getpixel(right),
(0, 127, 0),
threshold=1,
msg='white test pixel incorrect')
示例4: test_colorize_2color_offset
# 需要导入模块: from PIL import ImageOps [as 别名]
# 或者: from PIL.ImageOps import colorize [as 别名]
def test_colorize_2color_offset(self):
# Test the colorizing function with 2-color functionality and offset
# Open test image (256px by 10px, black to white)
im = Image.open("Tests/images/bw_gradient.png")
im = im.convert("L")
# Create image with original 2-color functionality with offsets
im_test = ImageOps.colorize(im,
black='red',
white='green',
blackpoint=50,
whitepoint=100)
# Test output image (2-color) with offsets
left = (25, 1)
middle = (75, 1)
right = (125, 1)
self.assert_tuple_approx_equal(im_test.getpixel(left),
(255, 0, 0),
threshold=1,
msg='black test pixel incorrect')
self.assert_tuple_approx_equal(im_test.getpixel(middle),
(127, 63, 0),
threshold=1,
msg='mid test pixel incorrect')
self.assert_tuple_approx_equal(im_test.getpixel(right),
(0, 127, 0),
threshold=1,
msg='white test pixel incorrect')
示例5: random_texture
# 需要导入模块: from PIL import ImageOps [as 别名]
# 或者: from PIL.ImageOps import colorize [as 别名]
def random_texture():
files = list(iglob('resources/textures/*.png'))
file = files[np.random.randint(0, len(files))]
texture = Image.open(file).convert('L')
texture = ImageOps.colorize(
texture,
'black',
(np.random.randint(50, 256), np.random.randint(50, 256), np.random.randint(50, 256))
)
return texture
示例6: apply_color
# 需要导入模块: from PIL import ImageOps [as 别名]
# 或者: from PIL.ImageOps import colorize [as 别名]
def apply_color(image: ImageType, color: Tuple[int, int, int]) -> ImageType:
# [r, g, b, a][3] -> a
alpha = image.split()[3]
colored = ImageOps.colorize(ImageOps.grayscale(image), white=color, black="black")
colored.putalpha(alpha)
return colored
示例7: deepfry
# 需要导入模块: from PIL import ImageOps [as 别名]
# 或者: from PIL.ImageOps import colorize [as 别名]
def deepfry(img: Image) -> Image:
colours = (
(randint(50, 200), randint(40, 170), randint(40, 190)),
(randint(190, 255), randint(170, 240), randint(180, 250))
)
img = img.copy().convert("RGB")
# Crush image to hell and back
img = img.convert("RGB")
width, height = img.width, img.height
img = img.resize((int(width ** uniform(0.8, 0.9)), int(height ** uniform(0.8, 0.9))), resample=Image.LANCZOS)
img = img.resize((int(width ** uniform(0.85, 0.95)), int(height ** uniform(0.85, 0.95))), resample=Image.BILINEAR)
img = img.resize((int(width ** uniform(0.89, 0.98)), int(height ** uniform(0.89, 0.98))), resample=Image.BICUBIC)
img = img.resize((width, height), resample=Image.BICUBIC)
img = ImageOps.posterize(img, randint(3, 7))
# Generate colour overlay
overlay = img.split()[0]
overlay = ImageEnhance.Contrast(overlay).enhance(uniform(1.0, 2.0))
overlay = ImageEnhance.Brightness(overlay).enhance(uniform(1.0, 2.0))
overlay = ImageOps.colorize(overlay, colours[0], colours[1])
# Overlay red and yellow onto main image and sharpen the hell out of it
img = Image.blend(img, overlay, uniform(0.1, 0.4))
img = ImageEnhance.Sharpness(img).enhance(randint(5, 300))
return img
示例8: sample_filter
# 需要导入模块: from PIL import ImageOps [as 别名]
# 或者: from PIL.ImageOps import colorize [as 别名]
def sample_filter(im):
'''
A simple filter to be applied to the image
'''
black = "#000099"
white= "#99CCFF"
filter_image = ImageOps.colorize(ImageOps.grayscale(im), black, white)
return filter_image
示例9: image_recolorize
# 需要导入模块: from PIL import ImageOps [as 别名]
# 或者: from PIL.ImageOps import colorize [as 别名]
def image_recolorize(src, black="#000099", white="#99CCFF"):
# img = image_recolorize(img, black="#000000", white="#FFFFFF")
"""
Returns a recolorized version of the initial image using a two-tone
approach. The color in the black argument is used to replace black pixels
and the color in the white argument is used to replace white pixels.
The defaults set the image to a blue hued image.
"""
return ImageOps.colorize(ImageOps.grayscale(src), black, white)
示例10: test_sanity
# 需要导入模块: from PIL import ImageOps [as 别名]
# 或者: from PIL.ImageOps import colorize [as 别名]
def test_sanity(self):
ImageOps.autocontrast(hopper("L"))
ImageOps.autocontrast(hopper("RGB"))
ImageOps.autocontrast(hopper("L"), cutoff=10)
ImageOps.autocontrast(hopper("L"), ignore=[0, 255])
ImageOps.colorize(hopper("L"), (0, 0, 0), (255, 255, 255))
ImageOps.colorize(hopper("L"), "black", "white")
ImageOps.pad(hopper("L"), (128, 128))
ImageOps.pad(hopper("RGB"), (128, 128))
ImageOps.crop(hopper("L"), 1)
ImageOps.crop(hopper("RGB"), 1)
ImageOps.deform(hopper("L"), self.deformer)
ImageOps.deform(hopper("RGB"), self.deformer)
ImageOps.equalize(hopper("L"))
ImageOps.equalize(hopper("RGB"))
ImageOps.expand(hopper("L"), 1)
ImageOps.expand(hopper("RGB"), 1)
ImageOps.expand(hopper("L"), 2, "blue")
ImageOps.expand(hopper("RGB"), 2, "blue")
ImageOps.fit(hopper("L"), (128, 128))
ImageOps.fit(hopper("RGB"), (128, 128))
ImageOps.flip(hopper("L"))
ImageOps.flip(hopper("RGB"))
ImageOps.grayscale(hopper("L"))
ImageOps.grayscale(hopper("RGB"))
ImageOps.invert(hopper("L"))
ImageOps.invert(hopper("RGB"))
ImageOps.mirror(hopper("L"))
ImageOps.mirror(hopper("RGB"))
ImageOps.posterize(hopper("L"), 4)
ImageOps.posterize(hopper("RGB"), 4)
ImageOps.solarize(hopper("L"))
ImageOps.solarize(hopper("RGB"))
ImageOps.exif_transpose(hopper("L"))
ImageOps.exif_transpose(hopper("RGB"))
示例11: test_colorize_3color_offset
# 需要导入模块: from PIL import ImageOps [as 别名]
# 或者: from PIL.ImageOps import colorize [as 别名]
def test_colorize_3color_offset(self):
# Test the colorizing function with 3-color functionality and offset
# Open test image (256px by 10px, black to white)
im = Image.open("Tests/images/bw_gradient.png")
im = im.convert("L")
# Create image with new three color functionality with offsets
im_test = ImageOps.colorize(im,
black='red',
white='green',
mid='blue',
blackpoint=50,
whitepoint=200,
midpoint=100)
# Test output image (3-color) with offsets
left = (25, 1)
left_middle = (75, 1)
middle = (100, 1)
right_middle = (150, 1)
right = (225, 1)
self.assert_tuple_approx_equal(im_test.getpixel(left),
(255, 0, 0),
threshold=1,
msg='black test pixel incorrect')
self.assert_tuple_approx_equal(im_test.getpixel(left_middle),
(127, 0, 127),
threshold=1,
msg='low-mid test pixel incorrect')
self.assert_tuple_approx_equal(im_test.getpixel(middle),
(0, 0, 255),
threshold=1,
msg='mid incorrect')
self.assert_tuple_approx_equal(im_test.getpixel(right_middle),
(0, 63, 127),
threshold=1,
msg='high-mid test pixel incorrect')
self.assert_tuple_approx_equal(im_test.getpixel(right),
(0, 127, 0),
threshold=1,
msg='white test pixel incorrect')