Skip to main content

ImageSingle

ClassSource

ImageSingle(
img: str | PathLike[str] | IO[bytes] | Image | Self,
fmt: str | ImageFormat | None = None,
)

The basic one-layer style of image.

Add some extended method for providing extra image processing features.

Arguments​

Requires​

NameTypeRequired
Description
imgstr | PathLike[str] | IO[bytes] | Image | SelfThe image used for initializing this instance. It can be a file path, a file-like object, an image object or this kind of object.
fmtstr | ImageFormat | NoneThe format to be used for saving this image. If not specified, will try to automatically infer the format.

Methods​

add_background​

v_1: Self = self.add_background(color: str | tuple[float, ...] = "#1b2838")

Add a background to the image.

If the current image mode is not RGBA, the output image mode will be converted to RGB automatically.

Requires​

NameTypeRequired
Description
colorstr | tuple[float, ...]The color of the added background.

Returns​

NameType
Description
v_1SelfThe modified image.

blur​

v_1: Self = self.blur(blur_size: int, dil_size: int | None = None)

Make the image blured.

This operation is non-inplace.

Requires​

NameTypeRequired
Description
blur_sizeintThe size of blur filter.
dil_sizeint | NoneThe size of the dilation. If not specified, it will be the same as blur_size.

Returns​

NameType
Description
v_1SelfThe modified image.

convert​

v_1: Self = self.convert(mode: str)

Convert the image mode.

This operation is non-inplace.

Requires​

NameTypeRequired
Description
modestrThe mode of the converted image.

Returns​

NameType
Description
v_1SelfThe converted image.

copy​

v_1: Self = self.copy()

Get a copy of this object.

Returns​

NameType
Description
v_1SelfThe copied image.

crop​

v_1: Self = self.crop(
size: tuple[int | None, int | None], anchor: str | ImageAnchor = ImageAnchor.center
)

Crop this image.

This operation is non-inplace.

Requires​

NameTypeRequired
Description
sizetuple[int | None, int | None]

The size of the cropped image. If this size is larger than the original size, the image mode will be converted to RGBA.

If a size is value None, will treat it as width/height.

anchorstr | ImageAnchorThe anchor where we set for cropping the image. For example, when this anchor is "center", will crop the image for four sides.

Returns​

NameType
Description
v_1SelfThe cropped image.

quantize​

v_1: Self = self.quantize(n: int = 16)

Quantize the image.

This operation is non-inplace.

Requires​

NameTypeRequired
Description
nintThe number of colors used during the quantization.

Returns​

NameType
Description
v_1SelfThe converted image.

resize​

v_1: Self = self.resize(size: tuple[None, int], resample: int | None = None)
v_1: Self = self.resize(size: tuple[int, None], resample: int | None = None)
v_1: Self = self.resize(size: tuple[int, int], resample: int | None = None)

Resize this image.

This operation is non-inplace.

Arguments (ver. 1)​

Resize this image.

This operation is non-inplace.

Requires​
NameTypeRequired
Description
sizetuple[None, int]The size of the resized image. Only the target height is provided. Will resize the image while preserving the w/h ratio.
resampleint | None

The resample method specified by Image.Resampling.

If not specified, will select the resample method automatically:

  1. If size is smaller than the current image size (for either width or height), will use Image.Resampling.LANCZOS.

  2. Otherwise (usually used for enlarging the image), use Image.Resampling.BICUBIC.

Returns​
NameType
Description
v_1SelfThe resized image.

Arguments (ver. 2)​

Resize this image.

This operation is non-inplace.

Requires​
NameTypeRequired
Description
sizetuple[int, None]The size of the resized image. Only the target width is provided. Will resize the image while preserving the w/h ratio.
resampleint | None

The resample method specified by Image.Resampling.

If not specified, will select the resample method automatically:

  1. If size is smaller than the current image size (for either width or height), will use Image.Resampling.LANCZOS.

  2. Otherwise (usually used for enlarging the image), use Image.Resampling.BICUBIC.

Returns​
NameType
Description
v_1SelfThe resized image.

Arguments (ver. 3)​

Resize this image.

This operation is non-inplace.

Requires​
NameTypeRequired
Description
sizetuple[int, int]The size of the resized image.
resampleint | None

The resample method specified by Image.Resampling.

If not specified, will select the resample method automatically:

  1. If size is smaller than the current image size (for either width or height), will use Image.Resampling.LANCZOS.

  2. Otherwise (usually used for enlarging the image), use Image.Resampling.BICUBIC.

Returns​
NameType
Description
v_1SelfThe resized image.

save​

self.save(
fobj: str | PathLike[str] | IO[bytes],
quality: str | ImageQuality = ImageQuality.high,
)

Save the image as a file.

Before saving the image, this method will run optimizations corresponding to the saving format.

Requires​

NameTypeRequired
Description
fobjstr | PathLike[str] | IO[bytes]The file path of file-like object of the output file.
qualitystr | ImageQualityThe quality of the image to be saved.

save_steam_screenshot​

self.save_steam_screenshot(
folder_path: str | PathLike[str],
file_name: str,
quality: str | ImageQuality = ImageQuality.high,
skip_thumbnails: bool = False,
)

Save the image as a Steam screenshot.

When using this method to save the image, we will create a "thumbnails" folder where we save the image, and save the small version (thumbnail image) in the "thumbnails" folder.

Requires​

NameTypeRequired
Description
folder_pathstr | PathLike[str]The path of the folder where we save the image.
file_namestrThe name of the saved screenshot file.
qualitystr | ImageQualityThe quality of the image to be saved.
skip_thumbnailsboolA flag. If specified, will not save thumbnails.

to_lossless​

v_1: Self = self.to_lossless()

Convert the format of this image to the lossless type.

This operation is non-inplace.

Returns​

NameType
Description
v_1SelfThe converted image.

to_lossy​

v_1: Self = self.to_lossy()

Convert the format of this image to the lossy type.

This operation is non-inplace.

Returns​

NameType
Description
v_1SelfThe converted image.

to_palette​

v_1: Self = self.to_palette(n: int | None = None)

Quantize the image and convert the image mode to the palette-based mode. Note that this method will convert the image to the RGB image.

This operation is non-inplace.

Requires​

NameTypeRequired
Description
nint | NoneThe number of colors used in the palette. If not specified, will use Image.Palette.WEB palette.

Returns​

NameType
Description
v_1SelfThe converted image.

Properties​

height​

height: int = self.height

The image height.


size​

size: tuple[int, int] = self.size

The image (width, height).


width​

width: int = self.width

The image width.