一次蛋疼的图片处理(二)

/ 0评 / 0

之前做的那个均值线性渐变,用到视频处理的时候效果不好啊,啊啊啊啊,要做成曲线值控制的渐变,经过分析,觉得arcsinx的曲线是最合适的,于是乎,就这样操作了,大部分代码其实还是不变的,变化的在下面,主要要弄清楚sin与arcsin之间的关系,来看

from PIL import Image
import numpy as np

def get_color(first, last, area_wid, index):
    tem_c = first - last
    sin_value = np.arcsin(np.sin(index / area_wid)) #这里最主要
    print sin_value, tem_c * sin_value
    print '\n'
    return first - (tem_c * sin_value)


def line(first, last, area_w, area_h, image):
    for i in range(area_h):
        for j in range(area_w):
           image[i, j] = get_color(first, last, area_w, j * 1.0)


def line1(first, last, start, area_w, leng, image):
    for i in range(area_w):
       image[leng, start + i] = get_color(first, last, area_w, i * 1.0)


if __name__ == '__main__':
    im = Image.open('xping.png')
    width = im.size[0]
    height = im.size[1]

    # 中间
    box1 = (width / 4, 0, width * 3 / 4, height)
    region1 = im.crop(box1)

    img = np.array(region1.convert('L'))
    rows, cols = img.shape

    bigList = []
    for i in range(rows):
        smallList = []
        for j in range(cols):
            num = img[i, j]
            if num == 255 or num == 0:
                smallList.append((i, j))
        bigList.append(smallList)

    resList = []
    for smallArr in bigList:
        if len(smallArr) != 0:
            for index in range(len(smallArr)):
                if index > 0:
                    tur = smallArr[index - 1]
                    tur1 = smallArr[index]
                    if tur1[1] - tur[1] > 20:
                        resList.append((tur[0], tur[1], tur1[1]))

    for i in resList:
        line1(255, 0, i[1], i[2] - i[1], i[0], img)

    ceshi1 = Image.fromarray(np.uint8(img))
    ceshi1.save('ceshi1.png')

    # 左右
    box2 = (0, 0, width / 4, height)
    region2 = im.crop(box2).transpose(Image.FLIP_LEFT_RIGHT)

    box3 = (width * 3 / 4, 0, width, height)
    region3 = im.crop(box3).transpose(Image.FLIP_LEFT_RIGHT)

    imgf = Image.new('RGBA', (width / 2, height))
    loc1 = (0, 0, width / 4, height)
    imgf.paste(region2, loc1)
    loc2 = (width / 4, 0, width / 2, height)
    imgf.paste(region3, loc2)

    img = np.array(imgf.convert('L'))
    rows, cols = img.shape

    bigList = []
    for i in range(rows):
        smallList = []
        for j in range(cols):
            num = img[i, j]
            if num == 255 or num == 0:
                smallList.append((i, j))
        bigList.append(smallList)

    resList = []
    for smallArr in bigList:
        if len(smallArr) != 0:
            for index in range(len(smallArr)):
                if index > 0:
                    tur = smallArr[index - 1]
                    tur1 = smallArr[index]
                    if tur1[1] - tur[1] > 20:
                        resList.append((tur[0], tur[1], tur1[1]))

    for i in resList:
        line1(255, 0, i[1], i[2] - i[1], i[0], img)

    ceshi2 = Image.fromarray(np.uint8(img))
    ceshi2.save('ceshi2.png')

    toImage = Image.new('RGBA', (width, height))

    box1 = (0, 0, width / 4, height)
    region1 = ceshi2.crop(box1).transpose(Image.FLIP_LEFT_RIGHT)
    region1.save('ceshi_01.png')

    box2 = (width / 4, 0, width / 2, height)
    region2 = ceshi2.crop(box2).transpose(Image.FLIP_LEFT_RIGHT)
    region2.save('ceshi_02.png')

    toImage.paste(region1, box1)

    loc1 = (width / 4, 0, width * 3 / 4, height)
    toImage.paste(ceshi1, loc1)

    loc2 = (width * 3 / 4, 0, width, height)
    toImage.paste(region2, loc2)

    toImage.save('ceshi3.png')

搞定,来看看效果,暂时先这样了,还要去搞另一个大问题。。。

评论已关闭。