[{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/000/183/original/edited_P1410470.jpg?1695058239","metadata":{"caption":""}},{"element_type":"text","content":"\n  \n        \u003cp\u003ei'm working on a big rgb matrix project using the espn API to get data for tracking various sports teams. since it's an rgb matrix it makes sense to display the team logos. however, there are a lot of teams across all of the leagues and images for rgb matrices have to be just so. this part of the project was really intimidating.\u003c/p\u003e\n\u003cp\u003ehowever, as luck would have it, the espn API includes links to excellent .PNG image files for each team. i wrote a python script that creates folders on your computer for each set of images, downloads the images, names them to match the team abbreviation in the API, resizes them, adds gamma correction from the \u003ca href=\"https://learn.adafruit.com/image-correction-for-rgb-led-matrices\" target=\"_blank\"\u003eimage correction for led matrices\u003c/a\u003e guide, spits out a properly formatted bitmap file and deletes the original .PNG image\u003c/p\u003e\n      \n","metadata":{}},{"element_type":"code","content":"import wget\nfrom PIL import Image\nimport os\nimport json\nimport ssl\nimport requests\nimport math\nssl._create_default_https_context = ssl._create_unverified_context\n\nhome_directory = r\"YOUR-DIRECTORY-HERE\"\n\nmlb_folder_name = \"mlb_logos\"\nmlb_folder = os.path.join(home_directory, mlb_folder_name)\nos.makedirs(mlb_folder)\nnfl_folder_name = \"nfl_logos\"\nnfl_folder = os.path.join(home_directory, nfl_folder_name)\nos.makedirs(nfl_folder)\nnhl_folder_name = \"nhl_logos\"\nnhl_folder = os.path.join(home_directory, nhl_folder_name)\nos.makedirs(nhl_folder)\nnba_folder_name = \"nba_logos\"\nnba_folder = os.path.join(home_directory, nba_folder_name)\nos.makedirs(nba_folder)\nmls_folder_name = \"mls_logos\"\nmls_folder = os.path.join(home_directory, mls_folder_name)\nos.makedirs(mls_folder)\n\nsize = (32, 32)\n\nmlb_data = (\"https://site.api.espn.com/apis/site/v2/sports/baseball/mlb/teams\")\nnfl_data = (\"https://site.api.espn.com/apis/site/v2/sports/football/nfl/teams\")\nnhl_data = (\"https://site.api.espn.com/apis/site/v2/sports/hockey/nhl/teams\")\nnba_data = (\"https://site.api.espn.com/apis/site/v2/sports/basketball/nba/teams\")\nmls_data = (\"https://site.api.espn.com/apis/site/v2/sports/soccer/usa.1/teams\")\n\nGAMMA = 2.6\n\ndef process(img, output_8_bit=False, passthrough=None):\n    \"\"\"Given a color image filename, load image and apply gamma correction\n       and error-diffusion dithering while quantizing to 565 color\n       resolution. If output_8_bit is True, image is reduced to 8-bit\n       paletted mode after quantization/dithering. If passthrough (a list\n       of 3-tuple RGB values) is provided, dithering won't be applied to\n       colors in the provided list, they'll be quantized only (allows areas\n       of the image to remain clean and dither-free). Writes a BMP file\n       with the same name plus '-processed' and .BMP extension.\n    \"\"\"\n    err_next_pixel = (0, 0, 0) # Error diffused to the right\n    err_next_row = [(0, 0, 0) for _ in range(img.size[0])] # \" diffused down\n    for row in range(img.size[1]):\n        for column in range(img.size[0]):\n            pixel = img.getpixel((column, row))\n            want = (math.pow(pixel[0] / 255.0, GAMMA) * 31.0, # Gamma and\n                    math.pow(pixel[1] / 255.0, GAMMA) * 63.0, # quantize\n                    math.pow(pixel[2] / 255.0, GAMMA) * 31.0) # to 565 res\n            if pixel in passthrough: # In passthrough list?\n                got = (pixel[0] \u003e\u003e 3, # Take color literally,\n                       pixel[1] \u003e\u003e 2, # though quantized\n                       pixel[2] \u003e\u003e 3)\n            else:\n                got = (min(max(int(err_next_pixel[0] * 0.5 +        # Diffuse\n                                   err_next_row[column][0] * 0.25 + # from\n                                   want[0] + 0.5), 0), 31),         # prior XY\n                       min(max(int(err_next_pixel[1] * 0.5 +\n                                   err_next_row[column][1] * 0.25 +\n                                   want[1] + 0.5), 0), 63),\n                       min(max(int(err_next_pixel[2] * 0.5 +\n                                   err_next_row[column][2] * 0.25 +\n                                   want[2] + 0.5), 0), 31))\n            err_next_pixel = (want[0] - got[0],\n                              want[1] - got[1],\n                              want[2] - got[2])\n            err_next_row[column] = err_next_pixel\n            rgb565 = ((got[0] \u003c\u003c 3) | (got[0] \u003e\u003e 2), # Quantized result\n                      (got[1] \u003c\u003c 2) | (got[1] \u003e\u003e 4), # after dither\n                      (got[2] \u003c\u003c 3) | (got[2] \u003e\u003e 2))\n            img.putpixel((column, row), rgb565) # Put pixel back in image\n\n    if output_8_bit:\n        img = img.convert('P', palette=Image.ADAPTIVE)\n\n# This is a set of color values where error diffusion dithering won't be\n# applied (is still calculated for subsequent pixels, but not applied).\n# Here it's set up for primary colors, so these always appear unfettered\n# in output images.    \nPASSTHROUGH = ((0, 0, 0),\n               (255, 0, 0),\n               (255, 255, 0),\n               (0, 255, 0),\n               (0, 255, 255),\n               (0, 0, 255),\n               (255, 0, 255),\n               (255, 255, 255))\n\ndef logo_to_bitmap(data, folder, logo_num):\n    response = requests.get(data)\n    the_json = response.json()\n    for i in range(len(the_json['sports'][0]['leagues'][0]['teams'])):\n        url = the_json['sports'][0]['leagues'][0]['teams'][i]['team']['logos'][logo_num]['href']\n        filename = str(the_json['sports'][0]['leagues'][0]['teams'][i]['team']['abbreviation']) + \".png\"\n        file = os.path.join(folder, filename)\n        converted_filename = str(the_json['sports'][0]['leagues'][0]['teams'][i]['team']['abbreviation']) + \".bmp\"\n        converted_file = os.path.join(folder, converted_filename)\n        print(url)\n        testfile = wget.download(url, file)\n        \n        img = Image.open(file)\n\n        img.thumbnail(size)\n        img1 = img.convert(\"RGB\")\n        process(img1, True, PASSTHROUGH)\n        img1.save(converted_file)\n        os.remove(file)\n    \nlogo_to_bitmap(mlb_data, mlb_folder, 0)\nlogo_to_bitmap(nfl_data, nfl_folder, 0)\nlogo_to_bitmap(nhl_data, nhl_folder, 0)\nlogo_to_bitmap(nba_data, nba_folder, 0)\nlogo_to_bitmap(mls_data, mls_folder, 1)","metadata":{"language":"python","linenums":false}}]