图像处理混合模式

blend-modes的Python项目详细描述


这个python包实现了图像的混合模式。

说明

混合模式包可以混合不同的图像或图像 层,通过混合模式。这些模式常见于 像Adobe PhotoshopGIMP

通过混合模式混合允许以多种方式混合图像。 此包当前支持以下混合模式( 包中的各个函数位于italics):

  • 柔和灯光(blend_modes.soft_light
  • 仅变亮(blend_modes.lighten_only
  • 闪避(blend_modes.dodge
  • 加法(blend_modes.addition
  • 仅变暗(blend_modes.darken_only
  • 相乘(blend_modes.multiply
  • 强光(blend_modes.hard_light
  • 差异(blend_modes.difference
  • 减去(blend_modes.subtract
  • 谷物提取物(已知来自gimp,blend_modes.grain_extract
  • 颗粒合并(从gimp得知,blend_modes.grain_merge
  • 除(blend_modes.divide
  • 覆盖(blend_modes.overlay
  • 正常(blend_modes.normal

混合的强度可以通过opacity 传递到函数中的参数。参见Usage了解 更多信息。

混合模式包的速度优化。它利用了 通过numpy进行矢量化。当 在cython中实现包。然而,cython实现是 不是这个包裹的一部分。

用法

混合模式函数将表示为数组的图像数据作为 输入。这些图像数据通常是通过 图像处理包。两个流行的图像处理软件包 Python是PIL或它的叉子 PillowOpenCV。本章中的例子说明了 使用这些包混合图像。

输入和输出格式

典型的混合模式操作如下所示:

blended_img=soft_light(bg_img,fg_img,opacity)

混合模式功能期望 Numpy中的浮点数组 将维度1中的[像素设置为 输入。两个图像的大小必须相同,因此 对于bg_imgfg_img,维度1必须相同。相同 适用于维度2中的像素。因此,一个有效的 数组应该是bg_img.shape == (640,320,4)并且 fg_img.shape == (640,320,4)

三维通道的顺序应该是r,g,b,a, 其中a是alpha通道。所有值都应该是floats 范围0.0<;=值<;=255.0

混合模式函数返回与输入格式相同的数组 格式。

示例

下面的示例演示如何在 典型应用。

示例分为三部分:

  1. 加载背景和前景图像。前景图像是 混合到背景图像上。
  2. 使用混合模式包通过“软 轻“混合模式。包支持多种混合模式。见 Description获取完整列表。
  3. 显示混合图像。

枕头示例

下面的示例演示如何将混合模式包与 PILPillow包。

fromPILimportImageimportnumpyfromblend_modesimportsoft_light# Import background imagebackground_img_raw=Image.open('background.png')# RGBA imagebackground_img=numpy.array(background_img_raw)# Inputs to blend_modes need to be numpy arrays.background_img_float=background_img.astype(float)# Inputs to blend_modes need to be floats.# Import foreground imageforeground_img_raw=Image.open('foreground.png')# RGBA imageforeground_img=numpy.array(foreground_img_raw)# Inputs to blend_modes need to be numpy arrays.foreground_img_float=foreground_img.astype(float)# Inputs to blend_modes need to be floats.# Blend imagesopacity=0.7# The opacity of the foreground that is blended onto the background is 70 %.blended_img_float=soft_light(background_img_float,foreground_img_float,opacity)# Convert blended image back into PIL imageblended_img=numpy.uint8(blended_img_float)# Image needs to be converted back to uint8 type for PIL handling.blended_img_raw=Image.fromarray(blended_img)# Note that alpha channels are displayed in black by PIL by default.# This behavior is difficult to change (although possible).# If you have alpha channels in your images, then you should give# OpenCV a try.# Display blended imageblended_img_raw.show()

opencv示例

下面的示例演示如何使用混合模式包 OpenCV

importcv2# import OpenCVimportnumpyfromblend_modesimportsoft_light# Import background imagebackground_img_float=cv2.imread('background.png',-1).astype(float)# Import foreground imageforeground_img_float=cv2.imread('foreground.png',-1).astype(float)# Blend imagesopacity=0.7# The opacity of the foreground that is blended onto the background is 70 %.blended_img_float=soft_light(background_img_float,foreground_img_float,opacity)# Display blended imageblended_img_uint8=blended_img_float.astype(numpy.uint8)# Convert image to OpenCV native display formatcv2.imshow('window',blended_img_uint8)cv2.waitKey()# Press a key to close window with the image.

安装

混合模式包可以通过PIP安装: $ pip install blend_modes

依赖关系

混合模式包需要 Numpy才能正常工作。 为了加载图像,已成功使用以下软件包:

另请参见

混合模式在 Wikipedia。实际的 实现可以在GIMP source code中找到,例如。 在描述division操作的文件中, gimpoperationdividecode.c

贡献

我对任何贡献或反馈都很满意。请告诉我 您通过上的“问题”选项卡发表的评论 GitHub

许可证

混合模式包在MIT License (MIT)下分发。 还请注意依赖项的许可证。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java setContextClassLoader含义   大家好,我是一名编程初学者,我的项目中有一个问题   泛型类中泛型方法和非泛型类中泛型方法的java差异行为   java我需要帮助找出我做错了什么   使用JPA/Toplink的java批插入   jodatime DateTime出现java性能问题。与*()   java函数名对AWS Lambda函数意味着什么   java如何从SurfaceView正确更新Android UI   java当线程中运行的任务完成时,线程和任务会发生什么?   java在Spring AMQP中SimpleMessageListenerContainer和DirectMessageListenerContainer有什么区别?   java应用程序在几个小时后停止刷新?   与C++不同的java函数匹配规则   java如何确定最大稳定位图尺寸   java如何创建没有主键的Spring实体和存储库   java Play框架无法编译我的类,在堆栈跟踪中显示了奇怪的字符   在Java中使用Printf创建数字表   jvm获取运行Java应用程序时使用的所有类的列表?   java为什么我的安卓应用程序找不到XML文件中声明的文本视图或按钮?   身份验证如何在Java中为JAAS设置用户和密码?