相关疑难解决方法(0)

如何使用PIL将透明png图像与另一个图像合并

我有一个透明的png图像"foo.png",我打开了另一个图像

im = Image.open("foo2.png");
Run Code Online (Sandbox Code Playgroud)

现在我需要的是将foo.png与foo2.png合并.

(foo.png包含一些文本,我想在foo2.png上打印该文本)

python image image-processing python-imaging-library

138
推荐指数
4
解决办法
10万
查看次数

使用PIL将RGBA PNG转换为RGB

我正在使用PIL将使用Django上传的透明PNG图像转换为JPG文件.输出看起来很糟糕.

源文件

透明的源文件

Image.open(object.logo.path).save('/tmp/output.jpg', 'JPEG')
Run Code Online (Sandbox Code Playgroud)

要么

Image.open(object.logo.path).convert('RGB').save('/tmp/output.png')
Run Code Online (Sandbox Code Playgroud)

结果

两种方式,生成的图像如下所示:

结果文件

有没有办法来解决这个问题?我想要有透明背景的白色背景.


感谢很棒的答案,我想出了以下函数集合:

import Image
import numpy as np


def alpha_to_color(image, color=(255, 255, 255)):
    """Set all fully transparent pixels of an RGBA image to the specified color.
    This is a very simple solution that might leave over some ugly edges, due
    to semi-transparent areas. You should use alpha_composite_with color instead.

    Source: http://stackoverflow.com/a/9166671/284318

    Keyword Arguments:
    image -- PIL RGBA Image object
    color -- Tuple r, g, b (default 255, 255, 255)

    """ …
Run Code Online (Sandbox Code Playgroud)

python png jpeg python-imaging-library rgba

85
推荐指数
3
解决办法
10万
查看次数

在python中混合重叠的图像

我在python中拍摄了两张图像,并将第一张图像重叠到第二张图像上.我想做的是将图像混合在一起.有没有办法在python中执行此操作而不是for循环?

python image-manipulation color-blending

11
推荐指数
2
解决办法
2万
查看次数

覆盖两个numpy阵列,将第四个平面视为alpha级别

我有两个numpy形状的阵列(256,256,4).我想将第四个256 x 256平面视为alpha级别,并导出一个覆盖这些数组的图像.

代码示例:

import numpy as np
from skimage import io

fg = np.ndarray((256, 256, 4), dtype=np.uint8)
one_plane = np.random.standard_normal((256, 256)) * 100 + 128
fg[:,:,0:3] = np.tile(one_plane, 3).reshape((256, 256, 3), order='F')
fg[:, :, 3] = np.zeros((256, 256), dtype=np.uint8)
fg[0:128, 0:128, 3] = np.ones((128, 128), dtype=np.uint8) * 255
fg[128:256, 128:256, 3] = np.ones((128, 128), dtype=np.uint8) * 128

bg = np.ndarray((256, 256, 4), dtype=np.uint8)
bg[:,:,0:3] = np.random.standard_normal((256, 256, 3)) * 100 + 128
bg[:, :, 3] = np.ones((256, 256), dtype=np.uint8) …
Run Code Online (Sandbox Code Playgroud)

python numpy image alphablending scikit-image

4
推荐指数
1
解决办法
3031
查看次数