扩展PIL的c功能

bqucvtff  于 2023-02-03  发布在  其他
关注(0)|答案(1)|浏览(140)

我想使用不同的混合算法创建类似于PIL Image.blend的功能。为此,我需要:(1)直接修改PIL模块并编译我自己的定制PIL还是(2)编写一个导入和扩展PIL的python c模块?
我已尝试失败:

#include "_imaging.c"

我还尝试从PIL源代码中取出我需要的部分,并将它们放在我自己的文件中。我进入的越多,我必须取出的东西就越多,这似乎不是理想的解决方案。

    • 更新:**编辑以添加python中实现的混合算法(这模拟Photoshop中的叠加混合模式):
def overlay(upx, lpx):
    return (2 * upx * lpx / 255 ) if lpx < 128 else ((255-2 * (255 - upx) * (255 - lpx) / 255))

def blend_images(upper = None, lower = None):
    upixels = upper.load()
    lpixels = lower.load()
    width, height = upper.size
    pixeldata = [0] * len(upixels[0, 0])
    for x in range(width):
        for y in range(height):
            # the next for loop is to deal with images of any number of bands
            for i in range(len(upixels[x,y])):
                pixeldata[i] =  overlay(upixels[x, y][i], lpixels[x, y][i])
            upixels[x,y] = tuple(pixeldata)
    return upper

我也尝试过使用scipy的weave.inline来实现它,但没有成功:

def blend_images(upper=None, lower=None):
    upixels = numpy.array(upper)
    lpixels = numpy.array(lower)
    width, height = upper.size
    nbands = len(upixels[0,0])
    code = """
        #line 120 "laplace.py" (This is only useful for debugging)
        int upx, lpx;
        for (int i = 0; i < width-1; ++i) {
            for (int j=0; j<height-1; ++j) {
                for (int k = 0; k < nbands-1; ++k){
                    upx = upixels[i,j][k];
                    lpx = lpixels[i,j][k];
                    upixels[i,j][k] = ((lpx < 128) ? (2 * upx * lpx / 255):(255 - 2 * (255 - upx) * (255 - lpx) / 255));
                }
            }
        }
        return_val = upixels;
        """
        # compiler keyword only needed on windows with MSVC installed
    upixels = weave.inline(code,
                           ['upixels', 'lpixels', 'width', 'height', 'nbands'],
                           type_converters=converters.blitz,
                           compiler = 'gcc')
    return Image.fromarray(upixels)

我对upixellpixel数组做了一些错误的处理,但是我不确定如何修复它们。我对upixels[i,j][k]的类型有点困惑,并且不确定我可以将它赋给什么。

vs91vp4v

vs91vp4v1#

这是我在NumPy中的实现。我没有单元测试,所以我不知道它是否包含bug。我想如果它失败了,我会听到你的消息。关于发生了什么的解释在注解中。它在0.07秒内处理一个200x400 RGBA图像

import Image, numpy

def blend_images(upper=None, lower=None):
    # convert to arrays
    upx = numpy.asarray(upper).astype('uint16')
    lpx = numpy.asarray(lower).astype('uint16')
    # do some error-checking
    assert upper.mode==lower.mode
    assert upx.shape==lpx.shape
    # calculate the results of the two conditions
    cond1 = 2 * upx * lpx / 255
    cond2 = 255 - 2 * (255 - upx) * (255 - lpx) / 255
    # make a new array that is defined by condition 2
    arr = cond2
    # this is a boolean array that defines where in the array lpx<128
    mask = lpx<128
    # populate the parts of the new arry that meet the critera for condition 1
    arr[mask] = cond1[mask]
    # prevent overflow (may not be necessary)
    arr.clip(0, 255, arr)
    # convert back to image
    return Image.fromarray(arr.astype('uint8'), upper.mode)

相关问题