imgutils.operate.censor_

Overview:

A tool for obscuring specified regions on an image.

censor_areas

imgutils.operate.censor_.censor_areas(image: str | PathLike | bytes | bytearray | BinaryIO | Image, method: str, areas: List[Tuple[float, float, float, float]], **kwargs) Image[source]

Applies censoring to specific areas of an image using the registered censor method.

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

  • method (str) – The name of the registered censor method to be used.

  • areas (List[Tuple[float, float, float, float]]) – A list of tuples representing the rectangular areas to be censored in the format (x0, y0, x1, y1).

  • kwargs – Additional keyword arguments to be passed to the censor method.

Returns:

An instance of PIL Image with the censored areas.

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)
>>> ]
>>>
>>> # censor with black color
>>> color_black = censor_areas(origin, 'color', areas, color='black')
>>>
>>> # censor with pixelate
>>> pixelate = censor_areas(origin, 'pixelate', areas, radius=12)
>>>
>>> # censor with emoji
>>> emoji = censor_areas(origin, 'emoji', areas)

This is the result:

../../_images/censor_areas.plot.py.svg

censor_nsfw

imgutils.operate.censor_.censor_nsfw(image: str | PathLike | bytes | bytearray | BinaryIO | Image, method: str, nipple_f: bool = False, penis: bool = True, pussy: bool = True, level: str = 's', version: str = 'v1.0', max_infer_size=640, conf_threshold: float = 0.3, iou_threshold: float = 0.7, **kwargs)[source]

Applies censoring to sensitive areas in NSFW images based on object detection.

The censor area selected by this function is provided by the imgutils.detect.censor.detect_censors() function.

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

  • method (str) – The name of the registered censor method to be used.

  • nipple_f (bool) – Whether to censor female nipples. Default is False.

  • penis (bool) – Whether to censor penises. Default is True.

  • pussy (bool) – Whether to censor vaginas. Default is True.

  • level (str) – The scale for NSFW object detection model. Options are s (small), n (nano, faster than s). Default is s.

  • version (str) – The version of the NSFW object detection model. Default is v1.0.

  • max_infer_size (int) – The maximum size for inference. Default is 640.

  • conf_threshold (float) – The confidence threshold for object detection. Default is 0.3.

  • iou_threshold (float) – The IoU (Intersection over Union) threshold for non-maximum suppression. Default is 0.7.

  • kwargs – Additional keyword arguments to be passed to the censor method.

Returns:

An instance of PIL Image with the sensitive areas censored.

Return type:

Image.Image

Examples::
>>> from PIL import Image
>>> from imgutils.operate import censor_nsfw
>>>
>>> origin = Image.open('nude_girl.png')
>>>
>>> # censor with black color
>>> color_black = censor_nsfw(origin, 'color', nipple_f=True, color='black')
>>>
>>> # censor with pixelate
>>> pixelate = censor_nsfw(origin, 'pixelate', nipple_f=True, radius=12)
>>>
>>> # censor with emoji
>>> emoji = censor_nsfw(origin, 'emoji', nipple_f=True)
This is the result (Warning: NSFW!!!)../../_images/censor_nsfw.plot.py.svg

BaseCensor

class imgutils.operate.censor_.BaseCensor[source]

The Censor base class serves as the foundation for creating custom censor methods by inheriting from this class and registering them using the register_censor_method() function.

censor_area(image: Image, area: Tuple[int, int, int, int], **kwargs) Image[source]

Applies censoring to a specific area within the image.

Parameters:
  • image (Image.Image) – An instance of PIL Image representing the input image.

  • area (Tuple[int, int, int, int]) – A tuple representing the rectangular area to be censored in the format (left, upper, right, lower).

  • kwargs – Additional keyword arguments for customization.

Returns:

An instance of PIL Image with the censored area.

Return type:

Image.Image

ColorCensor

class imgutils.operate.censor_.ColorCensor[source]

A class that performs color censoring by filling a specific area of an image with a solid color.

Inherits from BaseCensor.

censor_area(image: Image, area: Tuple[int, int, int, int], color: str = 'black', **kwargs) Image[source]

Fills a specific area within the image with a solid color for censoring.

Parameters:
  • image (Image.Image) – An instance of PIL Image representing the input image.

  • area (Tuple[int, int, int, int]) – A tuple representing the rectangular area to be censored in the format (left, upper, right, lower).

  • color (str) – The color used to fill the censor area. Default is black. Can be any valid color name or RGB value.

  • kwargs – Additional keyword arguments for customization.

Returns:

An instance of PIL Image with the censored area filled with the specified color.

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
>>> color_default = censor_areas(image, 'color', areas)
>>>
>>> # green
>>> color_green = censor_areas(image, 'color', areas, color='green')
>>>
>>> # #ffff00
>>> color_ffff00 = censor_areas(image, 'color', areas, color='#ffff00')

This is the result:

../../_images/censor_color.plot.py.svg

BlurCensor

class imgutils.operate.censor_.BlurCensor[source]

A class that performs blurring censoring on a specific area of an image.

Inherits from BaseCensor.

censor_area(image: Image, area: Tuple[int, int, int, int], radius: int = 4, **kwargs) Image[source]

Applies blurring censoring to a specific area within the image.

Parameters:
  • image (Image.Image) – An instance of PIL Image representing the input image.

  • area (Tuple[int, int, int, int]) – A tuple representing the rectangular area to be censored in the format (left, upper, right, lower).

  • radius (int) – The radius of the blurring effect. Default is 4.

  • kwargs – Additional keyword arguments for customization.

Returns:

An instance of PIL Image with the blurred area.

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
>>> blur_4 = censor_areas(image, 'blur', areas)
>>>
>>> # radius=8
>>> blur_8 = censor_areas(image, 'blur', areas, radius=8)
>>>
>>> # radius=12
>>> blur_12 = censor_areas(image, 'blur', areas, radius=12)

This is the result:

../../_images/censor_blur.plot.py.svg

PixelateCensor

class imgutils.operate.censor_.PixelateCensor[source]

A class that performs pixelization censoring on a specific area of an image.

Inherits from BaseCensor.

censor_area(image: Image, area: Tuple[int, int, int, int], radius: int = 4, **kwargs) Image[source]

Applies pixelization censoring to a specific area within the image.

Parameters:
  • image (Image.Image) – An instance of PIL Image representing the input image.

  • area (Tuple[int, int, int, int]) – A tuple representing the rectangular area to be censored in the format (left, upper, right, lower).

  • radius (int) – The radius of the pixelation effect. Default is 4.

  • kwargs – Additional keyword arguments for customization.

Returns:

An instance of PIL Image with the pixelated area.

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
>>> pixelate_4 = censor_areas(image, 'pixelate', areas)
>>>
>>> # radius=8
>>> pixelate_8 = censor_areas(image, 'pixelate', areas, radius=8)
>>>
>>> # radius=12
>>> pixelate_12 = censor_areas(image, 'pixelate', areas, radius=12)

This is the result:

../../_images/censor_pixelate.plot.py.svg

register_censor_method

imgutils.operate.censor_.register_censor_method(name: str, cls: Type[BaseCensor], *args, **kwargs)[source]
Overview:

Registers a censor method for subsequent censoring tasks.

Parameters:
  • name (str) – The name of the censor method.

  • cls (Type[BaseCensor]) – The class representing the censor method. It should be a subclass of BaseCensor.

  • args – Positional arguments to be passed when initializing the censor method.

  • kwargs – Keyword arguments to be passed when initializing the censor method.

Raises:

KeyError – If the censor method name already exists.