-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.py
47 lines (35 loc) · 1.44 KB
/
image.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
37
38
39
40
41
42
43
44
45
46
47
import io
import aiohttp
import asyncio
import aiofiles
from PIL import Image
cid='52H'
secrets='EwLd'
baseurl='https://khalbank.sirv.com'
files=f'{baseurl}/v2/files'
uploadurl=f'{files}/upload?filename=/avatar'
deleteurl=f'{files}/delete?filename=/avatar'
async def get_token():
hdr = {'content-type': "application/json"}
payload={"clientId": cid,"clientSecret":secrets}
async with aiohttp.ClientSession(headers=hdr) as session:
async with session.post(f'{baseurl}/token',json=payload) as resp:
return (await resp.json())['token']
async def image_upload(token,name):
hdr = {'content-type': "application/json",'authorization': "Bearer "+token}
async with aiofiles.open(f'./avatar/{name}', mode='rb') as f:
payload = await f.read()
async with aiohttp.ClientSession(headers=hdr) as session:
await session.post(f'{uploadurl}/{name}', data=payload)
async def image_delete(token,name):
hdr = {'content-type': "application/json",'authorization': "Bearer "+token}
async with aiohttp.ClientSession(headers=hdr) as session:
await session.post(f'{deleteurl}/{name}')
async def byt_img(byt,filename):
image = Image.open(io.BytesIO(byt))
image = image.convert('RGB')
image.save(f'./avatar/{filename}','jpeg',optimize = True,quality = 40)
async def readimage(filename):
async with aiofiles.open(f'./avatar/{filename}', mode='rb') as f:
byt = await f.read()
return byt