|
| 1 | +import argparse |
| 2 | +import os |
| 3 | + |
| 4 | +from PIL import Image |
| 5 | +import stepic |
| 6 | + |
| 7 | + |
| 8 | +def encode(file, text): |
| 9 | + # Open Image or file in which you want to hide your data |
| 10 | + image = Image.open(file) |
| 11 | + |
| 12 | + # Convert the string to bytes |
| 13 | + text = bytes(text, 'utf-8') |
| 14 | + |
| 15 | + # Encode some text into your image file and save it in another file |
| 16 | + encodeImage = stepic.encode(image, text) |
| 17 | + |
| 18 | + # Get the filename from the path |
| 19 | + filename = os.path.splitext(os.path.basename(file))[0] |
| 20 | + |
| 21 | + # Save the image in PNG file |
| 22 | + encodeImage.save(f'{filename}_enc.png', 'PNG') |
| 23 | + |
| 24 | + print(f'File Encoded: {filename}_enc.png') |
| 25 | + print(f'String Used To Encode: {text}') |
| 26 | + |
| 27 | + |
| 28 | +def decode(path): |
| 29 | + # Open Image or file in which data exists |
| 30 | + image = Image.open(path) |
| 31 | + |
| 32 | + # Decode the image to extract the text |
| 33 | + decodeImage = stepic.decode(image) |
| 34 | + |
| 35 | + # Print the text |
| 36 | + print(f'Decoded String From Image: {decodeImage}') |
| 37 | + |
| 38 | + |
| 39 | +def main(): |
| 40 | + parser = argparse.ArgumentParser(description='STEGANOGRAPHY IN IMAGES') |
| 41 | + |
| 42 | + # Argument is for images to encode/decode |
| 43 | + parser.add_argument('-f', dest='files', nargs='+', type=str, help='Image File', required=True) |
| 44 | + |
| 45 | + # Argument is to encode in the image |
| 46 | + parser.add_argument('-e', dest='encode', type=str, help='Encode The Text In Image') |
| 47 | + |
| 48 | + # Argument is to decode from image |
| 49 | + parser.add_argument('-d', dest='decode', action='store_true', help='Decode The Text In Image') |
| 50 | + |
| 51 | + args = parser.parse_args() |
| 52 | + |
| 53 | + # Get the files |
| 54 | + files = args.files |
| 55 | + |
| 56 | + if files: |
| 57 | + # If user wants to encode |
| 58 | + if args.encode: |
| 59 | + # Iterate through the files |
| 60 | + for file in files: |
| 61 | + # Check if the file exists |
| 62 | + if os.path.isfile(file): |
| 63 | + # Call the function |
| 64 | + encode(file, args.encode) |
| 65 | + |
| 66 | + else: |
| 67 | + |
| 68 | + print(f'File Not Found: {file}') |
| 69 | + |
| 70 | + # If user wants to decode |
| 71 | + elif args.decode: |
| 72 | + # Iterate through the files |
| 73 | + for file in files: |
| 74 | + # Check if the file exists |
| 75 | + if os.path.isfile(file): |
| 76 | + # Call the function |
| 77 | + decode(file) |
| 78 | + |
| 79 | + else: |
| 80 | + print(f'File Not Found: {file}') |
| 81 | + |
| 82 | + else: |
| 83 | + print('Use -h For Help') |
| 84 | + |
| 85 | + else: |
| 86 | + print('File Not Found') |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + main() |
0 commit comments