User Tools

Site Tools


obtener_valores_de_una_api_y_escribirlos_sobre_una_imagen_jpg

"No Mama" Dollar Meme Generator

Objective: This script generates a humorous meme by overlaying the latest unofficial dollar exchange rate value onto an image.

Code

main.py
import requests
import json
import csv
from PIL import Image, ImageFont, ImageDraw
 
def main():
    headers = {'content-type': "application/json"}
    url = "https://api.bluelytics.com.ar/v2/latest"
    response = requests.request("GET", url, params="", headers=headers, timeout=20)
    imgfont = ImageFont.truetype("DejaVuSans-Bold.ttf", 22)
    #today = time.strftime("%d/%m/%Y")
    resultFilePath = "/home/facundo/noMama/latest.jpg"
 
    if response.status_code == requests.codes.ok:
        #start_time = time.time()
 
        # acomoda los datos del json
        data = json.loads(response.text)
        blue = data.get("blue")
 
        # Escribe literalmente lo que dice skinner usando el valor de blue_avg sobre la imagen final y guarda el archivo resultante en la carpeta de trabajo
        mensaje = " NO MAMA!!! \n NO DIVIDAS TU SUELDO POR $" + str(round(blue.get("value_avg")))
        background = Image.open("template.jpeg")
        image_editable = ImageDraw.Draw(background)
        image_editable.text((10,400), mensaje, (255, 255, 0), font=imgfont)
        background.save(resultFilePath)
    else:
        print("falla la consulta a la api")
 
if __name__ == '__main__':
    main()

Code Explanation:

1. Imports: The script imports necessary modules such as `requests`, `json`, `csv`, and `PIL` (Python Imaging Library) for handling HTTP requests, JSON data, CSV files, and image manipulation, respectively.

2. Function Definition: The `main()` function serves as the entry point for the script's execution.

3. API Request: The script sends a GET request to the Blue Lytics API to retrieve the latest dollar exchange rate data. It handles potential errors with a timeout of 20 seconds.

4. Font and File Path Initialization: It initializes the font for the text overlay and sets the file path for the resulting meme image.

5. Response Handling: If the API request is successful (status code 200), the script proceeds to process the JSON response.

6. Meme Generation: It constructs a humorous message using the fetched exchange rate value and overlays it onto a background image (template.jpeg). The message includes the phrase “NO MAMA!!!” along with the current rounded average dollar value.

7. Image Saving: The resulting meme image is saved to the specified file path.

8. Error Handling: If the API request fails, an error message is printed to the console.

9. Execution Entry Point: The `main()` function is called when the script is run directly.

Note: This script demonstrates how to write text onto an image using Python's PIL library. It's intended for entertainment purposes, showcasing a practical use case of image manipulation in Python.

The “No Mama” Dollar Meme Generator adds a touch of humor to the mundane task of checking exchange rates, making it an amusing example of Python scripting.

[Blue Lytics API](https://api.bluelytics.com.ar/v2/latest) | [PIL Documentation](https://pillow.readthedocs.io/en/stable/)

Result

obtener_valores_de_una_api_y_escribirlos_sobre_una_imagen_jpg.txt · Last modified: 2024/10/17 21:42 by 127.0.0.1