1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- # -*- coding: utf-8 -*-
- import io
- import os
- import aiofiles
- import aiohttp
- from PIL import Image
- from utils.fileuploader import ossfile_uploader
- async def crop_img(orgimg, point=(), i=0):
- """
- 切割图片
- """
- img = Image.open(orgimg)
- region = img.crop(point)
- buf = io.BytesIO()
- region.save(buf, format="png")
- new_img = "resource/" + os.path.split(orgimg)[-1].replace(".", f"{i}_crop.")
- remote_url = ossfile_uploader.upload_from_str(new_img, buf.getvalue(),
- {"Content-Type": "image/png"})
- return remote_url
- async def download_remote_img(url):
- """
- 下载远程图片
- """
- name = os.path.split(url)[-1]
- local_img = f"resource/{name}"
- async with aiohttp.request("GET", url) as r:
- content = await r.read()
- async with aiofiles.open(local_img, "wb") as f:
- await f.write(content)
- await f.close()
- return local_img
- # if __name__ == "__main__":
- # loop = asyncio.get_event_loop()
- # loop.run_until_complete(crop_img("/tmp/test.jpg", (10, 10, 300, 300)))
|