简体   繁体   English

Python,如何异步保存 PIL 图像?

[英]Python, how can I asynchronously save the PIL images?

For asynchronous file saving, I can use aiofiles library.对于异步文件保存,我可以使用aiofiles库。

To use aiofiles library I'd have to do something like that:要使用aiofiles库,我必须这样做:

async with aiofiles.open(path, "wb") as file:
   await file.write(data)

How can I asynchronously save the PIL images?如何异步保存 PIL 图像? Even if I use Image.tobytes function to save it with file.write(data) , the saved image isn't correct.即使我使用Image.tobytes function 用file.write(data)保存它,保存的图像也不正确。

So how can I asynchronously save a PIL image?那么如何异步保存 PIL 图像呢?

Thanks to the comment posted by @MarkSetchell I managed to find the solution.感谢@MarkSetchell 发表的评论,我设法找到了解决方案。

async def save_image(path: str, image: memoryview) -> None:
    async with aiofiles.open(path, "wb") as file:
        await file.write(image)


image = Image.open(...)
buffer = BytesIO()
image.save(buffer, format="JPEG")

await save_image('./some/path', buffer.getbuffer())

I don't know how much speed one can gain, but in my case, I'm able to run some data processing code, data downloading code, and image saving code concurrently which gives me a speed up.我不知道一个人可以获得多少速度,但就我而言,我能够同时运行一些数据处理代码、数据下载代码和图像保存代码,这让我加快了速度。

@CrazyChucky, thanks for point out the async sync. @CrazyChucky,感谢您指出异步同步。 I worked again on this我再次为此工作

from fastapi import File,UploadFile
from PIL import Image,ImageFont, ImageDraw
from io import BytesIO

@router.post("/users/update_user_profile")
async def upload_file(self, image_file : UploadFile = File(None)):         
   out_image_name = 'image_name'+pathlib.Path(image_file.filename).suffix    
   out_image_path = f"images/user/{out_image_name}"
   request_object_content = await image_file.read()
   photo = Image.open(BytesIO(request_object_content)) 
   # make the image editable
   drawing = ImageDraw.Draw(photo)
   black = (3, 8, 12)   
   drawing.text((0,0), 'Your_text', fill=black)#, font=font)
  
   # Create a buffer to hold the bytes
   buf = BytesIO()

   # Save the image as jpeg to the buffer
   photo.save(buf, format='JPEG')#    

   async with aiofiles.open(out_image_path, 'wb') as out_file:       
       outContent = await out_file.write(buf.getbuffer())  # async write    

  return out_image_name 

I referred @Karol suggestion.我提到了@Karol 的建议。 Check now, this is for fastapi async method现在检查,这是用于 fastapi 异步方法的

In my case, following worked ( using python fastapi)就我而言,以下工作(使用 python fastapi)

from fastapi import File,UploadFile
from PIL import Image,ImageFont, ImageDraw
from io import BytesIO

async def upload_file(image_file : UploadFile = File(None)):       
    out_image_name = str(123)+pathlib.Path(image_file.filename).suffix 
    out_image_path = f"images/user/{out_image_name}"
    request_object_content = await image_file.read()
    photo = Image.open(BytesIO(request_object_content)) 
    # make the image editable
    drawing = ImageDraw.Draw(photo)
    black = (3, 8, 12)
    # font = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 40)
    drawing.text((0,0), 'text_to_water_mark', fill=black)#, font=font)
    photo.save(out_image_path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM