国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

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

瀏覽:83日期: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 編程
相關文章:
主站蜘蛛池模板: 欧美在线一级毛片观看 | 亚洲国产日韩成人综合天堂 | 日韩不卡一区二区三区 | 国产欧美日韩综合精品无毒 | 久久久一区二区三区不卡 | 欧美成人私人视频88在线观看 | 欧美成人久久 | 国产亚洲精品九九久在线观看 | 精品一区二区三区四区在线 | 玖草影院 | 久久精品国产亚洲综合色 | 亚洲精品久久99久久一区 | 美女大片高清特黄a大片 | 久久性生大片免费观看性 | 国产www| 亚洲逼 | 欧美日韩在线观看视频 | 狠狠色狠狠色综合久久第一次 | 草久在线观看 | 国产精品亚洲成在人线 | 亚洲成人第一页 | 手机看片1024欧美日韩你懂的 | 视频在线亚洲 | 秀人网私拍福利视频在线 | 成人欧美一区二区三区在线 | 亚洲人成影院午夜网站 | 国产精品成人久久久久久久 | 欧美在线一区二区三区欧美 | 韩国主播19福利视频在线 | 手机看片日韩国产一区二区 | 中文字幕有码在线观看 | 亚洲精品m在线观看 | 国产成人免费观看 | 男女朋友做爽爽爽免费视频网 | 久久久9视频在线观看 | 欧美骚视频 | 男人使劲躁女人视频小v | 亚洲一区二区中文字幕 | 91亚洲精品国产第一区 | 久久网站免费 | 夜鲁夜鲁夜鲁在线观看福利 |