简体   繁体   English

如何使用 Python 图像库 (PIL) 突出显示部分图像?

[英]How to highlight part of image with Python Imaging Library (PIL)?

How can I highlight part of image?如何突出显示图像的一部分? (Location defined as tuple of 4 numbers). (位置定义为 4 个数字的元组)。 You can imagine it like I have image of pc motherboard, and I need to highlight for example part where CPU Socket is located.你可以想象它就像我有 pc 主板的图像,我需要突出显示 CPU 插槽所在的部分。

Note that for Python 3, you need to use the pillow fork of PIL, which is a mostly backwards compatible fork of the original module but, unlike it, is currently actively being maintained.请注意,对于 Python 3,您需要使用 PIL 的枕头叉,这是原始模块的大部分向后兼容的叉,但与它不同的是,目前正在积极维护中。

Here's some sample code that shows how to do it using the PIL.ImageEnhance.Brightness class.下面是一些示例代码,展示了如何使用PIL.ImageEnhance.Brightness class 进行操作。

Doing what you want requires multiple steps:做你想做的事需要多个步骤:

  • The portion to be highlighted is cut out of — or cropped from — the image.要突出显示的部分从图像中剪掉或裁剪。
  • An instance of the Brightness class is created from this cropped image. Brightness class 的一个实例是从此裁剪图像创建的。
  • The cropped image is the lightened by calling the enhance() method of the Brightness instance.通过调用Brightness实例的 enhance enhance()方法使裁剪后的图像变亮。
  • The cropped and now lightened image is pasted back into the location it came from.裁剪后变亮的图像被粘贴回它的来源位置。

To make doing them all easier to repeat, below is a function named highlight_area() to perform them.为了使它们更容易重复,下面是一个名为highlight_area()的 function 来执行它们。 Note that I've also added a bonus feature that will optionally outline the highlighted region with a colored border — which you can of course remove if you don't need or want it.请注意,我还添加了一个额外功能,可以选择用彩色边框勾勒突出显示的区域 - 如果您不需要或不想要它,您当然可以将其删除。

from PIL import Image, ImageColor, ImageDraw, ImageEnhance


def highlight_area(img, region, factor, outline_color=None, outline_width=1):
    """ Highlight specified rectangular region of image by `factor` with an
        optional colored  boarder drawn around its edges and return the result.
    """
    img = img.copy()  # Avoid changing original image.
    img_crop = img.crop(region)

    brightner = ImageEnhance.Brightness(img_crop)
    img_crop = brightner.enhance(factor)

    img.paste(img_crop, region)

    # Optionally draw a colored outline around the edge of the rectangular region.
    if outline_color:
        draw = ImageDraw.Draw(img)  # Create a drawing context.
        left, upper, right, lower = region  # Get bounds.
        coords = [(left, upper), (right, upper), (right, lower), (left, lower),
                  (left, upper)]
        draw.line(coords, fill=outline_color, width=outline_width)

    return img


if __name__ == '__main__':

    img = Image.open('motherboard.jpg')

    red = ImageColor.getrgb('red')
    cpu_socket_region = 110, 67, 274, 295
    img2 = highlight_area(img, cpu_socket_region, 2.5, outline_color=red, outline_width=2)

    img2.save('motherboard_with_cpu_socket_highlighted.jpg')
    img2.show()  # Display the result.

Here's an example of using the function.这是使用 function 的示例。 The original image is shown on the left opposite the one resulting from calling the function on it with the values shown in the sample code.原始图像显示在左侧,与在其上调用 function 产生的图像相对,其值显示在示例代码中。

高亮前后

Honestly, thanks for people like you who make some fancy code w some fancy functions that make our life easier... Not only because the code is provided and is neat, but the code itself helps someone understand whats being done.老实说,感谢像你这样的人,他们编写了一些花哨的代码和一些花哨的功能,让我们的生活更轻松......不仅因为代码提供且整洁,而且代码本身可以帮助人们理解正在做什么。 bless u fam祝福你的家人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM