update example for a post involving image editing

Python 2 support was dropped from Twython, thanks!
In Python3 we actually have to use BytesIO, see https://github.com/python-pillow/Pillow/issues/2205
This commit is contained in:
Hannes 2020-07-17 11:50:06 +02:00 committed by GitHub
parent 02fb35651d
commit 33f46c087e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -59,15 +59,10 @@ with a status update.
.. code-block:: python .. code-block:: python
# Assume you are working with a JPEG # Assuming that you are working with a JPEG
from PIL import Image from PIL import Image
try: from io import BytesIO
# Python 3
from io import StringIO
except ImportError:
# Python 2
from StringIO import StringIO
photo = Image.open('/path/to/file/image.jpg') photo = Image.open('/path/to/file/image.jpg')
@ -76,14 +71,13 @@ with a status update.
height = int((float(photo.size[1]) * float(wpercent))) height = int((float(photo.size[1]) * float(wpercent)))
photo = photo.resize((basewidth, height), Image.ANTIALIAS) photo = photo.resize((basewidth, height), Image.ANTIALIAS)
image_io = StringIO.StringIO() image_io = BytesIO()
photo.save(image_io, format='JPEG') photo.save(image_io, format='JPEG')
# If you do not seek(0), the image will be at the end of the file and # If you do not seek(0), the image will be at the end of the file and
# unable to be read # unable to be read
image_io.seek(0) image_io.seek(0)
response = twitter.upload_media(media=image_io) response = twitter.upload_media(media=image_io)
twitter.update_status(status='Checkout this cool image!', media_ids=[response['media_id']]) twitter.update_status(status='Checkout this cool image!', media_ids=[response['media_id']])