Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from PIL import Image, ImageDraw, ImageFont
- from io import BytesIO
- from datetime import datetime
- import textwrap
- import os
- def get_cat_image():
- """Fetch a random cat image from TheCatAPI"""
- response = requests.get('https://api.thecatapi.com/v1/images/search')
- data = response.json()
- image_url = data[0]['url']
- # Download the image
- img_response = requests.get(image_url)
- img = Image.open(BytesIO(img_response.content))
- return img
- def get_quote():
- """Fetch a random quote from ZenQuotes API"""
- response = requests.get('https://zenquotes.io/api/random')
- data = response.json()
- quote = data[0]['q']
- author = data[0]['a']
- return f'"{quote}"\n- {author}'
- def create_wallpaper():
- # Check if wallpaper already exists for today
- today = datetime.now().strftime("%Y%m%d")
- filename = f'wallpaper_{today}.jpg'
- if os.path.exists(f'/your/path/here/{filename}'):
- print(f"Wallpaper for {today} already exists!")
- return filename
- # Get resources
- img = get_cat_image()
- quote = get_quote()
- # Resize image to maintain aspect ratio but ensure minimum dimensions
- target_width = 1920
- target_height = 1080
- # Calculate new dimensions maintaining aspect ratio
- ratio = min(target_width/img.width, target_height/img.height)
- new_width = int(img.width * ratio)
- new_height = int(img.height * ratio)
- # Resize image
- img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
- # Create new image with target dimensions
- wallpaper = Image.new('RGB', (target_width, target_height), 'black')
- # Paste resized image in center
- offset_x = (target_width - new_width) // 2
- offset_y = (target_height - new_height) // 2
- wallpaper.paste(img, (offset_x, offset_y))
- # Add text
- draw = ImageDraw.Draw(wallpaper)
- try:
- font = ImageFont.truetype("DejaVuSans.ttf", 40)
- except Exception as e:
- print(e)
- font = ImageFont.load_default()
- wrapper = textwrap.TextWrapper(width=100) # Limit characters per line
- wrapped_quote = wrapper.fill(quote)
- # Calculate text position (centered, in the bottom third)
- text_bbox = draw.textbbox((0, 0), wrapped_quote, font=font)
- text_width = text_bbox[2] - text_bbox[0]
- text_height = text_bbox[3] - text_bbox[1]
- text_x = (target_width - text_width) // 2
- text_y = target_height - text_height - 100 # 100 pixels from bottom
- # Draw text with thick outline for better visibility
- outline_color = 'black'
- text_color = 'white'
- outline_width = 3
- # Draw text outline
- for adj in range(-outline_width, outline_width+1):
- for opp in range(-outline_width, outline_width+1):
- if adj != 0 or opp != 0:
- draw.text((text_x+adj, text_y+opp), wrapped_quote, font=font, fill=outline_color)
- # Draw main text
- draw.text((text_x, text_y), wrapped_quote, font=font, fill=text_color)
- wallpaper.save(f'/your/path/here/{filename}', quality=95)
- return filename
- if __name__ == "__main__":
- try:
- filename = create_wallpaper()
- print(f"Successfully created wallpaper: {filename}")
- except Exception as e:
- print(f"Error creating wallpaper: {e}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement