I want to tweet an image saved on a model's ImageField
. I'm using tweepy
, but I'm open to anything that works.
This is what I've tried:
First:
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
Then:
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)
Finally:
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
I'm not sure what the update_with_media
method expects as a file. Here is the relevanttweepy
source code and here are the docs.