久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何將圖像保存在內(nèi)存中并使用 PIL 上傳?

How to save image in-memory and upload using PIL?(如何將圖像保存在內(nèi)存中并使用 PIL 上傳?)
本文介紹了如何將圖像保存在內(nèi)存中并使用 PIL 上傳?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我對(duì) Python 還很陌生.目前我正在制作一個(gè)原型,它可以拍攝一張圖片,從中創(chuàng)建一個(gè)縮略圖并將其上傳到 ftp 服務(wù)器.

I'm fairly new to Python. Currently I'm making a prototype that takes an image, creates a thumbnail out of it and and uploads it to the ftp server.

到目前為止,我已經(jīng)準(zhǔn)備好獲取圖像、轉(zhuǎn)換和調(diào)整大小.

So far I got the get image, convert and resize part ready.

我遇到的問題是使用 PIL(枕頭)圖像庫(kù)轉(zhuǎn)換圖像的類型與使用 storebinary() 上傳時(shí)可以使用的類型不同

The problem I run into is that using the PIL (pillow) Image library converts the image is a different type than that can be used when uploading using storebinary()

我已經(jīng)嘗試了一些方法,例如使用 StringIO 或 BufferIO 將圖像保存在內(nèi)存中.但是我一直在出錯(cuò).有時(shí)圖像確實(shí)已上傳,但文件似乎為空(0 字節(jié)).

I already tried some approaches like using StringIO or BufferIO to save the image in-memory. But I'm getting errors all the time. Sometimes the image does get uploaded but the file appears to be empty (0 bytes).

這是我正在使用的代碼:

Here is the code I'm working with:

import os
import io
import StringIO
import rawpy
import imageio
import Image
import ftplib

# connection part is working
ftp = ftplib.FTP('bananas.com')
ftp.login(user="banana", passwd="bananas")
ftp.cwd("/public_html/upload")

def convert_raw():
    files = os.listdir("/home/pi/Desktop/photos")

    for file in files:
        if file.endswith(".NEF") or file.endswith(".CR2"):
            raw = rawpy.imread(file)
            rgb = raw.postprocess()
            im = Image.fromarray(rgb)
            size = 1000, 1000
            im.thumbnail(size)

            ftp.storbinary('STOR Obama.jpg', img)
            temp.close()
    ftp.quit()

convert_raw()

我嘗試了什么:

temp = StringIO.StringIO
im.save(temp, format="png")
img = im.tostring()
temp.seek(0)
imgObj = temp.getvalue()

我得到的錯(cuò)誤在于 ftp.storbinary('STOR Obama.jpg', img).

消息:

buf = fp.read(blocksize)
attributeError: 'str' object has no attribute read

推薦答案

不要將字符串傳遞給 storbinary.您應(yīng)該將文件或文件對(duì)象(內(nèi)存映射文件)傳遞給它.此外,這一行應(yīng)該是 temp = StringIO.StringIO().所以:

Do not pass a string to storbinary. You should pass a file or file object (memory-mapped file) to it instead. Also, this line should be temp = StringIO.StringIO(). So:

temp = StringIO.StringIO() # this is a file object
im.save(temp, format="png") # save the content to temp
ftp.storbinary('STOR Obama.jpg', temp) # upload temp

這篇關(guān)于如何將圖像保存在內(nèi)存中并使用 PIL 上傳?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 postgresql 表中)
list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動(dòng)對(duì)象不可下標(biāo))
主站蜘蛛池模板: 国产一区二区久久 | 日韩视频一区在线观看 | 先锋影音资源网站 | 久久鲁视频 | 亚洲免费视频一区二区 | 精品一区二区久久 | 日韩欧美一区二区三区 | 国产高清精品网站 | 国产精品国产三级国产aⅴ原创 | 日韩一区二区三区在线视频 | 国产亚洲欧美日韩精品一区二区三区 | 免费看av大片 | 91在线看| 粉嫩av | 久久国产精品视频 | 久久激情五月丁香伊人 | 久久久免费少妇高潮毛片 | 日本视频免费观看 | 亚洲一区在线免费观看 | 日本久久久一区二区三区 | 一区二区三区四区不卡视频 | 91综合网| 国产一区在线免费 | 亚洲精品免费观看 | 久久综合一区 | 欧美成人免费 | 久久九精品 | 综合久久综合久久 | 欧美成人免费在线视频 | 亚洲一区二区在线播放 | 网站一区二区三区 | 日韩精品视频在线播放 | 精品区 | 精国产品一区二区三区四季综 | 国产精品久久久久婷婷二区次 | 亚洲欧美中文日韩在线v日本 | 欧美成人综合 | 成人在线一级片 | 99精品在线观看 | 久久综合伊人一区二区三 | 一区二区中文字幕 |