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