如何在python中叠加图像

时间:2016-11-22 18:34:15

标签: python cryptography pillow

from PIL import Image
import sys

infile1 = Image.open(sys.argv[1])
infile2 = Image.open(sys.argv[2])
infile3 = Image.open(sys.argv[3])
infile4 = Image.open(sys.argv[4])

outfile1 = Image.new('1', infile1.size)
outfile2 = Image.new('1', infile1.size)
outfile3 = Image.new('1', infile1.size)

for x in range(infile1.size[0]):
    for y in range(infile1.size[1]):
        outfile1.putpixel((x, y), max(infile1.getpixel((x, y)), infile2.getpixel((x, y))))
        outfile2.putpixel((x, y), max(infile3.getpixel((x, y)), infile4.getpixel((x, y))))
        outfile3.putpixel((x, y), max(outfile1.getpixel((x, y)), outfile2.getpixel((x, y))))


outfile1.save('result.png')

infile1,infile2,infile3和infile4是作为输入给出的png图像。这些.png文件是可视加密方案的输出,其中一个输入图像被分成4个不同的图像(共享)。 你可以在这里找到它:http://www.datagenetics.com/blog/november32013/index.html

将原始图像拆分为4股

for x in range(0, image.size[0]):
    for y in range(0, image.size[1]):
    sourcepixel = image.getpixel((x, y))
    print sourcepixel
    assert sourcepixel in (0, 255)
    coinflip = random.random()
    #coinflip = 0.6
    print coinflip
    if sourcepixel == 0: #if it's black
        if coinflip < .5:
            print "black pixel with less than 0.5"
            #arrangement 1
            #black, white, white top row [(0,0),(1,0),(2,)]
            share1.putpixel((x * 2, y * 2), 0)
            share1.putpixel((x * 2 + 1, y * 2 ), 255)
            share1.putpixel((x * 2 + 2, y * 2 ), 255)

            #black,black,black mid row
            share1.putpixel((x * 2, y * 2 + 1), 0)
            share1.putpixel((x * 2 + 1, y * 2 + 1), 0)
            share1.putpixel((x * 2 + 2, y * 2 + 1), 0)

            #white, black, white bottom row
            share1.putpixel((x * 2, y * 2 + 2), 255)
            share1.putpixel((x * 2 + 1, y * 2 + 2), 0 )
            share1.putpixel((x * 2 + 2, y * 2 + 2), 255)

            #arrangement 2
            #white,black,white
            share2.putpixel((x * 2, y * 2), 255)
            share2.putpixel((x * 2 + 1, y * 2 ), 0)
            share2.putpixel((x * 2 + 2, y * 2 ), 255)

            #black,black,white
            share2.putpixel((x * 2, y * 2 + 1), 0)
            share2.putpixel((x * 2 + 1, y * 2 + 1), 0)
            share2.putpixel((x * 2 + 2, y * 2 + 1), 255)

            #black,black,white
            share2.putpixel((x * 2, y * 2 + 2), 0)
            share2.putpixel((x * 2 + 1, y * 2 + 2), 0 )
            share2.putpixel((x * 2 + 2, y * 2 + 2), 255)

        else:
            #arrangement 3
            #white,black,white
            share3.putpixel((x * 2, y * 2), 255)
            share3.putpixel((x * 2 + 1, y * 2 ), 0)
            share3.putpixel((x * 2 + 2, y * 2 ), 255)

            #black,black,black
            share3.putpixel((x * 2, y * 2 + 1), 0)
            share3.putpixel((x * 2 + 1, y * 2 + 1), 0)
            share3.putpixel((x * 2 + 2, y * 2 + 1), 0)

            #white,white,black
            share3.putpixel((x * 2, y * 2 + 2), 255)
            share3.putpixel((x * 2 + 1, y * 2 + 2), 255)
            share3.putpixel((x * 2 + 2, y * 2 + 2), 0)

            #arrangement 4
            #white,black,black
            share4.putpixel((x * 2, y * 2), 255)
            share4.putpixel((x * 2 + 1, y * 2 ), 0)
            share4.putpixel((x * 2 + 2, y * 2 ), 0)

            #white,black,black
            share4.putpixel((x * 2, y * 2 + 1), 255)
            share4.putpixel((x * 2 + 1, y * 2 + 1), 0)
            share4.putpixel((x * 2 + 2, y * 2 + 1), 0)

            #white,black,white
            share4.putpixel((x * 2, y * 2 + 2), 255)
            share4.putpixel((x * 2 + 1, y * 2 + 2), 0)
            share4.putpixel((x * 2 + 2, y * 2 + 2), 255)

    elif sourcepixel == 255: # if it's white
        if coinflip < .5:
            print "white pixel with less than 0.5"
            #arrangement 1
            #white, white, white top row [(0,0),(1,0),(2,)]
            share1.putpixel((x * 2, y * 2), 255)
            share1.putpixel((x * 2 + 1, y * 2 ), 255)
            share1.putpixel((x * 2 + 2, y * 2 ), 255)

            #black,black,black mid row
            share1.putpixel((x * 2, y * 2 + 1), 0)
            share1.putpixel((x * 2 + 1, y * 2 + 1), 0)
            share1.putpixel((x * 2 + 2, y * 2 + 1), 0)

            #black, black, white bottom row
            share1.putpixel((x * 2, y * 2 + 2), 0)
            share1.putpixel((x * 2 + 1, y * 2 + 2), 0 )
            share1.putpixel((x * 2 + 2, y * 2 + 2), 255)

            #arrangement 2
            #white,black,white
            share2.putpixel((x * 2, y * 2), 255)
            share2.putpixel((x * 2 + 1, y * 2 ), 255)
            share2.putpixel((x * 2 + 2, y * 2 ), 0)

            #black,black,white
            share2.putpixel((x * 2, y * 2 + 1), 0)
            share2.putpixel((x * 2 + 1, y * 2 + 1), 0)
            share2.putpixel((x * 2 + 2, y * 2 + 1), 255)

            #black,white,black
            share2.putpixel((x * 2, y * 2 + 2), 0)
            share2.putpixel((x * 2 + 1, y * 2 + 2), 255 )
            share2.putpixel((x * 2 + 2, y * 2 + 2), 0)

        else:
            #arrangement 3
            #white,black,white
            share3.putpixel((x * 2, y * 2), 255)
            share3.putpixel((x * 2 + 1, y * 2 ), 0)
            share3.putpixel((x * 2 + 2, y * 2 ), 255)

            #black,black,white
            share3.putpixel((x * 2, y * 2 + 1), 0)
            share3.putpixel((x * 2 + 1, y * 2 + 1), 0)
            share3.putpixel((x * 2 + 2, y * 2 + 1), 255)

            #white,black,black
            share3.putpixel((x * 2, y * 2 + 2), 255)
            share3.putpixel((x * 2 + 1, y * 2 + 2), 0)
            share3.putpixel((x * 2 + 2, y * 2 + 2), 0)

            #arrangement 4
            #white,black,black
            share4.putpixel((x * 2, y * 2), 255)
            share4.putpixel((x * 2 + 1, y * 2 ), 0)
            share4.putpixel((x * 2 + 2, y * 2 ), 0)

            #black,black,black
            share4.putpixel((x * 2, y * 2 + 1), 0)
            share4.putpixel((x * 2 + 1, y * 2 + 1), 0)
            share4.putpixel((x * 2 + 2, y * 2 + 1), 0)

            #white,white,white
            share4.putpixel((x * 2, y * 2 + 2), 255)
            share4.putpixel((x * 2 + 1, y * 2 + 2), 255)
            share4.putpixel((x * 2 + 2, y * 2 + 2), 255)

share1.save('share1.png')
share2.save('share2.png')
share3.save('share3.png')
share4.save('share4.png')

我在画笔中创建了一个简单的testfile.png,白色背景上有一些黑色形状。 这些股票是分开的,通常会留下2股的更多信息。

然而,在组合这些份额时,结果文件看起来并不像原始图像。 关于如何测试股票是否正常的任何建议,如果他们是如何将它们组合起来(重叠它们以便返回原始图像)?

0 个答案:

没有答案