Skip to content

Commit 1b63796

Browse files
Soutrik DasSoutrik Das
Soutrik Das
authored and
Soutrik Das
committed
Added Image to URL using imgbb (with apikey)
1 parent 5a12f82 commit 1b63796

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Image to URL/uploadimage.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import base64
2+
import requests
3+
import pyperclip as pc
4+
from PIL import ImageGrab, Image
5+
import os
6+
from dotenv import load_dotenv
7+
8+
9+
device = "mac"
10+
# set device to "windows" if you are on windows, "mac" if you are on macOS
11+
# On windows this uses the keyboard library ( which works only on windows )
12+
# On Mac there is an alternative library called pynput but that requires a lot of permissions
13+
# Which frankly even I am not comfortable since there is a better alternative on mac :
14+
# Open Automator > Create New Document > Choose a Type : Quick Action > Search for "Run Shell Script" > Drag it to the right side > Set Workflow receives input : none > Change shell to python3 > Paste this script in there ( after you have configured everything , like the api_key ... )
15+
# Save that Quick Action. Go to Keyboard Shortcuts > Services > General > Assign your preffered shortcut for the Quick Action you just made.
16+
17+
18+
load_dotenv()
19+
dotenv_path = os.path.join(os.path.dirname(__file__), '../.env')
20+
api_key_imgbb = os.environ.get("API_KEY_IMGBB")
21+
# api_key_imgbb = "put your imgbb key here ( if not using .env)"
22+
23+
24+
def upload_imgbb():
25+
img = ImageGrab.grabclipboard() # grab the current clipboard
26+
27+
# check if the item is a non image
28+
if img is None:
29+
print("Last object was not an image")
30+
return
31+
# save the image
32+
img.save('random.png', 'PNG')
33+
34+
# open that image in a fileobject
35+
with open("random.png", "rb") as file:
36+
url = "https://api.imgbb.com/1/upload"
37+
payload = {
38+
"key": api_key_imgbb,
39+
"image": base64.b64encode(file.read()),
40+
}
41+
42+
# send a post request
43+
res = requests.post(url, payload) # get the response here
44+
link = res.json()["data"]["url"]
45+
print(link)
46+
# copy the link into our clipboard
47+
pc.copy(link)
48+
os.remove("random.png")
49+
50+
51+
if device == "windows":
52+
import keyboard
53+
# edit the shortcut (for windows ) here ( check keyboard library documentation/examples )
54+
keyboard.add_hotkey("ctrl+shift+z", upload_imgbb)
55+
keyboard.wait('esc') # pressing esc will exit the background python file
56+
elif device == "mac" or device == "linux":
57+
upload_imgbb()

0 commit comments

Comments
 (0)