How to overlay images in python

不问归期 提交于 2019-12-25 09:27:41

问题


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 and infile4 are png images that are given as input. These .png files are the output of a visual cryptography scheme where one input image is split into 4 different images (shares). you can find it here: http://www.datagenetics.com/blog/november32013/index.html

While splitting the original image into 4 shares

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')

I created a simple testfile.png in paintbrush, a white background with some black shapes on it. The shares are divided, often leaving more information on 2 shares than the others.

On combining these shares however, the result file doesn't look anything like the original image. Any suggestions on how to test if the shares are made fine, and if they are then how would i combine them (over lay them so that the original image is returned) ?

来源:https://stackoverflow.com/questions/40749205/how-to-overlay-images-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!