Advertisement
Thus8478

catspiration.py

Feb 21st, 2025
347
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | Source Code | 0 1
  1. import requests
  2. from PIL import Image, ImageDraw, ImageFont
  3. from io import BytesIO
  4. from datetime import datetime
  5. import textwrap
  6. import os
  7.  
  8. def get_cat_image():
  9.     """Fetch a random cat image from TheCatAPI"""
  10.     response = requests.get('https://api.thecatapi.com/v1/images/search')
  11.     data = response.json()
  12.     image_url = data[0]['url']
  13.    
  14.     # Download the image
  15.     img_response = requests.get(image_url)
  16.     img = Image.open(BytesIO(img_response.content))
  17.     return img
  18.  
  19. def get_quote():
  20.     """Fetch a random quote from ZenQuotes API"""
  21.     response = requests.get('https://zenquotes.io/api/random')
  22.     data = response.json()
  23.     quote = data[0]['q']
  24.     author = data[0]['a']
  25.     return f'"{quote}"\n- {author}'
  26.  
  27. def create_wallpaper():
  28.     # Check if wallpaper already exists for today
  29.     today = datetime.now().strftime("%Y%m%d")
  30.     filename = f'wallpaper_{today}.jpg'
  31.    
  32.     if os.path.exists(f'/your/path/here/{filename}'):
  33.         print(f"Wallpaper for {today} already exists!")
  34.         return filename
  35.  
  36.     # Get resources
  37.     img = get_cat_image()
  38.     quote = get_quote()
  39.    
  40.     # Resize image to maintain aspect ratio but ensure minimum dimensions
  41.     target_width = 1920
  42.     target_height = 1080
  43.    
  44.     # Calculate new dimensions maintaining aspect ratio
  45.     ratio = min(target_width/img.width, target_height/img.height)
  46.     new_width = int(img.width * ratio)
  47.     new_height = int(img.height * ratio)
  48.    
  49.     # Resize image
  50.     img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
  51.    
  52.     # Create new image with target dimensions
  53.     wallpaper = Image.new('RGB', (target_width, target_height), 'black')
  54.    
  55.     # Paste resized image in center
  56.     offset_x = (target_width - new_width) // 2
  57.     offset_y = (target_height - new_height) // 2
  58.     wallpaper.paste(img, (offset_x, offset_y))
  59.    
  60.     # Add text
  61.     draw = ImageDraw.Draw(wallpaper)
  62.    
  63.     try:
  64.         font = ImageFont.truetype("DejaVuSans.ttf", 40)
  65.     except Exception as e:
  66.         print(e)
  67.         font = ImageFont.load_default()
  68.    
  69.     wrapper = textwrap.TextWrapper(width=100)  # Limit characters per line
  70.     wrapped_quote = wrapper.fill(quote)
  71.    
  72.     # Calculate text position (centered, in the bottom third)
  73.     text_bbox = draw.textbbox((0, 0), wrapped_quote, font=font)
  74.     text_width = text_bbox[2] - text_bbox[0]
  75.     text_height = text_bbox[3] - text_bbox[1]
  76.    
  77.     text_x = (target_width - text_width) // 2
  78.     text_y = target_height - text_height - 100  # 100 pixels from bottom
  79.    
  80.     # Draw text with thick outline for better visibility
  81.     outline_color = 'black'
  82.     text_color = 'white'
  83.     outline_width = 3
  84.    
  85.     # Draw text outline
  86.     for adj in range(-outline_width, outline_width+1):
  87.         for opp in range(-outline_width, outline_width+1):
  88.             if adj != 0 or opp != 0:
  89.                 draw.text((text_x+adj, text_y+opp), wrapped_quote, font=font, fill=outline_color)
  90.    
  91.     # Draw main text
  92.     draw.text((text_x, text_y), wrapped_quote, font=font, fill=text_color)
  93.    
  94.     wallpaper.save(f'/your/path/here/{filename}', quality=95)
  95.     return filename
  96.  
  97. if __name__ == "__main__":
  98.     try:
  99.         filename = create_wallpaper()
  100.         print(f"Successfully created wallpaper: {filename}")
  101.     except Exception as e:
  102.         print(f"Error creating wallpaper: {e}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement