imgutils.operate.imgcensor

Overview:

Censors a specified area of an image using a custom image or emoji.

ImageBasedCensor

class imgutils.operate.imgcensor.ImageBasedCensor(images: str | PathLike | bytes | bytearray | BinaryIO | Image | List[str | PathLike | bytes | bytearray | BinaryIO | Image] | Tuple[str | PathLike | bytes | bytearray | BinaryIO | Image, ...], rotate: Tuple[int, int] = (-30, 30), step: int = 10)[source]

A class that performs censoring on a given area using images by finding the best solution based on rotation.

Parameters:
  • images (MultiImagesTyping) – The input images for censoring.

  • rotate (Tuple[int, int]) – The range of rotation angles in degrees (start, end) to consider.

  • step (int) – The step size between rotation angles.

censor_area(image: Image, area: Tuple[int, int, int, int], ratio_threshold: float = 0.5, **kwargs) Image[source]

Censors the specified area of the input image using the available images.

Parameters:
  • image (Image.Image) – The input image to be censored.

  • area (Tuple[int, int, int, int]) – The coordinates of the target area to censor (x0, y0, x1, y1).

  • ratio_threshold (float) – The minimum ratio of the covered area to the total mask area required for a solution to be considered valid.

  • kwargs – Additional keyword arguments to be passed.

Returns:

The censored image.

Return type:

Image.Image

Examples::
>>> from PIL import Image
>>> from imgutils.operate import censor_areas
>>>
>>> origin = Image.open('genshin_post.jpg')
>>> areas = [  # areas to censor
>>>     (967, 143, 1084, 261),
>>>     (246, 208, 331, 287),
>>>     (662, 466, 705, 514),
>>>     (479, 283, 523, 326)
>>> ]
>>>
>>> # register the star image
>>> register_censor_method('star', ImageBasedCensor, images=['star.png'])
>>>
>>> # default
>>> censored = censor_areas(image, 'star', areas)
../../_images/censor_image.plot.py.svg

Note

It is important to note that when using ImageBasedCensor to censor an image, you need to manually register the image used for censoring using the register_censor_method() function.

EmojiBasedCensor

class imgutils.operate.imgcensor.EmojiBasedCensor(rotate: Tuple[int, int] = (-30, 30), step: int = 10)[source]

Performs censoring on a given area of an image using emoji images.

Parameters:
  • rotate (Tuple[int, int]) – The range of rotation angles in degrees (start, end) to consider.

  • step (int) – The step size between rotation angles.

censor_area(image: Image, area: Tuple[int, int, int, int], emoji: str = ':smiling_face_with_heart-eyes:', style: Literal['twitter', 'apple', 'google', 'microsoft', 'samsung', 'whatsapp', 'facebook', 'messenger', 'joypixels', 'openmoji', 'emojidex', 'mozilla'] = 'twitter', ratio_threshold: float = 0.5, **kwargs) Image[source]

Censors the specified area of the input image using emoji expressions.

Parameters:
  • image (Image.Image) – The input image to be censored.

  • area (Tuple[int, int, int, int]) – The coordinates of the target area to censor (x0, y0, x1, y1).

  • emoji (str) – The emoji expression to use for censoring. Emoji code in emoji is supported. (default: :smiling_face_with_heart-eyes:, which equals to 😍)

  • style (_EmojiStyleTyping) – The style of the emoji expression. (default: twitter)

  • ratio_threshold (float) – The minimum ratio of the covered area to the total mask area required for a solution to be considered valid.

  • kwargs – Additional keyword arguments to be passed.

Returns:

The censored image.

Return type:

Image.Image

Examples::
>>> from PIL import Image
>>> from imgutils.operate import censor_areas
>>>
>>> origin = Image.open('genshin_post.jpg')
>>> areas = [  # areas to censor
>>>     (967, 143, 1084, 261),
>>>     (246, 208, 331, 287),
>>>     (662, 466, 705, 514),
>>>     (479, 283, 523, 326)
>>> ]
>>>
>>> # default
>>> emoji_default = censor_areas(image, 'emoji', areas)
>>>
>>> # cat_face (use emoji code)
>>> emoji_green = censor_areas(image, 'emoji', areas, emoji=':cat_face:')
>>>
>>> # grinning_face_with_sweat (use emoji)
>>> emoji_liuhanhuangdou = censor_areas(image, 'emoji', areas, emoji='😅')

This is the result:

../../_images/censor_emoji.plot.py.svg

SingleImage

class imgutils.operate.imgcensor.SingleImage(image: Image)[source]

A class that attempts to find a solution to completely cover a given area of an image while minimizing the covered area.

Parameters:

image (Image.Image) – The input image.

Variables:
  • image (Image.Image) – The original image for censoring.

  • mask (np.ndarray) – The mask of the image. True means this pixel is not transparent and able to cover some area.

  • prefix (np.ndarray) – The prefix sum of the mask.

  • cx (float) – The X-coordinate of the mass center of this image. The position of the occlusion should be as close as possible to the mass center of the image.

  • cy (float) – The Y-coordinate of the mass center of this image. The position of the occlusion should be as close as possible to the mass center of the image.

  • width (int) – The width of the image.

  • height (int) – The height of the image.

__init__(image: Image)[source]
find_for_area(width: int, height: int) Tuple[float, float, float, float][source]

Finds a solution to completely cover a given area with a rectangle while minimizing the covered area.

Parameters:
  • width (int) – The width of the target area to cover.

  • height (int) – The height of the target area to cover.

Returns:

The coordinates (x, y) of the found solution (top-left corner), the scaling factor applied to the width and height, and the ratio of the covered area to the total mask area.

Return type:

Tuple[float, float, float, float]

property height

The height of the image.

Returns:

The height of the image.

Return type:

int

property width

The width of the image.

Returns:

The width of the image.

Return type:

int