Map tile on Discord with Python

Hi,

I am currently trying to retrieve a traffic flow map for my area, and I would like to send it to a Discord channel.
The goal is to get the Raster Flow Tile we can see here: https://developer.tomtom.com/content/traffic-api-explorer#/Traffic%20Flow

The issue when I try to retrieve it with the code below, VSCode tells me this:
Command raised an exception: OSError: [WinError 6] Descripteur non valide
I guess it can be translated as : OSError: [WinError 6] The handle is invalid

import os
import json
import sys
import discord
import urllib.request

async def fd_road_map_tomtom():
        print('fd_road_map_tomtom')
        api_key = os.getenv('TOMTOM_TOKEN')
        api_url = f"https://api.tomtom.com/traffic/map/4/tile/flow/absolute/13/[redacted]/[redacted].png?thickness=2&tileSize=512&key={api_key}"
        imageRequest = urllib.request.urlopen(api_url)
        imageBinary = imageRequest.read()
        image = sys.stdout.buffer.write(imageBinary)
        file = discord.File(image)
        return file

In an other function I have the following, that works well except for the image.

async def trafficFlow(self, ctx):
        choosenTime = None
        try:
            embed = await fd_road_cond_tomtom(choosenTime)
            await ctx.send(embed=embed)
        except:
            print('Les données n\ont pas pu être récupérées.')
        else:
           tomtomMap = await fd_road_map_tomtom()
           await ctx.send('TempsReel.png', file=tomtomMap)

How can this be solved?

Thanks!

Do you have an indication which line caused that?

image = sys.stdout.buffer.write(imageBinary)
file = discord.File(image)

If I’m not wrong… write() returns the number of written bytes, so It’s not a handle to a file.

Hi,

Thanks for your response. Finally, here is what I did, may someone want the answer. Plus, I also combined the traffic map with the empty background, with the real map background:

import os
import requests
from PIL import Image

async def fd_road_map_tomtom():
    print('fd_road_map_tomtom')
    api_key = os.getenv('TOMTOM_TOKEN')
    api_url = f"https://api.tomtom.com/traffic/map/4/tile/flow/relative/13/{tile x here}/{tile y here}.png?thickness=5&tileSize=512&key={api_key}"
    r=requests.get(api_url, stream=True)
    if r.status_code == 200:
        with open('./functions/temp/TomTomTempsReel.png', 'wb') as f:
            print('got here')
            f.write(r.content)

            if 6 <= datetime.datetime.now().hour <= 18:   # Daytime map background
                background = Image.open('./functions/temp/TomTomJournee.png')
            else:    # Nighttime map background
                background = Image.open('./functions/temp/TomTomSoir.png')

            img = Image.open('./functions/temp/TomTomTempsReel.png')
            background.paste(img, (0, 0), img)
            background.save('./functions/temp/TomTomTempsReel.png',"PNG")

            file = discord.File('./functions/temp/TomTomTempsReel.png')
    else:
        file = discord.File('./functions/temp/TomTomLogo.png')
    return file