1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- # -*- 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
- def crop_img_remote(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
- def crop_img_local(orgimg, point=()):
- """
- 切割图片
- """
- img = Image.open(orgimg)
- region = img.crop(point)
- new_img = "/tmp/" + os.path.split(orgimg)[-1].replace(".", "_crop.")
- region.save(new_img, format="png")
- return new_img
- async def download_remote_img(url):
- """
- 下载远程图片
- """
- name = os.path.split(url)[-1]
- local_img = f"/tmp/{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
|