-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgbb.py
32 lines (29 loc) · 1.26 KB
/
imgbb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import requests
from config import IMGBB_API_KEY # Import the API key from config.py
def upload_image_to_imgbb(api_key, image_path):
with open(image_path, 'rb') as image_file:
files = {
'image': image_file
}
response = requests.post(f'https://api.imgbb.com/1/upload?key={api_key}', files=files)
if response.status_code == 200:
data = response.json().get('data', {})
print("Upload successful!")
return {
'id': data.get('id'),
'title': data.get('title'),
'url_viewer': data.get('url_viewer'),
'url': data.get('url'), # Direct URL
'display_url': data.get('display_url'),
'width': data.get('width'),
'height': data.get('height'),
'size': data.get('size'),
'delete_url': data.get('delete_url'),
}
else:
print("Upload failed!")
print("Response:", response.json())
return None # Return None if the upload failed
if __name__ == "__main__":
image_path = 'algae1.jpeg' # Your image path
upload_image_to_imgbb(IMGBB_API_KEY, image_path) # Use the imported API key