【发布时间】:2016-09-22 05:18:19
【问题描述】:
我想发布保存在模特ImageField 上的图片。我正在使用tweepy,但我愿意接受任何可行的方法。
这是我尝试过的:
第一:
api = tweepy.API(auth)
api.update_with_media(filename=model_object.image.name, status=text, file=model_object.image)
错误:TweepError:图像的文件类型无效:无
那么:
from PIL import Image
api = tweepy.API(auth)
image_file = Image.open(model_object.image)
api.update_with_media(filename=model_object.image.name, status=text, file=image_file)
219,在 update_with_media 中 headers, post_data = API._pack_image(filename, 3072, form_field='media[]', f=f) 文件 “/home/alejandro/Proyectos/criptohisoka/local/lib/python2.7/site-packages/tweepy/api.py”, 第 1311 行,在 _pack_image 中 f.seek(0, 2) # Seek to end of file TypeError: seek() 需要 2 个参数(给定 3 个)
最后:
from PIL import Image
from StringIO import StringIO
api = tweepy.API(auth)
image_file = Image.open(model_object.image)
stringio_obj = StringIO()
image_file.save(stringio_obj, format="JPEG")
api.update_with_media(filename=model_object.image.name, status=text, file=stringio_obj)
TweepError:图像的文件类型无效:无
我不确定update_with_media 方法期望的文件是什么。这是相关的tweepysource code,这是docs。
【问题讨论】:
-
看起来它正在寻找一个 File 对象,所以你的第二个例子应该可以工作。你能用 TypeError 扩展堆栈跟踪吗?看起来问题可能不在于实际调用。
-
@lonewaft 刚刚添加了一些更多的回溯......但没有什么可添加的。对我来说似乎是一个错误。
标签: python django twitter tweepy