





'''simple docstring'''

import unittest

import numpy as np

from transformers.testing_utils import 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 DeiTImageProcessor


class      SCREAMING_SNAKE_CASE__      (   unittest.TestCase		):







					"""simple docstring"""






					def __init__(   self       :      int		,	UpperCAmelCase_       :      Tuple		,	UpperCAmelCase_       :      Any=7		,	UpperCAmelCase_       :      Union[str, Any]=3		,	UpperCAmelCase_       :      Optional[Any]=18		,	UpperCAmelCase_       :      Optional[int]=30		,	UpperCAmelCase_       :      Any=400		,	UpperCAmelCase_       :      Optional[Any]=True		,	UpperCAmelCase_       :      Any=None		,	UpperCAmelCase_       :      List[str]=True		,	UpperCAmelCase_       :      Dict=None		,	UpperCAmelCase_       :      List[Any]=True		,	UpperCAmelCase_       :      List[str]=[0.5, 0.5, 0.5]		,	UpperCAmelCase_       :      Union[str, Any]=[0.5, 0.5, 0.5]		,	):







							"""simple docstring"""
							__UpperCAmelCase	:							Tuple     =     size if size is not None else {"height": 20, "width": 20}
							__UpperCAmelCase	:							Any     =     crop_size if crop_size is not None else {"height": 18, "width": 18}

							__UpperCAmelCase	:							int     =     parent
							__UpperCAmelCase	:							Union[str, Any]     =     batch_size
							__UpperCAmelCase	:							Any     =     num_channels
							__UpperCAmelCase	:							str     =     image_size
							__UpperCAmelCase	:							int     =     min_resolution
							__UpperCAmelCase	:							Union[str, Any]     =     max_resolution
							__UpperCAmelCase	:							Dict     =     do_resize
							__UpperCAmelCase	:							Any     =     size
							__UpperCAmelCase	:							str     =     do_center_crop
							__UpperCAmelCase	:							Tuple     =     crop_size
							__UpperCAmelCase	:							Union[str, Any]     =     do_normalize
							__UpperCAmelCase	:							Any     =     image_mean
							__UpperCAmelCase	:							int     =     image_std






					def 		lowerCamelCase_   (   self       :      List[str]		):







							"""simple docstring"""
							return {
							    "do_resize": self.do_resize,
							    "size": self.size,
							    "do_center_crop": self.do_center_crop,
							    "crop_size": self.crop_size,
							    "do_normalize": self.do_normalize,
							    "image_mean": self.image_mean,
							    "image_std": self.image_std,
							}






@require_torch
@require_vision
class      SCREAMING_SNAKE_CASE__      (   snake_case__							,unittest.TestCase		):







					"""simple docstring"""






					SCREAMING_SNAKE_CASE							=	DeiTImageProcessor if is_vision_available() else None
					SCREAMING_SNAKE_CASE							=	True
					def 		lowerCamelCase_   (   self       :      List[str]		):







							"""simple docstring"""
							__UpperCAmelCase	:							List[Any]     =     DeiTImageProcessingTester(self		)
					@property
					def 		lowerCamelCase_   (   self       :      List[Any]		):







							"""simple docstring"""
							return self.image_processor_tester.prepare_image_processor_dict()
					def 		lowerCamelCase_   (   self       :      int		):







							"""simple docstring"""
							__UpperCAmelCase	:							str     =     self.image_processing_class(**self.image_processor_dict		)
							self.assertTrue(hasattr(UpperCAmelCase_		,	"do_resize"		)		)
							self.assertTrue(hasattr(UpperCAmelCase_		,	"size"		)		)
							self.assertTrue(hasattr(UpperCAmelCase_		,	"do_center_crop"		)		)
							self.assertTrue(hasattr(UpperCAmelCase_		,	"center_crop"		)		)
							self.assertTrue(hasattr(UpperCAmelCase_		,	"do_normalize"		)		)
							self.assertTrue(hasattr(UpperCAmelCase_		,	"image_mean"		)		)
							self.assertTrue(hasattr(UpperCAmelCase_		,	"image_std"		)		)
					def 		lowerCamelCase_   (   self       :      List[Any]		):







							"""simple docstring"""
							__UpperCAmelCase	:							Dict     =     self.image_processing_class.from_dict(self.image_processor_dict		)
							self.assertEqual(image_processor.size		,	{"height": 20, "width": 20}		)
							self.assertEqual(image_processor.crop_size		,	{"height": 18, "width": 18}		)

							__UpperCAmelCase	:							Tuple     =     self.image_processing_class.from_dict(self.image_processor_dict		,	size=42		,	crop_size=84		)
							self.assertEqual(image_processor.size		,	{"height": 42, "width": 42}		)
							self.assertEqual(image_processor.crop_size		,	{"height": 84, "width": 84}		)
					def 		lowerCamelCase_   (   self       :      int		):







							"""simple docstring"""
							pass
					def 		lowerCamelCase_   (   self       :      List[str]		):







							"""simple docstring"""
							# Initialize image_processing
							__UpperCAmelCase	:							Optional[int]     =     self.image_processing_class(**self.image_processor_dict		)
							# create random PIL images
							__UpperCAmelCase	:							List[Any]     =     prepare_image_inputs(self.image_processor_tester		,	equal_resolution=UpperCAmelCase_		)
							for image in image_inputs:
									self.assertIsInstance(UpperCAmelCase_		,	Image.Image		)

							# Test not batched input
							__UpperCAmelCase	:							Optional[Any]     =     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.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )		,	)

							# Test batched
							__UpperCAmelCase	:							Any     =     image_processing(UpperCAmelCase_		,	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.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )		,	)
					def 		lowerCamelCase_   (   self       :      List[str]		):







							"""simple docstring"""
							# Initialize image_processing
							__UpperCAmelCase	:							Dict     =     self.image_processing_class(**self.image_processor_dict		)
							# create random numpy tensors
							__UpperCAmelCase	:							List[Any]     =     prepare_image_inputs(self.image_processor_tester		,	equal_resolution=UpperCAmelCase_		,	numpify=UpperCAmelCase_		)
							for image in image_inputs:
									self.assertIsInstance(UpperCAmelCase_		,	np.ndarray		)

							# Test not batched input
							__UpperCAmelCase	:							List[str]     =     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.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )		,	)

							# Test batched
							__UpperCAmelCase	:							Any     =     image_processing(UpperCAmelCase_		,	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.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )		,	)






					def 		lowerCamelCase_   (   self       :      Any		):







							"""simple docstring"""
							# Initialize image_processing
							__UpperCAmelCase	:							Optional[Any]     =     self.image_processing_class(**self.image_processor_dict		)
							# create random PyTorch tensors
							__UpperCAmelCase	:							str     =     prepare_image_inputs(self.image_processor_tester		,	equal_resolution=UpperCAmelCase_		,	torchify=UpperCAmelCase_		)
							for image in image_inputs:
									self.assertIsInstance(UpperCAmelCase_		,	torch.Tensor		)

							# Test not batched input
							__UpperCAmelCase	:							Dict     =     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.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )		,	)

							# Test batched
							__UpperCAmelCase	:							Optional[int]     =     image_processing(UpperCAmelCase_		,	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.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )		,	)











