Amazing-Python-Scripts

Форк
0
70 строк · 2.7 Кб
1
import argparse
2
from stegano import lsb
3
import os
4

5

6
def hide_text_in_image(image_path, text, output_path):
7
    secret_image = lsb.hide(image_path, text)
8
    secret_image.save(output_path)
9

10

11
def reveal_text_from_image(image_path):
12
    try:
13
        secret_text = lsb.reveal(image_path)
14
        # here 1 and 0 are used to enhance script quality
15
        return (secret_text, 1)
16
    except UnicodeDecodeError:
17
        return ("Unable to reveal text. Decoding error occurred.", 0)
18
    except IndexError:
19
        return ("Failed to find message in this file format, Try using different file format like png", 0)
20
    except Exception as e:
21
        return (f"Contact the owner! {e}", 0)
22

23

24
def main():
25
    print("\n[Welcome to Image text hider this script can hide text inside image]\n")
26
    print("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")
27
    print("To reveal the hidden text inside image\nUSAGE: python img_text_hider.py reveal hidden_img_name.jpg\n")
28
    parser = argparse.ArgumentParser(description="Image Text Hider")
29

30
    subparsers = parser.add_subparsers(
31
        dest="command", help="Available commands")
32

33
    # Hide command
34
    hide_parser = subparsers.add_parser(
35
        "hide", help="Hide text behind an image")
36
    hide_parser.add_argument("image", help="Path to the image file")
37
    hide_parser.add_argument("text", help="Text to hide")
38
    hide_parser.add_argument(
39
        "output", help="Output path for the image with hidden text")
40

41
    # Reveal command
42
    reveal_parser = subparsers.add_parser(
43
        "reveal", help="Reveal text from an image")
44
    reveal_parser.add_argument("image", help="Path to the image file")
45

46
    args = parser.parse_args()
47

48
    if args.command == "hide":
49
        if os.path.exists(args.image):
50
            hide_text_in_image(args.image, args.text, args.output)
51
            print(
52
                "Text hidden in the image successfully. Output image saved at", args.output)
53
        else:
54
            print("Image path you specified does not exist, Make sure to check image path and file name with extention")
55
    elif args.command == "reveal":
56
        if os.path.exists(args.image):
57

58
            revealed_text, check = reveal_text_from_image(args.image)
59

60
            if check == 1:  # if works out well
61
                print(f"Revealed text: [{revealed_text}]")
62
            else:       # else display with error so that user can troubleshot the problem easily
63
                print(f'Error!,{revealed_text}')
64

65
        else:
66
            print("Image path you specified does not exist, Make sure to check image path and file name with extention")
67

68

69
if __name__ == "__main__":
70
    main()
71

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.