在PIL中调整大小后将图像上传到Imgur [英] Upload Image To Imgur After Resizeing In PIL

查看:124
本文介绍了在PIL中调整大小后将图像上传到Imgur的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,该脚本将从链接中获取图像.然后,将使用PIL模块调整图像大小,并使用pyimgur将图像上传到Imgur.我不想将映像保存在磁盘上,而是在内存中处理映像,然后将其从内存上载到Imgur. 脚本:

I am writing a script which will get an image from a link. Then the image will be resized using the PIL module and the uploaded to Imgur using pyimgur. I dont want to save the image on disk, instead manipulate the image in memory and then upload it from memory to Imgur. The Script:

from pyimgur import Imgur
import cStringIO
import requests
from PIL import Image

LINK = "http://pngimg.com/upload/cat_PNG106.png"
CLIENT_ID = '29619ae5d125ae6'
im = Imgur(CLIENT_ID)

def _upload_image(img, title):
    uploaded_image = im.upload_image(img, title=title)
    return uploaded_image.link

def _resize_image(width, height, link):
    #Retrieve our source image from a URL
    fp = requests.get(link)
    #Load the URL data into an image
    img = cStringIO.StringIO(fp.content)
    im = Image.open(img)
    #Resize the image
    im2 = im.resize((width, height), Image.NEAREST)
    #saving the image into a cStringIO object to avoid writing to disk
    out_im2 = cStringIO.StringIO()
    im2.save(out_im2, 'png')
    return out_im2.getvalue()

运行此脚本时,出现以下错误:TypeError: file() argument 1 must be encoded string without NULL bytes, not str 有人在想解决方案吗?

When I run this script I get this error: TypeError: file() argument 1 must be encoded string without NULL bytes, not str Anyone has a solution in mind?

推荐答案

它看起来与

It looks like the same problem as this, and the solution is to use StringIO.

搜索此类问题的常见技巧是使用错误消息/字符串的通用部分进行搜索.

A common tip for searching such issues is to search using the generic part of the error message/string.

这篇关于在PIL中调整大小后将图像上传到Imgur的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