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

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

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

問(wèn)題描述

我對(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.

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

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)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(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 的開(kāi)頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒(méi)有插入到 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))
主站蜘蛛池模板: 国产精品中文字幕在线观看 | 2022精品国偷自产免费观看 | 男人天堂色 | 欧美日韩一区二区三区不卡视频 | 日韩一区二区三区视频在线播放 | 日韩色视频 | 亚洲国产一区二区视频 | 久久精品欧美视频 | 一区二区在线不卡 | 亚洲精品视频导航 | 伊人久操 | 成人免费一区二区三区视频网站 | 噜噜噜噜狠狠狠7777视频 | 成人国产一区二区三区精品麻豆 | 日韩一区二区三区视频 | 国产高清视频 | 日韩成人在线观看 | 色视频www在线播放国产人成 | 黄在线免费观看 | 国产午夜三级一区二区三 | 久久久精品网 | 麻豆精品久久 | 亚洲高清在线观看 | 天天干天天爱天天 | 亚洲国产精品网站 | 麻豆视频在线免费看 | 99久久精品免费看国产四区 | 欧美一区二区三区一在线观看 | 精品免费av | 亚洲成人精品 | 超碰成人免费观看 | 国产91色在线 | 亚洲 | 亚洲精品乱码久久久久久蜜桃91 | 亚洲一区二区三区国产 | 免费在线观看黄视频 | 日韩欧美亚洲综合 | 国产毛片av| 六月成人网 | 粉嫩一区二区三区性色av | 337p日本欧洲亚洲大胆鲁鲁 | 久久精品二区 |