Amazing-Python-Scripts
70 строк · 2.7 Кб
1import argparse
2from stegano import lsb
3import os
4
5
6def hide_text_in_image(image_path, text, output_path):
7secret_image = lsb.hide(image_path, text)
8secret_image.save(output_path)
9
10
11def reveal_text_from_image(image_path):
12try:
13secret_text = lsb.reveal(image_path)
14# here 1 and 0 are used to enhance script quality
15return (secret_text, 1)
16except UnicodeDecodeError:
17return ("Unable to reveal text. Decoding error occurred.", 0)
18except IndexError:
19return ("Failed to find message in this file format, Try using different file format like png", 0)
20except Exception as e:
21return (f"Contact the owner! {e}", 0)
22
23
24def main():
25print("\n[Welcome to Image text hider this script can hide text inside image]\n")
26print("To Hide the text inside image\nUSAGE: python img_text_hider.py hide img_name_with_path.jpg 'This is my secret msg' output_file_name.jpg\n")
27print("To reveal the hidden text inside image\nUSAGE: python img_text_hider.py reveal hidden_img_name.jpg\n")
28parser = argparse.ArgumentParser(description="Image Text Hider")
29
30subparsers = parser.add_subparsers(
31dest="command", help="Available commands")
32
33# Hide command
34hide_parser = subparsers.add_parser(
35"hide", help="Hide text behind an image")
36hide_parser.add_argument("image", help="Path to the image file")
37hide_parser.add_argument("text", help="Text to hide")
38hide_parser.add_argument(
39"output", help="Output path for the image with hidden text")
40
41# Reveal command
42reveal_parser = subparsers.add_parser(
43"reveal", help="Reveal text from an image")
44reveal_parser.add_argument("image", help="Path to the image file")
45
46args = parser.parse_args()
47
48if args.command == "hide":
49if os.path.exists(args.image):
50hide_text_in_image(args.image, args.text, args.output)
51print(
52"Text hidden in the image successfully. Output image saved at", args.output)
53else:
54print("Image path you specified does not exist, Make sure to check image path and file name with extention")
55elif args.command == "reveal":
56if os.path.exists(args.image):
57
58revealed_text, check = reveal_text_from_image(args.image)
59
60if check == 1: # if works out well
61print(f"Revealed text: [{revealed_text}]")
62else: # else display with error so that user can troubleshot the problem easily
63print(f'Error!,{revealed_text}')
64
65else:
66print("Image path you specified does not exist, Make sure to check image path and file name with extention")
67
68
69if __name__ == "__main__":
70main()
71