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

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

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

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

問(wèn)題描述

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

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

但是似乎save是一個(gè)I/O操作,存在延遲性,如果直接在file.save()下面直接調(diào)用pillow的Image.open,會(huì)出錯(cuò),因?yàn)閳D片數(shù)據(jù)還沒(méi)有寫入圖片。

咋辦?

問(wèn)題解答

回答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是個(gè)阻塞操作

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

回答2:

其實(shí)這類圖片處理,直接使用阿里云的OSS或者七牛等類似的存儲(chǔ)功能更好,直接將圖片上傳到OOS中,然后調(diào)用特別的后綴進(jìn)行指定的圖片處理,未來(lái)也訪問(wèn)OSS上處理后的地址。這樣既可以規(guī)避用自己服務(wù)器處理圖片的負(fù)荷,而且也降低了訪問(wèn)的壓力,對(duì)于降低程序的復(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)文章:
主站蜘蛛池模板: 高清一级淫片a级中文字幕 高清一区二区 | 免费久草 | 成人毛片免费看 | 国产一级aaa全黄毛片 | 欧美人成在线 | 91精品久久久久 | 欧美专区一区二区三区 | 偷窥女厕国产在线视频 | 久久综合久久88 | 免费国产成人综合 | 亚洲综合色视频在线观看 | 在线观看国产精品入口 | 欧美黄www免费 | 欧美区在线 | 欧美激情成人网 | 无码免费一区二区三区免费播放 | 国产一区二区三区四区五区 | 毛片高清 | 亚洲午夜久久久久国产 | 亚洲欧美手机在线观看 | 免费又黄又爽又猛大片午夜 | 欧美一级黄 | 精品视频在线免费看 | www.色黄| 日韩欧美在线观看 | a一级网站| 美国一级毛片免费看成人 | 国产三级中文字幕 | 久久久国产精品免费看 | 欧美成人eee在线 | 国产男女免费视频 | 久久久精品久久视频只有精品 | 三级黄色a | 欧美日本高清视频在线观看 | 欧美精品综合一区二区三区 | 欧美成人久久久免费播放 | 国产孕妇做受视频在线观看 | 最刺激黄a大片免费网站 | 国产精品一一在线观看 | 免费在线亚洲 | 亚洲天堂资源网 |