姜戈。保存在 ImageField 上的推文图片
Django. Tweet image saved on ImageField
我想发布保存在模特 ImageField
上的图片。我正在使用 tweepy
,但我愿意接受任何有效的方法。
这是我试过的:
第一个:
api = tweepy.API(auth)
api.update_with_media(filename=model_object.image.name, status=text, file=model_object.image)
error: TweepError: Invalid file type for image: None
然后:
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, in update_with_media
headers, post_data = API._pack_image(filename, 3072, form_field='media[]', f=f) File
"/home/alejandro/Proyectos/criptohisoka/local/lib/python2.7/site-packages/tweepy/api.py",
line 1311, in _pack_image
f.seek(0, 2) # Seek to end of file TypeError: seek() takes exactly 2 arguments (3 given)
最后:
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: Invalid file type for image: None
我不确定 update_with_media
方法需要什么文件。这里是相关的tweepy
source code and here are the docs.
由于 upload_with_media
端点已被弃用,请参阅 here,我建议您使用以下方法:
使用文件的绝对路径
media_ids = api.media_upload(filename=model_object.image.file.name)
Twitter API 将使用缓存在 media_ids
变量中的长整数进行响应。
最后,使用 update_status
端点:
params = {'status': 'chop chop chop', 'media_ids': [media_ids.media_id_string]}
response = api.update_status(**params)
参考tweepy中的方法定义,以及他们在推特中的对应关系api:
我想发布保存在模特 ImageField
上的图片。我正在使用 tweepy
,但我愿意接受任何有效的方法。
这是我试过的:
第一个:
api = tweepy.API(auth)
api.update_with_media(filename=model_object.image.name, status=text, file=model_object.image)
error: TweepError: Invalid file type for image: None
然后:
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, in update_with_media headers, post_data = API._pack_image(filename, 3072, form_field='media[]', f=f) File "/home/alejandro/Proyectos/criptohisoka/local/lib/python2.7/site-packages/tweepy/api.py", line 1311, in _pack_image f.seek(0, 2) # Seek to end of file TypeError: seek() takes exactly 2 arguments (3 given)
最后:
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: Invalid file type for image: None
我不确定 update_with_media
方法需要什么文件。这里是相关的tweepy
source code and here are the docs.
由于 upload_with_media
端点已被弃用,请参阅 here,我建议您使用以下方法:
使用文件的绝对路径
media_ids = api.media_upload(filename=model_object.image.file.name)
Twitter API 将使用缓存在 media_ids
变量中的长整数进行响应。
最后,使用 update_status
端点:
params = {'status': 'chop chop chop', 'media_ids': [media_ids.media_id_string]}
response = api.update_status(**params)
参考tweepy中的方法定义,以及他们在推特中的对应关系api: