-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgUpload.py
36 lines (29 loc) · 1.12 KB
/
imgUpload.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
33
34
35
36
import requests
from PIL import Image
import io
ACCESS_TOKEN= # YOUR_ACCESS_TOKEN
def pil_to_binary(pil_img, format="JPEG"):
if pil_img.mode == "RGBA" and format == "JPEG":
pil_img = pil_img.convert("RGB")
buffered = io.BytesIO()
pil_img.save(buffered, format=format)
return buffered.getvalue()
def singleUpload(img, description):
headers = {'Authorization': "Bearer {}".format(ACCESS_TOKEN)}
img_binary = pil_to_binary(img)
files = {'imagedata': ('image.jpg', img_binary, 'image/jpeg')}
response = requests.post("https://upload.gyazo.com/api/upload" , headers=headers, files=files, data={"desc": description})
response.raise_for_status()
# JSONデータの取得
response_data = response.json()
# URLの存在チェックと表示
if "url" in response_data:
url = response_data["url"]
return url
else:
print("URL not found in response data.")
if __name__ == "__main__":
apple_image1 = Image.open("./images/banana.png")
description = "神秘的なバナナ"
upload_url = singleUpload(apple_image1, description)
print(upload_url)