图像处理混合模式

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 Intellij新项目JDK不可用   JAVA静态块执行了多少次?   java通过JCheckBox更改文本字段输入的字体颜色   java在星期几文本字段中输入值   java RichFaces 4.2日历。如何通过JavaScript设置日历的输入字段?   java在javafx中注册鼠标处理程序,但处理程序不是内联的   java将jchararray分配给常量unsigned char   在安卓4.4中注意到java奇怪的圆形浮动动作按钮吗?   java中用于解决8个难题的合适的树类是什么   为什么Java在我的Linux服务器上使用了如此多的内存?   java从多个推荐列表中提取顶级推荐   “426写入网络流失败。”尝试使用Java的URL类连接到FTP站点时收到   java如何在没有Gradle的情况下使用IntelliJ打开项目?   java如何在线程完成后使For循环继续?   java使用定制jackson ObjectMapper定制Spring引导序列化过程   java NativeActivity不会显示在屏幕上   如何使用Java中的EditorConfig在IntelliJ IDEA中关闭rightparen自己的行?   java从字符串中删除字母数字单词   Intellij IDEA 13.1中的maven Java编译错误