-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
50 lines (42 loc) · 1.97 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#This code uses the tweepy library to authenticate with the Twitter API and retrieve the images from the user's timeline. It then saves the images to a local directory called "images." Next, it creates a list of Image objects for each image that meets certain criteria (in this case, the function meets_criteria is used to filter out images that don't match a certain set of criteria). Finally, it saves the list of frames as a GIF using the save method of the Image class, just like in the previous example.
import tweepy
from PIL import Image
import requests
import os
# Your Twitter API keys and secrets
consumer_key = "YOUR_CONSUMER_KEY"
consumer_secret = "YOUR_CONSUMER_SECRET"
access_token = "YOUR_ACCESS_TOKEN"
access_token_secret = "YOUR_ACCESS_TOKEN_SECRET"
# Authenticate with the Twitter API
auth = tweepy.OAuth1UserHandler(
consumer_key,
consumer_secret,
access_token,
access_token_secret
)
api = tweepy.API(auth)
# The screen name of the Twitter user whose images you want to use
screen_name = "USERNAME"
# Download the images from the user's timeline
if not os.path.exists("images"):
os.makedirs("images")
for status in tweepy.Cursor(api.user_timeline, screen_name=screen_name).items():
if "media" in status.entities:
# Get the first image in the tweet
image_url = status.entities["media"][0]["media_url"]
# Download the image and save it to the "images" directory
response = requests.get(image_url)
open(f"images/{status.id}.jpg", "wb").write(response.content)
# Create the frames for the GIF
frames = []
for file in os.listdir("images"):
# Filter out any images that don't match a certain criteria (e.g. width, height, etc.)
if file.endswith(".jpg") and meets_criteria(file):
frame = Image.open(f"images/{file}")
frames.append(frame)
# Save the GIF
frames[0].save('animation.gif', format='GIF',
append_images=frames[1:],
save_all=True,
duration=1000, loop=0)