Weird Error with mock image

Hello everyone,

I’m trying to test a website i’m doing (mainly for fun) and I was trying to create mock images with different sizes, dimensions, etc. In order to achieve that I decided to create this 2 functions:

def create_image_file(
        filetype: str = 'webp', 
        size: int = 1,
        dimensions: tuple[int, int] = None,
    ) -> BytesIO:
    """
    Image file creation.
    """
    filetype = filetype.lower()
    if filetype == 'jpg': filetype = 'jpeg'
    img: BytesIO = generate_random_image_larger_than_X_KB(
        size,
        format=filetype,
        dimensions=dimensions
    )
    img.name: str = f'test_{random_string()}.{filetype}'
    return img

def random_string(length:int=6) -> str:
    letters:str = string.ascii_lowercase
    return ''.join(random.choice(letters) for i in range(length))

def generate_random_image_larger_than_X_KB(
        file_size_kb: int,
        format: str = 'webp',
        dimensions: tuple[int, int] = None,
    ) -> BytesIO:
    """
    It generate a random image that is over the file_size_kb.
    """
    width = height = int((file_size_kb * 1024) ** 0.5)
    if dimensions is not None: height, width = dimensions
    imarray = np.random.rand(width,height,3) * 255 # RGBA
    image = Image.fromarray(imarray.astype('uint8')).convert('RGB')
    if dimensions is None: image = image.resize((1920, 1080))
    images_bytes_io = BytesIO()
    image.save(images_bytes_io, format=format)
    images_bytes_io.seek(0)
    return images_bytes_io

The problem I am experiencing is that when i do the following test:

    def post_data(self, image) -> HttpResponse:
        response: HttpResponse = self.client.get(reverse('users:application'))
        response: HttpResponse = self.client.post(response.url, data={'image': image})
        return response

    def test_image_landscape_is_not_accepted(self):
        " Test that if the image uploaded is a landscape image, it is not accepted. "
        image: BytesIO = create_image_file(dimensions=(854, 480))
        response = self.post_data(image)
        self.assertFormsetError(
            formset=response.context['named_formsets']['image'],
            form_index=0,
            field='image_original',
            errors=['The image is not square. Please upload a square image.',
                    'The image is not 1:1 proportion. Please upload an image with this proportion.'],
        )

It replies with “Upload a valid image. The file you uploaded was either not an image or a corrupted image.” But if instead I use (854, 480), the image is accepted without any problem. This message doesn’t come from my code, it seems to come from Django.