from PIL import Image import bytes TARGETIMAGE = 'Christmass.png' FLAGIMAGE = 'WaldoFlag.png' Target = Image.open(TARGETIMAGE) Flag = Image.open(FLAGIMAGE) newImg = Target.copy() (TargetMaxX, TargetMaxY) = newImg.size (FlagMaxX, FlagMaxY) = Flag.size if ((TargetMaxX != FlagMaxX) or (TargetMaxX != FlagMaxX)): raise Exception("This does not work with images of different sizes (yet)") def Merge(OriginRGBA, TargetRGBA, numberOfBits=1): OriginBytes = [bytes.int2byte(x) for x in OriginRGBA] TargetBytes = [bytes.int2byte(x) for x in TargetRGBA] newbytes = [0] * len(TargetBytes) for i in range(len(OriginBytes)): newbytes[i] = bytes.swapMSB2LSB(OriginBytes[i], TargetBytes[i], numberOfBits) # print(OriginBytes, TargetBytes, newbytes) return tuple([bytes.byte2int(b) for b in TargetBytes]) for y in range(FlagMaxY): for x in range(FlagMaxX): OriginRGBA = Flag.getpixel((x, y)) TargetRGBA = Target.getpixel((x, y)) newValues = Merge(OriginRGBA, TargetRGBA) print('%s / %s X \t-\t %s / %s Y' % (x, FlagMaxX, y, FlagMaxY)) newImg.putpixel((x, y), newValues) # Target.close() # Flag.close() print("saving") newImg.save("Hidden.png") print("done") def extract(TargetRGBA, numberOfBits=1): TargetBytes = [bytes.int2byte(x) for x in TargetRGBA] newbytes = [0] * len(TargetBytes) for i in range(len(TargetBytes)): newbytes[i] = bytes.extractLSB(TargetBytes[i], numberOfBits) # print(newbytes, end="\n") return tuple([bytes.byte2int(b) for b in newbytes]) revert = newImg.copy() for y in range(FlagMaxY): for x in range(FlagMaxX): print('%s / %s X \t-\t %s / %s' % (x, FlagMaxX, y, FlagMaxY)) # print([bytes.int2byte(x) for x in Flag.getpixel((x, y))], end="\t - \t") revert.putpixel((x, y), extract(newImg.getpixel((x, y)))) # print("---\n") revert.save("revert.png")