扩展PIL的c功能

时间:2011-03-01 23:05:49

标签: python c python-imaging-library

我想使用不同的混合算法创建类似于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]的类型感到有点困惑,不知道我可以分配给它。

1 个答案:

答案 0 :(得分:3)

这是我在NumPy中的实现。我没有单元测试,所以我不知道它是否包含错误。我想我会听到你失败的消息。正在进行的解释是在评论中。它在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)
相关问题