久久久久久久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))
主站蜘蛛池模板: 欧美日韩国产传媒 | 日韩影音 | 久久国内精品 | 国产美女在线观看 | 亚洲精品中文字幕 | 国产偷录叫床高潮录音 | 午夜久久久 | 日韩在线不卡 | 日韩视频免费看 | 91精品久久久久久久久久 | 欧美一级片在线播放 | 欧美区在线 | 毛片片 | 9191av| 六月婷婷久久 | 欧洲色综合 | 久久精品色欧美aⅴ一区二区 | 成人免费在线网 | 中文字幕二区三区 | 国产成人精品一区二区三区四区 | 久久久精品视频一区二区三区 | 在线观看日韩 | 久久在线免费 | 天天夜碰日日摸日日澡 | 国产精品一区二区久久 | 综合色在线| 亚洲国产精品久久久 | 午夜精品久久久久久不卡欧美一级 | 亚洲综合在线视频 | 欧美网站一区二区 | 久操av在线 | 一区二区高清不卡 | 日韩在线观看一区 | 精品一二区| 在线一级片 | 精品国产网 | 国产精品五月天 | 久久久国产一区二区三区四区小说 | 青青久在线视频 | 精品96久久久久久中文字幕无 | 欧美黄色片在线观看 |