



from __future__ import annotations

from PIL import Image

# Define glider example
__snake_case						:Optional[Any]      		=     [
    [0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
]

# Define blinker example
__snake_case						:Tuple      		=     [[0, 1, 0], [0, 1, 0], [0, 1, 0]]





def      __snake_case	(			_UpperCAmelCase				):
    __a						       =				[]
    for i in range(len(snake_case__				)				):
        __a						       =				[]
        for j in range(len(cells[i]				)				):
            # Get the number of live neighbours
            __a						       =				0
            if i > 0 and j > 0:
                neighbour_count += cells[i - 1][j - 1]
            if i > 0:
                neighbour_count += cells[i - 1][j]
            if i > 0 and j < len(cells[i]				) - 1:
                neighbour_count += cells[i - 1][j + 1]
            if j > 0:
                neighbour_count += cells[i][j - 1]
            if j < len(cells[i]				) - 1:
                neighbour_count += cells[i][j + 1]
            if i < len(snake_case__				) - 1 and j > 0:
                neighbour_count += cells[i + 1][j - 1]
            if i < len(snake_case__				) - 1:
                neighbour_count += cells[i + 1][j]
            if i < len(snake_case__				) - 1 and j < len(cells[i]				) - 1:
                neighbour_count += cells[i + 1][j + 1]

            # Rules of the game of life (excerpt from Wikipedia):
            # 1. Any live cell with two or three live neighbours survives.
            # 2. Any dead cell with three live neighbours becomes a live cell.
            # 3. All other live cells die in the next generation.
            #    Similarly, all other dead cells stay dead.
            __a						       =				cells[i][j] == 1
            if (
                (alive and 2 <= neighbour_count <= 3)
                or not alive
                and neighbour_count == 3
            ):
                next_generation_row.append(1				)
            else:
                next_generation_row.append(0				)

        next_generation.append(snake_case__				)
    return next_generation





def      __snake_case	(			_UpperCAmelCase   , _UpperCAmelCase				):
    __a						       =				[]
    for _ in range(snake_case__				):
        # Create output image
        __a						       =				Image.new('''RGB'''   , (len(cells[0]				), len(snake_case__				))				)
        __a						       =				img.load()

        # Save cells to image
        for x in range(len(snake_case__				)				):
            for y in range(len(cells[0]				)				):
                __a						       =				255 - cells[y][x] * 255
                __a						       =				(colour, colour, colour)

        # Save image
        images.append(snake_case__				)
        __a						       =				new_generation(snake_case__				)
    return images


if __name__ == "__main__":
 __snake_case						:Union[str, Any]      		=     generate_images(GLIDER, 16)
 images[0].save('''out.gif''', save_all=True, append_images=images[1:])






import unittest

import numpy as np

from transformers.testing_utils import is_flaky, require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import DonutImageProcessor



class 				a							(			unittest.TestCase       ):







 """simple docstring"""






 def __init__(					self		,				lowerCAmelCase_		,				lowerCAmelCase_=7		,				lowerCAmelCase_=3		,				lowerCAmelCase_=18		,				lowerCAmelCase_=30		,				lowerCAmelCase_=4_00		,				lowerCAmelCase_=True		,				lowerCAmelCase_=None		,				lowerCAmelCase_=True		,				lowerCAmelCase_=False		,				lowerCAmelCase_=True		,				lowerCAmelCase_=True		,				lowerCAmelCase_=[0.5, 0.5, 0.5]		,				lowerCAmelCase_=[0.5, 0.5, 0.5]		,				)  ->					Optional[Any]:
        _A        =  parent
        _A        =  batch_size
        _A        =  num_channels
        _A        =  image_size
        _A        =  min_resolution
        _A        =  max_resolution
        _A        =  do_resize
        _A        =  size if size is not None else {"""height""": 18, """width""": 20}
        _A        =  do_thumbnail
        _A        =  do_align_axis
        _A        =  do_pad
        _A        =  do_normalize
        _A        =  image_mean
        _A        =  image_std



 def      UpperCAmelCase							(					self							)  ->					str:
        return {
            "do_resize": self.do_resize,
            "size": self.size,
            "do_thumbnail": self.do_thumbnail,
            "do_align_long_axis": self.do_align_axis,
            "do_pad": self.do_pad,
            "do_normalize": self.do_normalize,
            "image_mean": self.image_mean,
            "image_std": self.image_std,
        }



@require_torch
@require_vision
class 				a							(			__lowerCAmelCase       ,			unittest.TestCase       ):







 """simple docstring"""






 lowerCamelCase	:int    			=		DonutImageProcessor if is_vision_available() else None



 def      UpperCAmelCase							(					self							)  ->					int:
        _A        =  DonutImageProcessingTester(self							)



 @property
 def      UpperCAmelCase							(					self							)  ->					Optional[int]:
        return self.image_processor_tester.prepare_image_processor_dict()



 def      UpperCAmelCase							(					self							)  ->					Optional[Any]:
        _A        =  self.image_processing_class(**self.image_processor_dict							)
        self.assertTrue(hasattr(lowerCAmelCase_		,				"""do_resize"""							)							)
        self.assertTrue(hasattr(lowerCAmelCase_		,				"""size"""							)							)
        self.assertTrue(hasattr(lowerCAmelCase_		,				"""do_thumbnail"""							)							)
        self.assertTrue(hasattr(lowerCAmelCase_		,				"""do_align_long_axis"""							)							)
        self.assertTrue(hasattr(lowerCAmelCase_		,				"""do_pad"""							)							)
        self.assertTrue(hasattr(lowerCAmelCase_		,				"""do_normalize"""							)							)
        self.assertTrue(hasattr(lowerCAmelCase_		,				"""image_mean"""							)							)
        self.assertTrue(hasattr(lowerCAmelCase_		,				"""image_std"""							)							)



 def      UpperCAmelCase							(					self							)  ->					Tuple:
        _A        =  self.image_processing_class.from_dict(self.image_processor_dict							)
        self.assertEqual(image_processor.size		,				{"""height""": 18, """width""": 20}							)

        _A        =  self.image_processing_class.from_dict(self.image_processor_dict		,				size=42							)
        self.assertEqual(image_processor.size		,				{"""height""": 42, """width""": 42}							)

        # Previous config had dimensions in (width, height) order
        _A        =  self.image_processing_class.from_dict(self.image_processor_dict		,				size=(42, 84)							)
        self.assertEqual(image_processor.size		,				{"""height""": 84, """width""": 42}							)



 def      UpperCAmelCase							(					self							)  ->					Optional[int]:
        pass



 @is_flaky()
 def      UpperCAmelCase							(					self							)  ->					Optional[int]:
        # Initialize image_processing
        _A        =  self.image_processing_class(**self.image_processor_dict							)
        # create random PIL images
        _A        =  prepare_image_inputs(self.image_processor_tester		,				equal_resolution=lowerCAmelCase_							)
        for image in image_inputs:
               self.assertIsInstance(lowerCAmelCase_		,				Image.Image							)

        # Test not batched input
        _A        =  image_processing(image_inputs[0]		,				return_tensors="""pt"""							).pixel_values
        self.assertEqual(
            encoded_images.shape		,				(
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )		,				)

        # Test batched
        _A        =  image_processing(lowerCAmelCase_		,				return_tensors="""pt"""							).pixel_values
        self.assertEqual(
            encoded_images.shape		,				(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )		,				)



 @is_flaky()
 def      UpperCAmelCase							(					self							)  ->					Union[str, Any]:
        # Initialize image_processing
        _A        =  self.image_processing_class(**self.image_processor_dict							)
        # create random numpy tensors
        _A        =  prepare_image_inputs(self.image_processor_tester		,				equal_resolution=lowerCAmelCase_		,				numpify=lowerCAmelCase_							)
        for image in image_inputs:
               self.assertIsInstance(lowerCAmelCase_		,				np.ndarray							)

        # Test not batched input
        _A        =  image_processing(image_inputs[0]		,				return_tensors="""pt"""							).pixel_values
        self.assertEqual(
            encoded_images.shape		,				(
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )		,				)

        # Test batched
        _A        =  image_processing(lowerCAmelCase_		,				return_tensors="""pt"""							).pixel_values
        self.assertEqual(
            encoded_images.shape		,				(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )		,				)



 @is_flaky()
 def      UpperCAmelCase							(					self							)  ->					int:
        # Initialize image_processing
        _A        =  self.image_processing_class(**self.image_processor_dict							)
        # create random PyTorch tensors
        _A        =  prepare_image_inputs(self.image_processor_tester		,				equal_resolution=lowerCAmelCase_		,				torchify=lowerCAmelCase_							)
        for image in image_inputs:
               self.assertIsInstance(lowerCAmelCase_		,				torch.Tensor							)

        # Test not batched input
        _A        =  image_processing(image_inputs[0]		,				return_tensors="""pt"""							).pixel_values
        self.assertEqual(
            encoded_images.shape		,				(
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )		,				)

        # Test batched
        _A        =  image_processing(lowerCAmelCase_		,				return_tensors="""pt"""							).pixel_values
        self.assertEqual(
            encoded_images.shape		,				(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )		,				)




