Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a python script that uploads an image to imgur via keyboard shortcut. #314

Merged
merged 9 commits into from
Oct 13, 2024
1 change: 1 addition & 0 deletions image_uploader/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
secret.env
23 changes: 23 additions & 0 deletions image_uploader/README.md
Original file line number Diff line number Diff line change
@@ -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)
45 changes: 45 additions & 0 deletions image_uploader/main.py
Original file line number Diff line number Diff line change
@@ -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')