我正在编写一个脚本,它将从链接中获取图像。然后,使用PIL
模块调整图像大小,并使用pyimgur
将图像上传到Imgur。我不想将图像保存在磁盘上,而是在内存中操作图像,然后将其从内存上传到Imgur。
剧本:
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
任何人都有解决方案吗?