图像处理混合模式
项目描述
这个 Python 包实现了图像的混合模式。
描述
混合模式包可以通过混合模式混合不同的图像或图像层。这些模式在Adobe Photoshop或 GIMP等图形程序中很常见。
通过混合模式进行混合允许以多种方式混合图像。该包目前支持以下混合模式(包中各个函数的名称以斜体显示):
柔光 ( 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)
Grain Merge(来自 GIMP,blend_modes.grain_merge)
除法 ( blend_modes.divide )
叠加(blend_modes.overlay)
正常 ( blend_modes.normal )
可以通过传递给函数的不透明度参数来控制混合强度。有关详细信息,请参阅用法。
混合模式包针对速度进行了优化。它利用 Numpy 的矢量化。在 Cython 中实现包时可以实现进一步的加速。但是,Cython 实现不是此包的一部分。
用法
混合模式函数将表示为数组的图像数据作为输入。这些图像数据通常是通过图像处理包中的函数获得的。Python 中两个流行的图像处理包是PIL或其分支 Pillow和 OpenCV。本章中的示例展示了如何使用这些包混合图像。
输入和输出格式
一个典型的混合模式操作是这样调用的:
blended_img = soft_light(bg_img, fg_img, opacity)
混合模式函数期望 格式为 [维度 1的像素,维度 2 ,4 的像素] 作为输入的Numpy浮点数组。两个图像需要具有相同的大小,因此bg_img和fg_img维度 1 中的像素必须相同。同样适用于维度 2 中的像素。因此,数组的有效形状将是bg_img.shape == (640,320,4)和 fg_img.shape == (640,320,4)。
第三维通道的顺序应该是R, G, B, A,其中A是 alpha 通道。所有值都应该是0.0 <= value <= 255.0范围内的浮点数。
混合模式函数以与输入格式相同的格式返回数组。
例子
以下示例展示了如何在典型应用程序中使用 Blend Modes 包。
这些示例分为三个部分:
加载背景和前景图像。前景图像将被混合到背景图像上。
使用混合模式包通过“柔光”混合模式混合两个图像。该软件包支持多种混合模式。有关完整列表,请参阅 说明。
显示混合图像。
PIL/枕头示例
以下示例显示了如何将 Blend Modes 包与 PIL或 Pillow包一起使用。
from PIL import Image
import numpy
from blend_modes import soft_light
# Import background image
background_img_raw = Image.open('background.png') # RGBA image
background_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 image
foreground_img_raw = Image.open('foreground.png') # RGBA image
foreground_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 images
opacity = 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 image
blended_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 image
blended_img_raw.show()
OpenCV 示例
以下示例显示了如何将 Blend Modes 包与 OpenCV一起使用。
import cv2 # import OpenCV
import numpy
from blend_modes import soft_light
# Import background image
background_img_float = cv2.imread('background.png',-1).astype(float)
# Import foreground image
foreground_img_float = cv2.imread('foreground.png',-1).astype(float)
# Blend images
opacity = 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 image
blended_img_uint8 = blended_img_float.astype(numpy.uint8) # Convert image to OpenCV native display format
cv2.imshow('window', blended_img_uint8)
cv2.waitKey() # Press a key to close window with the image.
安装
Blend Modes 包可以通过 pip 安装: $ pip install blend_modes
依赖项
Blend Modes 包需要 Numpy才能正常运行。为了加载图像,已成功使用以下包:
也可以看看
混合模式在 Wikipedia上有进一步的描述。实际的实现可以在GIMP 源代码中找到,例如在描述除法操作 的文件gimpoperationdividecode.c中。
贡献
我对任何贡献或反馈感到高兴。请通过GitHub 上的问题选项卡让我知道您的评论 。
执照
Blend Modes 软件包在MIT 许可证 (MIT)下分发。还请注意依赖项的许可证。
项目详情
blend_modes -2.1.0.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 0a3145e4792e005764b9663f5ce899d30f7c24f4bcff00428907d03dbe068f37 |
|
| MD5 | ce9f1011818f87247b6b1c0e728542c2 |
|
| 布莱克2-256 | b40061401b0ef3a1e58e2ba305292fef3fc043be4eff0162138e67d9b9fc77de |