While Python Discord was preparing for the Code Jam, Zig dropped the branding images on the floor and they got scrambled! Please help us rearrange them so that we can be ready for the event in time.

Given an image such as the following:

new_image.png

You need to rearrange the tiles to get a new image:

logo_full_256.png

Luckily, you don’t have to figure out the order of the tiles on your own. You are given the following information:

<aside> 💡 To solve the qualifier, you are allowed to use the standard library, as well as the following third-party libraries: Pillow, numpy, opencv.

</aside>

Qualifier Repository


https://github.com/python-discord/code-jam-10-qualifier

In qualifier/qualifier.py complete the following steps:

  1. Complete the function valid_input. The function tells whether:

    1. The tile size allows splitting the image completely with no remainders. For example a tile size of (128, 128) divides an image of size (256, 256) into exactly 4 tiles. A tile size of 127 or 129 would not work with this image.
    2. The ordering of the tiles is valid. A valid ordering is one where each source tile in the input image is used exactly once.
    def valid_input(image_size: tuple[int, int], tile_size: tuple[int, int], ordering: list[int]) -> bool:
        """
        Return True if the given input allows the rearrangement of the image, False otherwise.
    
        The tile size must divide each image dimension without remainders, and `ordering` must use each input tile exactly
        once.
        """
    
  2. Complete the function rearrange_tiles. The function rearranges the image according to the rules explained above, and saves it to the given path. If the rearrangement input is not valid, raise a ValueError with the message "The tile size or ordering are not valid for the given image".

    def rearrange_tiles(image_path: str, tile_size: tuple[int, int], ordering: list[int], out_path: str) -> None:
        """
        Rearrange the image.
    
        The image is given in `image_path`. Split it into tiles of size `tile_size`, and rearrange them by `ordering`.
        The new image needs to be saved under `out_path`.
    
        The tile size must divide each image dimension without remainders, and `ordering` must use each input tile exactly
        once. If these conditions do not hold, raise a ValueError with the message:
        "The tile size or ordering are not valid for the given image".
        """
    

<aside> 💡 Note: You are allowed to use Pillow, numpy, and opencv for any part of the solution. The images provided are in .png format.

</aside>