From 9d1d7bef0f31f5b438da188be2e5f28fef8a584e Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Thu, 10 Oct 2024 13:42:50 +0530 Subject: [PATCH 1/6] Added a python script that uploads an image to imgur and also handles exceptions gracefully --- image_uploader/.gitignore | 1 + image_uploader/README.md | 23 ++++++++++++++++++++ image_uploader/main.py | 45 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 image_uploader/.gitignore create mode 100644 image_uploader/README.md create mode 100644 image_uploader/main.py diff --git a/image_uploader/.gitignore b/image_uploader/.gitignore new file mode 100644 index 0000000..30fe0fb --- /dev/null +++ b/image_uploader/.gitignore @@ -0,0 +1 @@ +secret.env \ No newline at end of file diff --git a/image_uploader/README.md b/image_uploader/README.md new file mode 100644 index 0000000..61efce3 --- /dev/null +++ b/image_uploader/README.md @@ -0,0 +1,23 @@ +# Image Upload Script + +## Overview + +This Python script allows users to upload images from their clipboard directly to Imgur by pressing a keyboard shortcut. It utilizes the Imgur API for image uploads and the `python-dotenv` package to manage environment variables securely. + +## Features + +- **Clipboard Image Capture**: Captures images from the clipboard. +- **Imgur API Integration**: Uploads images to Imgur using a simple API call. +- **Keyboard Shortcut**: Allows users to trigger the upload with a predefined keyboard shortcut (`Ctrl + Alt + S`). +- **Environment Variable Management**: Utilizes a `secret.env` file for managing sensitive data, such as the Imgur Client ID and add it under the name `IMGUR_CLIENT_ID`. + + +**Note**: You can add an image in your clipboard using `Win + Shift + S` +Also press Esc to end the program + +## Example Screenshot + +Here’s how the application looks when running: + +![Screenshot of the app](https://i.imgur.com/e35Pvyh.png) +![Screenshot of the app](https://i.imgur.com/ZfyHcsx.png) \ No newline at end of file diff --git a/image_uploader/main.py b/image_uploader/main.py new file mode 100644 index 0000000..edad516 --- /dev/null +++ b/image_uploader/main.py @@ -0,0 +1,45 @@ +import os +import requests +import keyboard +from PIL import ImageGrab +import io +import base64 +from dotenv import load_dotenv + + +load_dotenv("secret.env") +# Set your Imgur API client ID here +CLIENT_ID = os.getenv("IMGUR_CLIENT_ID") + +def upload_to_imgur(image_data): + headers = {"Authorization": f"Client-ID {CLIENT_ID}"} + response = requests.post("https://api.imgur.com/3/image", headers=headers, data={"image": image_data}) + return response.json() + +def upload_image(): + try: + image = ImageGrab.grabclipboard() + if image is None: + print("No image found in the clipboard.") + return + + + with io.BytesIO() as output: + image.save(output, format='PNG') + image_data = base64.b64encode(output.getvalue()).decode() # converted to base64 + + # Upload the image to Imgur + response = upload_to_imgur(image_data) + + if response.get("success"): + print("Image uploaded successfully:", response["data"]["link"]) + else: + print("Failed to upload image:", response) + except Exception as e: + print(f"An error occurred: {e}") + + +keyboard.add_hotkey('ctrl+alt+s', upload_image) + +print("Listening for the shortcut... (Press ESC to stop)") +keyboard.wait('esc') From a7bf40e440d9b90a76466373b0ddba21b4cca4e4 Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Fri, 11 Oct 2024 09:16:47 +0530 Subject: [PATCH 2/6] Made requested changes --- {image_uploader => Image Uploader}/.gitignore | 0 {image_uploader => Image Uploader}/README.md | 0 {image_uploader => Image Uploader}/main.py | 0 README.md | 3 ++- 4 files changed, 2 insertions(+), 1 deletion(-) rename {image_uploader => Image Uploader}/.gitignore (100%) rename {image_uploader => Image Uploader}/README.md (100%) rename {image_uploader => Image Uploader}/main.py (100%) diff --git a/image_uploader/.gitignore b/Image Uploader/.gitignore similarity index 100% rename from image_uploader/.gitignore rename to Image Uploader/.gitignore diff --git a/image_uploader/README.md b/Image Uploader/README.md similarity index 100% rename from image_uploader/README.md rename to Image Uploader/README.md diff --git a/image_uploader/main.py b/Image Uploader/main.py similarity index 100% rename from image_uploader/main.py rename to Image Uploader/main.py diff --git a/README.md b/README.md index 97710f0..db63d76 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,8 @@ More information on contributing and the general code of conduct for discussion | Image Compress | [Image Compress](https://github.com/DhanushNehru/Python-Scripts/tree/master/Image%20Compress) | Takes an image and compresses it. | | Image Manipulation without libraries | [Image Manipulation without libraries](https://github.com/DhanushNehru/Python-Scripts/tree/master/Image%20Manipulation%20without%20libraries) | Manipulates images without using any external libraries. | | Image Text | [Image Text](https://github.com/DhanushNehru/Python-Scripts/tree/master/Image%20Text) | Extracts text from the image. | -| Image Text to PDF | [Image Text to PDF](https://github.com/DhanushNehru/Python-Scripts/tree/master/Image%20Text%20to%20PDF) | Adds an image and text to a PDF. +| Image Text to PDF | [Image Text to PDF](https://github.com/DhanushNehru/Python-Scripts/tree/master/Image%20Text%20to%20PDF) | Adds an image and text to a PDF. +| Image Uploader | [Image Uploader](https://github.com/DhanushNehru/Python-Scripts/tree/master/Image%20Uploader) | Uploads images to Imgur using a keyboard shortcut. | | Image Watermarker | [Image Watermarker](https://github.com/DhanushNehru/Python-Scripts/tree/master/Image%20Watermarker) | Adds a watermark to an image. | Image to ASCII | [Image to ASCII](https://github.com/DhanushNehru/Python-Scripts/tree/master/Image%20to%20ASCII) | Converts an image into ASCII art. | | Image to Gif | [Image to Gif](https://github.com/DhanushNehru/Python-Scripts/tree/master/Image%20to%20GIF) | Generate gif from images. From 4aadeb2d685d794f98f92c7b2e917879b1843d33 Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Fri, 11 Oct 2024 18:08:58 +0530 Subject: [PATCH 3/6] Created morse code encoder and decoder --- Morse Code/README.md | 22 +++++++++++++++++ Morse Code/main.py | 57 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 1 + 3 files changed, 80 insertions(+) create mode 100644 Morse Code/README.md create mode 100644 Morse Code/main.py diff --git a/Morse Code/README.md b/Morse Code/README.md new file mode 100644 index 0000000..d0dd993 --- /dev/null +++ b/Morse Code/README.md @@ -0,0 +1,22 @@ +# Morse Code Encoder and Decoder + +This is a simple Python program that encodes text to Morse code and decodes Morse code back to text. It features exception handling and a menu-driven interface for user interaction. + +## Features + +- **Encode Text to Morse Code**: Convert alphanumeric text into Morse code. +- **Decode Morse Code to Text**: Convert Morse code back to alphanumeric text. +- **Exception Handling**: Handles unsupported characters during encoding and decoding. +- **Menu-Driven Interface**: Easy navigation with clear options for encoding, decoding, or exiting. + +## Requirements + +- Python 3.x + + +## How to use +Just run this program on your local device using + + ```bash + python morse_code.py + diff --git a/Morse Code/main.py b/Morse Code/main.py new file mode 100644 index 0000000..a5ac817 --- /dev/null +++ b/Morse Code/main.py @@ -0,0 +1,57 @@ +morse_code_dict = { + 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', + 'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---', + 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', + 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', + 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', + 'Z': '--..', '1': '.----', '2': '..---', '3': '...--', + '4': '....-', '5': '.....', '6': '-....', '7': '--...', + '8': '---..', '9': '----.', '0': '-----', ' ': '/' +} + +def encode_to_morse(text): + try: + return ' '.join(morse_code_dict[char] for char in text.upper() if char in morse_code_dict) + except KeyError: + print("Error: Input contains unsupported characters.") + +def decode_from_morse(morse_code): + try: + morse_words = morse_code.split(' / ') + decoded_message = '' + for word in morse_words: + decoded_message += ''.join( + [char for morse_char in word.split() for char, code in morse_code_dict.items() if code == morse_char] + ) + ' ' + return decoded_message.strip() + except Exception as e: + print("Error during decoding:", e) + +def menu(): + while True: + print("\nMorse Code Encoder/Decoder") + print("1. Encode Text to Morse Code") + print("2. Decode Morse Code to Text") + print("3. Exit") + + choice = input("Choose an option (1-3): ") + + if choice == '1': + text = input("Enter text to encode: ") + encoded = encode_to_morse(text) + print("Encoded Morse Code:", encoded) + + elif choice == '2': + morse_code = input("Enter Morse code to decode (use '/' for spaces): ") + decoded = decode_from_morse(morse_code) + print("Decoded Text:", decoded) + + elif choice == '3': + print("Exiting program.") + break + + else: + print("Invalid choice, please try again.") + +if __name__ == "__main__": + menu() diff --git a/README.md b/README.md index db63d76..68bf3cb 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ More information on contributing and the general code of conduct for discussion | Mail Sender | [Mail Sender](https://github.com/DhanushNehru/Python-Scripts/tree/master/Mail%20Sender) | Sends an email. | | Merge Two Images | [Merge Two Images](https://github.com/DhanushNehru/Python-Scripts/tree/master/Merge%20Two%20Images) | Merges two images horizontally or vertically. | | Mouse mover | [Mouse mover](https://github.com/DhanushNehru/Python-Scripts/tree/master/Mouse%20Mover) | Moves your mouse every 15 seconds. | +| Morse Code | [Mose Code](https://github.com/DhanushNehru/Python-Scripts/tree/master/Morse%20Code) | Encodes and decodes Morse code. | | No Screensaver | [No Screensaver](https://github.com/DhanushNehru/Python-Scripts/tree/master/No%20Screensaver) | Prevents screensaver from turning on. | | OTP Verification | [OTP Verification](https://github.com/DhanushNehru/Python-Scripts/tree/master/OTP%20%20Verify) | An OTP Verification Checker. | | Password Generator | [Password Generator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Password%20Generator) | Generates a random password. | From 28301739c3121cd137279bd0d09a07e9e6e6549a Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Sat, 12 Oct 2024 12:27:03 +0530 Subject: [PATCH 4/6] seperate 2 PRs --- Morse Code/README.md | 22 ----------------- Morse Code/main.py | 57 -------------------------------------------- 2 files changed, 79 deletions(-) delete mode 100644 Morse Code/README.md delete mode 100644 Morse Code/main.py diff --git a/Morse Code/README.md b/Morse Code/README.md deleted file mode 100644 index d0dd993..0000000 --- a/Morse Code/README.md +++ /dev/null @@ -1,22 +0,0 @@ -# Morse Code Encoder and Decoder - -This is a simple Python program that encodes text to Morse code and decodes Morse code back to text. It features exception handling and a menu-driven interface for user interaction. - -## Features - -- **Encode Text to Morse Code**: Convert alphanumeric text into Morse code. -- **Decode Morse Code to Text**: Convert Morse code back to alphanumeric text. -- **Exception Handling**: Handles unsupported characters during encoding and decoding. -- **Menu-Driven Interface**: Easy navigation with clear options for encoding, decoding, or exiting. - -## Requirements - -- Python 3.x - - -## How to use -Just run this program on your local device using - - ```bash - python morse_code.py - diff --git a/Morse Code/main.py b/Morse Code/main.py deleted file mode 100644 index a5ac817..0000000 --- a/Morse Code/main.py +++ /dev/null @@ -1,57 +0,0 @@ -morse_code_dict = { - 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', - 'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---', - 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', - 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', - 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', - 'Z': '--..', '1': '.----', '2': '..---', '3': '...--', - '4': '....-', '5': '.....', '6': '-....', '7': '--...', - '8': '---..', '9': '----.', '0': '-----', ' ': '/' -} - -def encode_to_morse(text): - try: - return ' '.join(morse_code_dict[char] for char in text.upper() if char in morse_code_dict) - except KeyError: - print("Error: Input contains unsupported characters.") - -def decode_from_morse(morse_code): - try: - morse_words = morse_code.split(' / ') - decoded_message = '' - for word in morse_words: - decoded_message += ''.join( - [char for morse_char in word.split() for char, code in morse_code_dict.items() if code == morse_char] - ) + ' ' - return decoded_message.strip() - except Exception as e: - print("Error during decoding:", e) - -def menu(): - while True: - print("\nMorse Code Encoder/Decoder") - print("1. Encode Text to Morse Code") - print("2. Decode Morse Code to Text") - print("3. Exit") - - choice = input("Choose an option (1-3): ") - - if choice == '1': - text = input("Enter text to encode: ") - encoded = encode_to_morse(text) - print("Encoded Morse Code:", encoded) - - elif choice == '2': - morse_code = input("Enter Morse code to decode (use '/' for spaces): ") - decoded = decode_from_morse(morse_code) - print("Decoded Text:", decoded) - - elif choice == '3': - print("Exiting program.") - break - - else: - print("Invalid choice, please try again.") - -if __name__ == "__main__": - menu() From edef9da422dc07359d5c2ded0b2f8dccda9ea20c Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Sat, 12 Oct 2024 12:30:18 +0530 Subject: [PATCH 5/6] minor improvements --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 2c70c5f..91aadc7 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,6 @@ More information on contributing and the general code of conduct for discussion | Mail Sender | [Mail Sender](https://github.com/DhanushNehru/Python-Scripts/tree/master/Mail%20Sender) | Sends an email. | | Merge Two Images | [Merge Two Images](https://github.com/DhanushNehru/Python-Scripts/tree/master/Merge%20Two%20Images) | Merges two images horizontally or vertically. | | Mouse mover | [Mouse mover](https://github.com/DhanushNehru/Python-Scripts/tree/master/Mouse%20Mover) | Moves your mouse every 15 seconds. | -| Morse Code | [Mose Code](https://github.com/DhanushNehru/Python-Scripts/tree/master/Morse%20Code) | Encodes and decodes Morse code. | | No Screensaver | [No Screensaver](https://github.com/DhanushNehru/Python-Scripts/tree/master/No%20Screensaver) | Prevents screensaver from turning on. | | OTP Verification | [OTP Verification](https://github.com/DhanushNehru/Python-Scripts/tree/master/OTP%20%20Verify) | An OTP Verification Checker. | | Password Generator | [Password Generator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Password%20Generator) | Generates a random password. | From d9a8c2fc8c87e5cf7b8af45ee8797f2c73243857 Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Sat, 12 Oct 2024 12:40:50 +0530 Subject: [PATCH 6/6] added Morse code encoder decoder under issue #313 --- Morse Code/README.md | 24 +++++++++++++++++++++++ Morse Code/main.py | 46 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 1 + 3 files changed, 71 insertions(+) create mode 100644 Morse Code/README.md create mode 100644 Morse Code/main.py diff --git a/Morse Code/README.md b/Morse Code/README.md new file mode 100644 index 0000000..bcb5326 --- /dev/null +++ b/Morse Code/README.md @@ -0,0 +1,24 @@ +# Morse Code Encoder/Decoder + +## Overview + +This project provides a simple Python program to encode text into Morse code and decode Morse code back into text. It includes exception handling for invalid characters and offers default values when no input is provided. + +## Features + +- **Encoding**: Convert plain text into Morse code. +- **Decoding**: Convert Morse code back into plain text. +- **Exception Handling**: Handles invalid characters gracefully. +- **Default Values**: If no input is provided, it uses default values ('SOS' for encoding and '... --- ...' for decoding). + +## Installation + +1. Ensure you have Python installed on your machine. +2. Download the `morse_code.py` file. + +## Usage + +Run the script directly from the command line: + +```bash +python morse_code.py diff --git a/Morse Code/main.py b/Morse Code/main.py new file mode 100644 index 0000000..fdcad69 --- /dev/null +++ b/Morse Code/main.py @@ -0,0 +1,46 @@ +class MorseCode: + # Morse code dictionary + morse_dict = { + 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', + 'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---', + 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', + 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', + 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', + 'Z': '--..', '1': '.----', '2': '..---', '3': '...--', + '4': '....-', '5': '.....', '6': '-....', '7': '--...', + '8': '---..', '9': '----.', '0': '-----', ' ': '/' + } + + @classmethod + def encode(cls, text=""): + """Encodes a given text into Morse code.""" + if not text: + text = "SOS" # Default value if no input is provided + try: + return ' '.join(cls.morse_dict[char.upper()] for char in text) + except KeyError as e: + print(f"Error: Character '{e.args[0]}' cannot be encoded in Morse code.") + return None + + @classmethod + def decode(cls, morse_code=""): + """Decodes a given Morse code into plain text.""" + if not morse_code: + morse_code = "... --- ..." # Default value if no input is provided + try: + reverse_dict = {v: k for k, v in cls.morse_dict.items()} + return ''.join(reverse_dict[code] for code in morse_code.split()) + except KeyError as e: + print(f"Error: Morse code '{e.args[0]}' cannot be decoded.") + return None + + +if __name__ == "__main__": + # Example usage + text = input("Enter text to encode (leave blank for default 'SOS'): ") + morse_code = MorseCode.encode(text) + print(f"Morse Code: {morse_code}") + + morse_input = input("Enter Morse code to decode (leave blank for default '... --- ...'): ") + decoded_text = MorseCode.decode(morse_input) + print(f"Decoded Text: {decoded_text}") diff --git a/README.md b/README.md index 91aadc7..2c70c5f 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ More information on contributing and the general code of conduct for discussion | Mail Sender | [Mail Sender](https://github.com/DhanushNehru/Python-Scripts/tree/master/Mail%20Sender) | Sends an email. | | Merge Two Images | [Merge Two Images](https://github.com/DhanushNehru/Python-Scripts/tree/master/Merge%20Two%20Images) | Merges two images horizontally or vertically. | | Mouse mover | [Mouse mover](https://github.com/DhanushNehru/Python-Scripts/tree/master/Mouse%20Mover) | Moves your mouse every 15 seconds. | +| Morse Code | [Mose Code](https://github.com/DhanushNehru/Python-Scripts/tree/master/Morse%20Code) | Encodes and decodes Morse code. | | No Screensaver | [No Screensaver](https://github.com/DhanushNehru/Python-Scripts/tree/master/No%20Screensaver) | Prevents screensaver from turning on. | | OTP Verification | [OTP Verification](https://github.com/DhanushNehru/Python-Scripts/tree/master/OTP%20%20Verify) | An OTP Verification Checker. | | Password Generator | [Password Generator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Password%20Generator) | Generates a random password. |