【问题标题】:Python Pillow: Make gradient for transparencyPython Pillow:为透明度制作渐变
【发布时间】:2016-10-11 11:16:03
【问题描述】:

我有在图像上添加渐变的代码。

def st(path, gradient_magnitude=2.):
    im = Image.open(path)
    if im.mode != 'RGBA':
        im = im.convert('RGBA')
    width, height = im.size
    gradient = Image.new('L', (width, 1), color=0xFF)
    for x in range(width):
        gradient.putpixel((x, 0), int(255 * (1 - gradient_magnitude * float(x) / width)))
    alpha = gradient.resize(im.size)
    black_im = Image.new('RGBA', (width, height), color=0x000000)
    black_im.putalpha(alpha)
    gradient_im = Image.alpha_composite(im, black_im)
    gradient_im.save('out.jpg', 'JPEG')

运行后我得到这张图片

如何让渐变更透明?

【问题讨论】:

  • 使用alpha=0.5 而不是alpha = gradient.resize(im.size)
  • 我收到了这个错误 TypeError: integer argument expected, got float
  • 如果 alpha 应该如何工作 - 新的 alpha 层。这可以是与该图像具有相同大小的“L”或“1”图像,也可以是整数或其他颜色值。这里 black_im.putalpha(alpha)

标签: python python-imaging-library pillow


【解决方案1】:

试试这个。 0.9 或 0.95 的初始不透明度值应该可以满足您的需求。我还对代码进行了一些重组,因为之前它是一团糟。写previous version 的人应该被告知并远离电脑。 ;-)

from PIL import Image

def apply_black_gradient(path_in, path_out='out.png',
                         gradient=1., initial_opacity=1.):
    """
    Applies a black gradient to the image, going from left to right.

    Arguments:
    ---------
        path_in: string
            path to image to apply gradient to
        path_out: string (default 'out.png')
            path to save result to
        gradient: float (default 1.)
            gradient of the gradient; should be non-negative;
            if gradient = 0., the image is black;
            if gradient = 1., the gradient smoothly varies over the full width;
            if gradient > 1., the gradient terminates before the end of the width;
        initial_opacity: float (default 1.)
            scales the initial opacity of the gradient (i.e. on the far left of the image);
            should be between 0. and 1.; values between 0.9-1. give good results
    """

    # get image to operate on
    input_im = Image.open(path_in)
    if input_im.mode != 'RGBA':
        input_im = input_im.convert('RGBA')
    width, height = input_im.size

    # create a gradient that
    # starts at full opacity * initial_value
    # decrements opacity by gradient * x / width
    alpha_gradient = Image.new('L', (width, 1), color=0xFF)
    for x in range(width):
        a = int((initial_opacity * 255.) * (1. - gradient * float(x)/width))
        if a > 0:
            alpha_gradient.putpixel((x, 0), a)
        else:
            alpha_gradient.putpixel((x, 0), 0)
        # print '{}, {:.2f}, {}'.format(x, float(x) / width, a)
    alpha = alpha_gradient.resize(input_im.size)

    # create black image, apply gradient
    black_im = Image.new('RGBA', (width, height), color=0) # i.e. black
    black_im.putalpha(alpha)

    # make composite with original image
    output_im = Image.alpha_composite(input_im, black_im)
    output_im.save(path_out, 'PNG')

    return

【讨论】:

  • 再次感谢!这是我第一次尝试图像处理,我将研究更多的文档和文章。谢谢!
  • 一如既往地高兴。您是否开始了解a = int((initial_opacity * 255.) * (1. - gradient * float(x)/width)) 行的工作原理?这就是所有魔术发生的地方(取消注释 print 语句可能会有所帮助)。之后,它实际上只是将 alpha 应用于黑色图像,然后将黑色图像与输入图像混合。
猜你喜欢
  • 2011-04-18
  • 2011-01-18
  • 2014-07-21
  • 2013-03-13
  • 1970-01-01
  • 2012-12-19
  • 2020-04-27
  • 2018-07-06
  • 2020-07-11
相关资源
最近更新 更多