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

如何將圖像保存在內存中并使用 PIL 上傳?

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

問題描述

我對 Python 還很陌生.目前我正在制作一個原型,它可以拍攝一張圖片,從中創建一個縮略圖并將其上傳到 ftp 服務器.

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.

到目前為止,我已經準備好獲取圖像、轉換和調整大小.

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

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

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()

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

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()

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

消息:

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

推薦答案

不要將字符串傳遞給 storbinary.您應該將文件或文件對象(內存映射文件)傳遞給它.此外,這一行應該是 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

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

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

相關文檔推薦

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:“浮動對象不可下標)
主站蜘蛛池模板: 日韩精品免费在线观看 | 99久久婷婷国产综合精品电影 | 一级做a爰片性色毛片 | 中文字幕中文字幕 | 超碰在线国产 | 成人黄色电影在线观看 | 国产精品久久久久久久久久久久 | 国产精品久久久久不卡 | 久久久综合 | 亚洲日韩中文字幕一区 | 中文字幕 国产精品 | h视频在线播放 | 久久久视 | av在线播放免费 | 一二三区av| 天堂一区二区三区 | 国产在线拍偷自揄拍视频 | 欧美精品成人一区二区三区四区 | 国产日韩中文字幕 | 99re视频这里只有精品 | 久久国产麻豆 | 国产高清免费在线 | 在线播放第一页 | 成人乱人乱一区二区三区软件 | 北条麻妃视频在线观看 | 亚州精品成人 | 日本成人在线播放 | 国产免费福利 | 伊人一区 | 一区二区三区av夏目彩春 | 亚洲天堂中文字幕 | 在线色网站 | 国产精品2区 | 日韩在线不卡 | 国产精品久久久久久久久久久久冷 | 99re视频在线观看 | 亚洲三区在线 | 国产精品久久久久久亚洲调教 | 久久亚洲二区 | 国产一区二区三区四区三区四 | 欧美视频中文字幕 |