'''simple docstring'''

from typing import List

import PIL
import torch
from PIL import Image

from ...configuration_utils import ConfigMixin
from ...models.modeling_utils import ModelMixin
from ...utils import PIL_INTERPOLATION


class      SCREAMING_SNAKE_CASE__      (   snake_case__							,snake_case__		):







					"""simple docstring"""






					def __init__(   self       :      Any		):







							"""simple docstring"""
							super().__init__()

							self.register_buffer("watermark_image"		,	torch.zeros((62, 62, 4)		)		)
							__UpperCAmelCase	:							Optional[int]     =     None






					def 		lowerCamelCase_   (   self       :      List[Any]		,	UpperCAmelCase_       :      List[PIL.Image.Image]		,	UpperCAmelCase_       :      int=None		):







							"""simple docstring"""
							# copied from https://github.com/deep-floyd/IF/blob/b77482e36ca2031cb94dbca1001fc1e6400bf4ab/deepfloyd_if/modules/base.py#L287

							__UpperCAmelCase	:							int     =     images[0].height
							__UpperCAmelCase	:							Any     =     images[0].width

							__UpperCAmelCase	:							Union[str, Any]     =     sample_size or h

							__UpperCAmelCase	:							Dict     =     min(h / sample_size		,	w / sample_size		)
							__UpperCAmelCase  ,			__UpperCAmelCase	:							Optional[int]     =     (int(h / coef		), int(w / coef		)) if coef < 1 else (h, w)

							__UpperCAmelCase  ,			__UpperCAmelCase	:							Union[str, Any]     =     1_024**2, img_w * img_h
							__UpperCAmelCase	:							Optional[int]     =     (Sa / Sa) ** 0.5
							__UpperCAmelCase  ,			__UpperCAmelCase  ,			__UpperCAmelCase	:							str     =     int(K * 62		), img_w - int(14 * K		), img_h - int(14 * K		)

							if self.watermark_image_as_pil is None:
									__UpperCAmelCase	:							int     =     self.watermark_image.to(torch.uinta		).cpu().numpy()
									__UpperCAmelCase	:							Optional[int]     =     Image.fromarray(UpperCAmelCase_		,	mode="RGBA"		)
									__UpperCAmelCase	:							Optional[int]     =     watermark_image

							__UpperCAmelCase	:							List[str]     =     self.watermark_image_as_pil.resize(
							    (wm_size, wm_size)		,	PIL_INTERPOLATION["bicubic"]		,	reducing_gap=UpperCAmelCase_		)

							for pil_img in images:
									pil_img.paste(UpperCAmelCase_		,	box=(wm_x - wm_size, wm_y - wm_size, wm_x, wm_y)		,	mask=wm_img.split()[-1]		)

							return images




