I use the flask
framework, and I use pillow
for image processing.
Generally uploading is done in a loop files
, and then one by one file.save()
I hope to execute after
save is completed The compression logic of pillow
.
But it seems that save
is an I/O operation and there is delay. If pillow
is called directly under file.save()
#Image.open, an error will occur because the image data has not been written to the image.
You see the save operation is not asynchronous
Update
copyfileobj is a blocking operation
In fact, for this type of image processing, it is better to directly use Alibaba Cloud's OSS or Qiniu and other similar storage functions. Directly upload the image to OOS, and then call a special suffix for specified image processing. In the future, you will also access OSS for processing. the address of. This can not only avoid the load of using your own server to process images, but also reduce the access pressure, which is also very beneficial to reducing the complexity of the program.
Please take a look at the fp parameter of Image.open. You can also use A filename (string), pathlib.Path object or a file object PIL.Image.open(fp, mode='r')
You can just pass file to Image.open(file) directly!