亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python怎么在上傳圖片后壓縮圖片大小

瀏覽:80日期:2022-07-16 10:24:39

問題描述

我用的是flask框架,圖片處理用的是pillow。

一般上傳都是在循環files,然后逐個file.save()我希望在save完成后,執行pillow的壓縮邏輯。

但是似乎save是一個I/O操作,存在延遲性,如果直接在file.save()下面直接調用pillow的Image.open,會出錯,因為圖片數據還沒有寫入圖片。

咋辦?

問題解答

回答1:

def save(self, dst, buffer_size=16384):'''Save the file to a destination path or file object. If thedestination is a file object you have to close it yourself after thecall. The buffer size is the number of bytes held in memory duringthe 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 copyfileobjclose_dst = Falseif isinstance(dst, string_types): dst = open(dst, ’wb’) close_dst = Truetry: copyfileobj(self.stream, dst, buffer_size)finally: if close_dst:dst.close()

你看save操作不是異步的吖

更新

copyfileobj是個阻塞操作

https://github.com/pallets/we...

回答2:

其實這類圖片處理,直接使用阿里云的OSS或者七牛等類似的存儲功能更好,直接將圖片上傳到OOS中,然后調用特別的后綴進行指定的圖片處理,未來也訪問OSS上處理后的地址。這樣既可以規避用自己服務器處理圖片的負荷,而且也降低了訪問的壓力,對于降低程序的復雜度也是大有好處的。

回答3:

樓主看看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.

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 黄色片免费在线观看 | 1769国产精品免费视频 | 国产精品久久国产三级国电话系列 | 亚洲欧美高清在线 | 久久久久久久999 | 91在线视频免费看 | 亚洲自拍第二页 | 91最新在线播放 | 久久入| 一级黄色免费观看 | 黄色片在线观看免费 | 国产高清视频 | 亚洲国产成人久久综合一 | 97精品国产 | 成人一区二区免费中文字幕 | 麻豆 一区 精品 在线 | 日产免费线路一页二页 | 一级做α爱毛毛片 | 久久久久avav久久久 | 国产在线观看黄 | 国产黄网站 | 国产人成精品免费视频 | 丁香婷婷开心激情深爱五月 | 国产主播精品 | 中文字幕亚洲无线码高清 | 大美香蕉伊在看欧美 | 澳门毛片精品一区二区三区 | 91性视频| 成 人 黄 色 大 片全部 | 加勒比上原亚衣在线播放 | 久久久久琪琪精品色 | 91资源在线视频 | 亚洲欧美日韩国产色另类 | 国产精品第一区第27页 | 7788成年网站免费观看 | 欧美啊啊| 久久91久久91精品免费观看 | 国产在线短视频 | 日韩欧美精品 | 久青草免费视频手机在线观看 | 亚洲色图欧美激情 |