-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTMDBlogo.py
282 lines (234 loc) · 10.9 KB
/
TMDBlogo.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import requests
from PIL import Image, ImageDraw, ImageFont, ImageFilter
from io import BytesIO
import os
import shutil
from urllib.request import urlopen
import textwrap
# Base URL for the API
url = "https://api.themoviedb.org/3/"
# Set your TMDB API Read Access Token key here
headers = {
"accept": "application/json",
"Authorization": "Bearer XXXX"
}
# The font used
truetype_url = 'https://github.com/googlefonts/roboto/raw/main/src/hinted/Roboto-Light.ttf'
# Endpoint for trending shows
trending_movies_url = f'{url}trending/movie/week?language=en-US'
trending_tvshows_url = f'{url}trending/tv/week?language=en-US'
# Fetching trending movies
trending_movies_response = requests.get(trending_movies_url, headers=headers)
trending_movies = trending_movies_response.json()
# Fetching trending TV shows
trending_tvshows_response = requests.get(trending_tvshows_url, headers=headers)
trending_tvshows = trending_tvshows_response.json()
# Fetching genres for movies
genres_url = f'{url}genre/movie/list?language=en-US'
genres_response = requests.get(genres_url, headers=headers)
genres_data = genres_response.json()
movie_genres = {genre['id']: genre['name'] for genre in genres_data.get('genres', [])}
# Fetching genres for TV shows
genres_url = f'{url}genre/tv/list?language=en-US'
genres_response = requests.get(genres_url, headers=headers)
genres_data = genres_response.json()
tv_genres = {genre['id']: genre['name'] for genre in genres_data.get('genres', [])}
# Fetching TV show details
def get_tv_show_details(tv_id):
tv_details_url = f'{url}tv/{tv_id}?language=en-US'
tv_details_response = requests.get(tv_details_url, headers=headers)
return tv_details_response.json()
# Fetching movie details
def get_movie_details(movie_id):
movie_details_url = f'{url}movie/{movie_id}?language=en-US'
movie_details_response = requests.get(movie_details_url, headers=headers)
return movie_details_response.json()
# Create a directory to save the backgrounds
background_dir = "tmdb_backgrounds"
# Clear the contents of the folder
if os.path.exists(background_dir):
shutil.rmtree(background_dir)
os.makedirs(background_dir, exist_ok=True)
#truncate overview
def truncate_overview(overview, max_chars):
if len(overview) > max_chars:
return overview[:max_chars]
else:
return overview
#truncate
def truncate(overview, max_chars):
if len(overview) > max_chars:
return overview[:max_chars-3]
else:
return overview
# resize image
def resize_image(image, height):
ratio = height / image.height
width = int(image.width * ratio)
return image.resize((width, height))
def resize_logo(image, width, height):
# Get the aspect ratio of the image
aspect_ratio = image.width / image.height
# Calculate new width and height to maintain aspect ratio
new_width = width
new_height = int(new_width / aspect_ratio)
# If the calculated height is greater than the desired height,
# recalculate the width to fit the desired height
if new_height > height:
new_height = height
new_width = int(new_height * aspect_ratio)
# Resize the image
resized_img = image.resize((new_width, new_height))
return resized_img
def clean_filename(filename):
# Remove problematic characters from the filename
cleaned_filename = "".join(c if c.isalnum() or c in "._-" else "_" for c in filename)
return cleaned_filename
# Fetch movie or TV show logo in English
def get_logo(media_type, media_id, language="en"):
logo_url = f"{url}{media_type}/{media_id}/images?language={language}"
logo_response = requests.get(logo_url, headers=headers)
logo_data = logo_response.json()
if logo_response.status_code == 200:
# Check if there are any logos available
logos = logo_data.get("logos", [])
if logos:
return logos[0]["file_path"]
return None
def process_image(image_url, title, is_movie, genre, year, rating, duration=None, seasons=None):
# Download the background image with a timeout of 10 seconds
response = requests.get(image_url, timeout=10)
if response.status_code == 200:
# Open the image
image = Image.open(BytesIO(response.content))
# Resize the image to have a width of 1500 pixels while preserving aspect ratio
image = resize_image(image, 1500)
# Open overlay images
bckg = Image.open(os.path.join(os.path.dirname(__file__), "bckg.png"))
overlay = Image.open(os.path.join(os.path.dirname(__file__), "overlay.png"))
tmdblogo = Image.open(os.path.join(os.path.dirname(__file__), "tmdblogo.png"))
# Paste images
bckg.paste(image, (1175, 0))
bckg.paste(overlay, (1175, 0), overlay)
bckg.paste(tmdblogo, (570, 977), tmdblogo)
# Add title text with shadow
draw = ImageDraw.Draw(bckg)
# Text font
font_title = ImageFont.truetype(urlopen(truetype_url), size=190)
font_overview = ImageFont.truetype(urlopen(truetype_url), size=45)
font_custom = ImageFont.truetype(urlopen(truetype_url), size=45)
# Text color
shadow_color = "black"
main_color = "white"
overview_color = (150, 150, 150) # Grey color for the summary
metadata_color = "white"
# Text position
title_position = (200, 520)
overview_position = (210, 830)
shadow_offset = 2
info_position = (210, 750) # Adjusted position for logo and info
custom_position = (210, 970)
# Wrap overview text
wrapped_overview = "\n".join(textwrap.wrap(overview, width=70, initial_indent="", subsequent_indent="", expand_tabs=True, tabsize=8, replace_whitespace=True, fix_sentence_endings=False, break_long_words=True, break_on_hyphens=True, drop_whitespace=True, max_lines=2, placeholder=" ..."))
# Draw Overview for info
draw.text((overview_position[0] + shadow_offset, overview_position[1] + shadow_offset), wrapped_overview, font=font_overview, fill=shadow_color)
draw.text(overview_position, wrapped_overview, font=font_overview, fill=metadata_color)
# Determine genre text and additional info
if is_movie:
genre_text = genre
additional_info = f"{duration}"
else:
genre_text = genre
additional_info = f"{seasons} {'Season' if seasons == 1 else 'Seasons'}"
rating_text = "TMDB: " + str(rating)
year_text = truncate(str(year), 7)
info_text = f"{genre_text} • {year_text} • {additional_info} • {rating_text}"
# Draw metadata
draw.text((info_position[0] + shadow_offset, info_position[1] + shadow_offset), info_text, font=font_overview, fill=shadow_color)
draw.text(info_position, info_text, font=font_overview, fill=overview_color)
# Get logo image URL
if is_movie:
logo_path = get_logo("movie", movie['id'], language="en")
else:
logo_path = get_logo("tv", tvshow['id'], language="en")
logo_drawn = False # Flag to track if logo is drawn
if logo_path:
logo_url = f"https://image.tmdb.org/t/p/original{logo_path}"
logo_response = requests.get(logo_url)
if logo_response.status_code == 200:
try:
logo_image = Image.open(BytesIO(logo_response.content))
# Resize the logo image to fit within a box while maintaining aspect ratio
logo_image = resize_logo(logo_image, 1200, 600)
logo_position = (210, info_position[1] - logo_image.height - 25) # Position for logo
logo_image = logo_image.convert('RGBA')
# Paste the logo onto the image
bckg.paste(logo_image, logo_position, logo_image)
logo_drawn = True # Logo was successfully drawn
except Exception as e:
print(f"Failed to draw logo for {title}: {e}")
if not logo_drawn:
# Draw title text if logo is not available or failed to draw
draw.text((title_position[0] + shadow_offset, title_position[1] + shadow_offset), title, font=font_title, fill=shadow_color)
draw.text(title_position, title, font=font_title, fill=main_color)
# Draw custom text
draw.text((custom_position[0] + shadow_offset, custom_position[1] + shadow_offset), custom_text, font=font_custom, fill=shadow_color)
draw.text(custom_position, custom_text, font=font_custom, fill=metadata_color)
# Save the resized image
filename = os.path.join(background_dir, f"{clean_filename(title)}.jpg")
bckg = bckg.convert('RGB')
bckg.save(filename)
print(f"Image saved: {filename}")
else:
print(f"Failed to download background for {title}")
# Process each trending movie
for movie in trending_movies.get('results', []):
# Extract movie details
title = movie['title']
overview = movie['overview']
year = movie['release_date']
rating = round(movie['vote_average'],1)
genre = ', '.join([movie_genres[genre_id] for genre_id in movie['genre_ids']])
# Fetch additional movie details
movie_details = get_movie_details(movie['id'])
duration = movie_details.get('runtime', 0)
# Format duration as hours and minutes
if duration:
hours = duration // 60
minutes = duration % 60
duration = f"{hours}h{minutes}min"
else:
duration = "N/A"
# Check if backdrop image is available
backdrop_path = movie['backdrop_path']
custom_text = "Now Trending on"
if backdrop_path:
# Construct image URL
image_url = f"https://image.tmdb.org/t/p/original{backdrop_path}"
# Process the image
process_image(image_url, title, is_movie=True, genre=genre, year=year, rating=rating, duration=duration)
else:
# Print error message if no backdrop image found
print(f"No backdrop image found for {title}")
# Process trending TV shows
for tvshow in trending_tvshows.get('results', []):
# Extract TV show details
title = truncate_overview(tvshow['name'],38)
overview = tvshow['overview']
year = tvshow['first_air_date']
rating = round(tvshow['vote_average'],1)
genre = ', '.join([tv_genres[genre_id] for genre_id in tvshow['genre_ids']])
# Fetch additional TV show details
tv_details = get_tv_show_details(tvshow['id'])
seasons = tv_details.get('number_of_seasons', 0)
# Check if backdrop image is available
backdrop_path = tvshow['backdrop_path']
custom_text = "Now Trending on"
if backdrop_path:
# Construct image URL
image_url = f"https://image.tmdb.org/t/p/original{backdrop_path}"
# Process the image
process_image(image_url, title, is_movie=False, genre=genre, year=year, rating=rating, seasons=seasons)
else:
# Print error message if no backdrop image found
print(f"No backdrop image found for {title}")