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

/ 0评 / 0

这几天都在搞一张图,一张蛋疼的图,来看看原图

要让两边大小差不多,然后黑白的周围要平均渐变,写程序解决‍。。。那就开整吧,选择的是Python,快呀!
先让两边大小差不多,来看

from PIL import Image
import numpy as np

if __name__ == '__main__':
    im = Image.open('res5.png')
    width = im.size[0]
    height = im.size[1]
    print "/* width:%d */" % (width)
    print "/* height:%d */" % (height)
    #裁剪
    #(左,上,右,下)
    box1 = (0, 0, width/2, height)
    region1 = im.crop(box1)

    box2 = (width/2, 0, width, height)
    region2 = im.crop(box2).transpose(Image.FLIP_LEFT_RIGHT)

    img1 = np.array(region1.convert('L'))
    img2 = np.array(region2.convert('L'))

    rows, cols = img1.shape
    for i in range(rows):
        for j in range(cols):
            if img1[i, j] == 255:
                img2[i, j] = 0

    res1 = Image.fromarray(np.uint8(img1))
    res1.save('res1.png')

    res2 = Image.fromarray(np.uint8(img2)).transpose(Image.FLIP_LEFT_RIGHT)
    res2.save('res2.png')

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

    loc1 = (0, 0)
    toImage.paste(res1, loc1)

    loc2 = (width/2, 0)
    toImage.paste(res2, loc2)
    toImage.save('res.png')

    box3 = (width*3/4, 0, width, height/2)
    region3 = toImage.crop(box3)
    img3 = np.array(region3.convert('L'))
    rows, cols = img3.shape

    blackList = []
    for i in range(rows):
        for j in range(cols):
            num = img3[i, j]
            if num > 0:
                blackList.append((i, j))
                break

    for i in blackList:
        if i[1] > 0:
            for j in range(10):
                img3[i[0], i[1]+j] = 0

    res3 = Image.fromarray(np.uint8(img3))
    toImage.paste(res3, box3)

    box4 = (width*3/4, height/2, width, height)
    res4 = res3.transpose(Image.FLIP_TOP_BOTTOM)
    toImage.paste(res4, box4)
    toImage.save('resf.png')

将上一步的到的图片拆分,拼接起来由白到黑做平均值线性渐变

from PIL import Image
import numpy as np

if __name__ == "__main__":
    im = Image.open('resf.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 = []
    str = ''
    for i in range(rows):
        str += '['
        smallList = []
        for j in range(cols):
            num = img[i, j]
            if num == 255 or num == 0:
                smallList.append((i, j))
                str += '(%d, %d)'%(i, j)
        bigList.append(smallList)
        str += ']\n'

    file = open('rgb.txt', 'w')
    file.write(str)
    file.close()

    resList = []
    str1 = ''
    for smallArr in bigList:
        if len(smallArr) != 0:
            str1 += '['
            for index in range(len(smallArr)):
                if index > 0:
                    tur = smallArr[index-1]
                    tur1 = smallArr[index]
                    if tur1[1]-tur[1] > 20:
                        str1 += '(%d, %d)' % (tur[1], tur1[1])
                        resList.append((tur[0], tur[1], tur1[1]))
            str1 += ']\n'

    file1 = open('rgb1.txt', 'w')
    file1.write(str1)
    file1.close()

    str2 = ''
    for i in resList:
        k = 255.0/(i[2]-i[1])
        str2 += '['
        for j in range(i[1], i[2]):
            img[i[0], j] = 255 - k*(j-i[1])
            str2 += '(%f, %d, %d)' % (k, j-i[1], j)
        str2 += ']\n'

    file2 = open('rgb2.txt', 'w')
    file2.write(str2)
    file2.close()

    res1 = Image.fromarray(np.uint8(img))
    res1.save('box1.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:
        k = 255.0 / (i[2] - i[1])
        for j in range(i[1], i[2]):
            img[i[0], j] = 255 - k * (j - i[1])

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

拆分,拼接图片,拆拆拆,拼拼拼

from PIL import Image

if __name__ == "__main__":
    im = Image.open('box1.png')
    width = im.size[0]
    height = im.size[1]
    width2 = width * 2

    im2 = Image.open('box0.png')

    box1 = (0, 0, width / 2, height)
    region1 = im2.crop(box1).transpose(Image.FLIP_LEFT_RIGHT)
    region1.save('region1.png')

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

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

    loc1 = (0, 0, width2 / 4, height)
    toImage.paste(region1, loc1)

    loc2 = (width2 / 4, 0, width2 * 3 / 4, height)
    toImage.paste(im, loc2)

    loc3 = (width2 * 3 / 4, 0, width2, height)
    toImage.paste(region2, loc3)

    toImage.save('res3.png')

最后由于上下各有一条颜色不对的地方,我们要做纵向的线性渐变,来看

from PIL import Image
import numpy as np

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

    box1 = (0, 0, width / 2, height / 2)
    region1 = im.crop(box1)
    region1.save('box3.png')

    img = np.array(region1.convert('L'))

    y = 0
    rows, cols = img.shape
    for i in range(cols):
        if i == width / 4:
            for j in range(rows):
                num = img[j, i]
                if num == 255:
                    y = j+1
                    break

    num_list = []
    num = 130.0
    v = (255 - num) / y
    for i in range(cols):
        if i == width / 4:
            for j in range(y):
                num += v
                img[j, i] = num
                num_list.append(num)

    for i in range(y):
        num = 130.0
        m = num_list[i] - 130.0
        k = m / (width / 4)
        for j in range(cols):
            if j < width / 4:
                num += k
                img[i, j] = num

    for i in range(y):
        m = num_list[i] - 130.0
        k = m / (width / 4)
        for j in range(cols):
            if j > width / 4:
                num = num_list[i] - k*(j - width/4)
                img[i, j] = num
                print num

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

    # 黑的
    box2 = (width / 2, 0, width, height / 2)
    region2 = im.crop(box2)
    region2.save('box4.png')

    img = np.array(region2.convert('L'))

    y = 0
    rows, cols = img.shape
    for i in range(cols):
        if i == width / 4:
            for j in range(rows):
                num = img[j, i]
                if num == 0:
                    y = j+1
                    break

    num_list = []
    num = 130.0
    v = num / y
    for i in range(cols):
        if i == width / 4:
            for j in range(y):
                num -= v
                img[j, i] = num
                num_list.append(num)

    for i in range(y):
        num = 130.0
        m = 130.0 - num_list[i]
        k = m / (width / 4)
        for j in range(cols):
            if j < width / 4:
                num -= k
                img[i, j] = num

    for i in range(y):
        m = 130.0 - num_list[i]
        k = m / (width / 4)
        for j in range(cols):
            if j > width / 4:
                num = num_list[i] + k*(j - width/4)
                img[i, j] = num
                print num

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

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

    loc1 = (0, 0, width / 2, height/2)
    toImage.paste(res1, loc1)

    loc2 = (width / 2, 0, width, height/2)
    toImage.paste(res2, loc2)

    loc3 = (0, height / 2, width / 2, height)
    res1_1 = res1.transpose(Image.FLIP_TOP_BOTTOM)
    toImage.paste(res1_1, loc3)

    loc4 = (width / 2, height / 2, width, height)
    res2_2 = res2.transpose(Image.FLIP_TOP_BOTTOM)
    toImage.paste(res2_2, loc4)

    toImage.save('xyxyx.png')

搞定收工,真是麻烦了,看效果

评论已关闭。