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

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

問題描述

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

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

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

咋辦?

問題解答

回答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中,然后調(diào)用特別的后綴進(jìn)行指定的圖片處理,未來也訪問OSS上處理后的地址。這樣既可以規(guī)避用自己服務(wù)器處理圖片的負(fù)荷,而且也降低了訪問的壓力,對于降低程序的復(fù)雜度也是大有好處的。

回答3:

樓主看看Image.open 的fp參數(shù),也可以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.

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产自产视频在线观看香蕉 | 欧美三级成人观看 | 成人国产亚洲欧美成人综合网 | 久久在现 | 久久久精品2018免费观看 | 天天色资料 | 国产精品小黄鸭一区二区三区 | 99精品亚洲 | 女人被男人狂躁的视频免费 | 黄视频在线免费 | 久久91精品综合国产首页 | 在线观看成年人免费视频 | 久久视频6免费观看视频精品 | 久久精品视频播放 | 久久精品国产国产精品四凭 | 亚洲综合精品一区二区三区中文 | 欧美毛片a级毛片免费观 | 视频色www永久免费 视频免费1区二区三区 | 国产成人禁片免费观看视频 | 国产在线观看网站 | 欧美色影院 | 综合欧美日韩一区二区三区 | a级日本乱理伦片免费入 | 国产精品视频一区麻豆 | 91国偷自产一区二区三区蜜臀 | 亚洲艹 | 毛片在线看免费 | a一级爱做片免费观看欧美 a一级黄 | 丁香天堂网| 1024成人 | 成人午夜私人影院入口 | 精品欧美一区二区vr在线观看 | 色综合天天娱乐综合网 | 在线观看久草视频 | 国产高清在线精品一区αpp | 在线免费观看网站入口在哪 | 国产美女无遮挡免费网站 | 国产精品成人影院 | 成人影院久久久久久影院 | 欧美三级视频在线播放 | 国产成人精品久久二区二区 |