application_vnd.jupyter.stderr_output_1.png
from PIL import Image

import matplotlib.pyplot as plt

import os

base = '/kaggle/input/alaska2-image-steganalysis/'
id = '{:05d}'.format(20)
cover_path = os.path.join(base, 'Cover', id + '.jpg')
img = plt.imread(cover_path)

from PIL import Image
def genData(data):
    newd = []
    for i in data:
        newd.append(format(ord(i), '08b'))
    return newd
def modPix(pix, data):
    datalist = genData(data)
    lendata = len(datalist)
    imdata = iter(pix)
    for i in range(lendata):
        pix = [value for value in imdata.__next__()[:3] + imdata.__next__()[:3] + imdata.__next__()[:3]]
        for j in range(0, 8):
            if datalist[i][j] == '0' and pix[j] % 2 != 0:
                if pix[j] % 2 != 0:
                    pix[j] -= 1
            elif datalist[i][j] == '1' and pix[j] % 2 == 0:
                pix[j] -= 1
        if i == lendata - 1:
            if pix[-1] % 2 == 0:
                pix[-1] -= 1
        elif pix[-1] % 2 != 0:
            pix[-1] -= 1
        pix = tuple(pix)
        yield pix[0:3]
        yield pix[3:6]
        yield pix[6:9]
def encode_enc(newimg, data):
    w = newimg.size[0]
    x, y = (0, 0)
    for pixel in modPix(newimg.getdata(), data):
        newimg.putpixel((x, y), pixel)
        if x == w - 1:
            x = 0
            y += 1
        else:
            x += 1
    return newimg
def encode():
    img = input('Enter image name(with extension): ')
    image = Image.open(img, 'r')
    data = input('Enter data to be encoded : ')
    if len(data) == 0:
        raise ValueError('Data is empty')
    newimg = image.copy()
    img = encode_enc(newimg, data)
    plt.imshow(img)
def decode():
    img = input('Enter image name(with extension) :')
    image = Image.open(img, 'r')
    data = ''
    imgdata = iter(image.getdata())
    while True:
        pixels = [value for value in imgdata.__next__()[:3] + imgdata.__next__()[:3] + imgdata.__next__()[:3]]
        binstr = ''
        for i in pixels[:8]:
            if i % 2 == 0:
                binstr += '0'
            else:
                binstr += '1'
        data += chr(int(binstr, 2))
        if pixels[-1] % 2 != 0:
            return data
def main():
    a = int(input(':: Welcome to Steganography ::\n1. Encode\n 2. Decode\n'))
    if a == 1:
        encode()
    elif a == 2:
        print('Decoded word- ' + decode())
    else:
        raise Exception('Enter correct input')
if __name__ == '__main__':
    main()