fdfddf
This commit is contained in:
parent
f4064461d5
commit
be442491f7
4 changed files with 32 additions and 69 deletions
|
@ -9,10 +9,14 @@ CF_KEY = environ.get("CLOUDFLARE_KEY").strip()
|
|||
CF_ZONE = environ.get("CLOUDFLARE_ZONE").strip()
|
||||
imgurkey = environ.get("imgurkey").strip()
|
||||
|
||||
def crop_and_resize(img, resize):
|
||||
|
||||
i = img
|
||||
def upload_file(file=None, resize=None):
|
||||
|
||||
if file: file.save("image.gif")
|
||||
i = IImage.open("image.gif")
|
||||
|
||||
|
||||
if resize:
|
||||
org_ratio = i.width / i.height
|
||||
new_ratio = resize[0] / resize[1]
|
||||
|
||||
|
@ -25,15 +29,9 @@ def crop_and_resize(img, resize):
|
|||
box = ((i.width // 2) - (crop_width // 2), 0,
|
||||
(i.width // 2) + (crop_width // 2), i.height)
|
||||
|
||||
return i.resize(resize, box=box)
|
||||
i = i.resize(resize, box=box)
|
||||
|
||||
|
||||
def upload_file(file, resize=None):
|
||||
|
||||
if resize:
|
||||
file.save("image.gif")
|
||||
i = IImage.open("image.gif")
|
||||
i = crop_and_resize(i, resize)
|
||||
img = io.BytesIO()
|
||||
i.save(img, format='GIF')
|
||||
req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {imgurkey}"}, data = {'image': base64.b64encode(img.getvalue())})
|
||||
|
@ -42,47 +40,12 @@ def upload_file(file, resize=None):
|
|||
print(req.text)
|
||||
return
|
||||
|
||||
else:
|
||||
req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {imgurkey}"}, data = {'image': base64.b64encode(file.read())})
|
||||
try: resp = req.json()['data']
|
||||
except:
|
||||
print(req.text)
|
||||
return
|
||||
|
||||
try: url = resp['link'].replace(".png", "_d.png").replace(".jpg", "_d.jpg").replace(".jpeg", "_d.jpeg") + "?maxwidth=9999"
|
||||
except Exception as e:
|
||||
print(req.text)
|
||||
return
|
||||
|
||||
new_image = Image(
|
||||
text=url,
|
||||
deletehash=resp["deletehash"],
|
||||
)
|
||||
|
||||
g.db.add(new_image)
|
||||
return(url)
|
||||
|
||||
|
||||
def upload_from_file(filename, resize=None):
|
||||
i = IImage.open(filename)
|
||||
if resize: i = crop_and_resize(i, resize)
|
||||
img = io.BytesIO()
|
||||
i.save(img, format='GIF')
|
||||
req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {imgurkey}"}, data = {'image': base64.b64encode(img.getvalue())})
|
||||
|
||||
try:
|
||||
resp = req.json()['data']
|
||||
url = resp['link'].replace(".png", "_d.png").replace(".jpg", "_d.jpg").replace(".jpeg", "_d.jpeg") + "?maxwidth=9999"
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print(req)
|
||||
print(req.text)
|
||||
return
|
||||
|
||||
new_image = Image(
|
||||
text=url,
|
||||
deletehash=resp["deletehash"],
|
||||
)
|
||||
new_image = Image(text=url, deletehash=resp["deletehash"])
|
||||
|
||||
g.db.add(new_image)
|
||||
return(url)
|
|
@ -1146,7 +1146,7 @@ def multiple_plots(**kwargs):
|
|||
posts_chart.legend(loc='upper left', frameon=True)
|
||||
comments_chart.legend(loc='upper left', frameon=True)
|
||||
|
||||
plt.savefig("multiplot.png")
|
||||
plt.savefig("image.gif")
|
||||
plt.clf()
|
||||
|
||||
return upload_from_file("multiplot.png")
|
||||
return upload_file()
|
|
@ -533,7 +533,7 @@ def thumbs(new_post):
|
|||
for chunk in image_req.iter_content(1024):
|
||||
file.write(chunk)
|
||||
|
||||
post.thumburl = upload_from_file("image.gif", (100, 100))
|
||||
post.thumburl = upload_file(resize=(100, 100))
|
||||
g.db.add(post)
|
||||
g.db.commit()
|
||||
|
||||
|
|
|
@ -330,7 +330,7 @@ def settings_images_profile(v):
|
|||
|
||||
highres = upload_file(request.files["profile"], (100,100))
|
||||
if not highres: abort(400)
|
||||
imageurl = upload_from_file("image.gif")
|
||||
imageurl = upload_file()
|
||||
if not imageurl: abort(400)
|
||||
v.highres = highres
|
||||
v.profileurl = imageurl
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue