def save(self, dst, buffer_size=16384):
"""Save the file to a destination path or file object. If the
destination is a file object you have to close it yourself after the
call. The buffer size is the number of bytes held in memory during
the copy process. It defaults to 16KB.
For secure file saving also have a look at :func:`secure_filename`.
:param dst: a filename or open file object the uploaded file
is saved to.
:param buffer_size: the size of the buffer. This works the same as
the `length` parameter of
:func:`shutil.copyfileobj`.
"""
from shutil import copyfileobj
close_dst = False
if isinstance(dst, string_types):
dst = open(dst, 'wb')
close_dst = True
try:
copyfileobj(self.stream, dst, buffer_size)
finally:
if close_dst:
dst.close()
樓主看看Image.open 的fp參數,也可以A filename (string), pathlib.Path object or a file object PIL.Image.open(fp, mode='r')
你直接傳file給Image.open(file)就可以了吧!
PIL.Image.open(fp, mode='r')
Opens and identifies the given image file.
This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the load() method). See new().
Parameters:
fp – A filename (string), pathlib.Path object or a file object. The file object must implement read(), seek(), and tell() methods, and be opened in binary mode.
mode – The mode. If given, this argument must be “r”.
Returns:
An Image object.
Raises:
IOError – If the file cannot be found, or the image cannot be opened and identified.
你看save操作不是非同步的吖
更新
copyfileobj是個阻塞操作
其實這類圖片處理,直接使用阿里雲的OSS或者七牛等類似的存儲功能更好,直接將圖片上傳到OOS中,然後調用特別的後綴進行指定的圖片處理,未來也訪問OSS上處理後的地址。這樣既可以規避用自己伺服器處理圖片的負荷,而且也降低了存取的壓力,對於降低程式的複雜度也是大有好處的。
樓主看看Image.open 的fp參數,也可以A filename (string), pathlib.Path object or a file object PIL.Image.open(fp, mode='r')
你直接傳file給Image.open(file)就可以了吧!