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:
You need to rearrange the tiles to get a new image:
Luckily, you don’t have to figure out the order of the tiles on your own. You are given the following information:
number of tiles - 1
, from left to right, top to bottom. Each tile in the new image is taken from a tile in the input image given by a tile index. In the example above, in order to rearrange the tiles in the first image to get the second image, the tile arrangement is 0, 3, 1, 2
:
<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>
https://github.com/python-discord/code-jam-10-qualifier
In qualifier/qualifier.py
complete the following steps:
Complete the function valid_input
. The function tells whether:
(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.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.
"""
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>