Image 类

Summary: The image is the base class for all type of images.

Module: aspose.imaging

Full Name: aspose.imaging.Image

Inheritance: IObjectWithBounds, IMetadataContainer, IHasExifData, IHasMetadata, IHasXmpData, DataStreamSupporter

Properties

NameTypeAccess描述
auto_adjust_paletteboolr/w获取或设置一个值,指示是否自动调整调色板。
background_colorColorr/w获取或设置背景颜色的值。
bits_per_pixelintr获取图像每像素位数计数。
boundsRectangler获取图像的边界。
buffer_size_hintintr/w获取或设置缓冲区大小提示,该提示定义了所有内部缓冲区的最大允许大小。
containerImager获取 Image 容器。
data_stream_containerStreamContainerr获取对象的数据流。
disposedboolr获取一个值,指示此实例是否已释放。
exif_dataExifDatar/w获取或设置 Exif 数据。
file_formatFileFormatr获取文件格式的值
has_background_colorboolr/w获取或设置指示图像是否具有背景颜色的值。
heightintr获取图像高度。
interrupt_monitorInterruptMonitorr/w获取或设置中断监视器。
is_cachedboolr获取一个值,指示对象的数据当前是否已缓存且无需读取数据。
metadataImageMetadatar获取图像元数据。
paletteIColorPaletter/w获取或设置颜色调色板。当像素直接表示时,不使用颜色调色板。
sizeSizer获取图像尺寸。
use_paletteboolr获取一个值,指示是否使用图像调色板。
widthintr获取图像宽度。
xmp_dataXmpPacketWrapperr/w获取或设置 Xmp 数据。

Methods

Name描述
cache_data()缓存数据,并确保不会从底层 DataStreamSupporter.data_stream_container 进行额外的数据加载。
can_load(file_path)确定是否可以从指定的文件路径加载图像。
can_load(file_path, load_options)确定是否可以从指定的文件路径加载图像,并可选地使用指定的打开选项。
can_load(stream)确定是否可以从指定的流加载图像。
can_load(stream, load_options)确定是否可以从指定的流加载图像,并可选地使用指定的 loadOptions
can_load_stream(stream)确定是否可以从指定的流加载图像。
can_load_stream_with_options(stream, load_options)确定是否可以从指定的流加载图像,并可选地使用指定的 loadOptions
can_load_with_options(file_path, load_options)确定是否可以从指定的文件路径加载图像,并可选地使用指定的打开选项。
can_save(options)确定是否可以将图像保存为由传入的保存选项表示的指定文件格式。
create(files)创建包含指定文件的多页图像。
create(files, throw_exception_on_load_error)创建包含指定文件的多页图像。
create(image_options, width, height)使用指定的创建选项创建新图像。
create(image_options, width, height, pixels)创建一个来自提供的像素数组的 RasterImage 实例。

验证指定的宽度和高度是否与像素数据的维度匹配。
仅当库处于授权模式时才能使用此方法。
create(images)使用指定的图像作为页面创建新图像。
create(images, dispose_images)创建一个新图像,将指定的图像作为页面。
create(multipage_create_options)创建指定的多页创建选项。
create_from_files(files)创建包含指定文件的多页图像,将其作为延迟加载页面。
create_from_files(files, throw_exception_on_load_error)创建包含指定文件的多页图像,将其作为延迟加载页面。
create_from_images(images)使用指定的图像作为页面创建新图像。
create_from_images(images, dispose_images)创建一个新图像,将指定的图像作为页面。
crop(left_shift, right_shift, top_shift, bottom_shift)使用位移裁剪图像。
crop(rectangle)裁剪指定的矩形。
get_default_options(args)获取默认选项。
get_file_format(file_path)获取文件格式。
get_file_format(stream)获取文件格式。
get_file_format_of_stream(stream)获取文件格式。
get_fitting_rectangle(rectangle, pixels, width, height)获取适合当前图像的矩形。
get_fitting_rectangle(rectangle, width, height)获取适合当前图像的矩形。
get_original_options()根据原始文件设置获取选项。
这有助于保持原始图像的位深度和其他参数不变。
例如,如果我们加载一个每像素 1 位的黑白 PNG 图像,然后使用
DataStreamSupporter.save(file_path) 方法保存,它将生成每像素 8 位的输出 PNG 图像。
为避免这种情况并以每像素 1 位保存 PNG 图像,请使用此方法获取相应的保存选项,并将其
作为第二个参数传递给 Image.save(file_path, options) 方法。
get_proportional_height(width, height, new_width)获取比例高度。
get_proportional_width(width, height, new_height)获取比例宽度。
get_serialized_stream(image_options, clipping_rectangle, page_number)转换为 aps。
load(file_path)从指定的文件路径或 URL 加载新图像。
如果 filePath 是文件路径,方法仅打开该文件。
如果 filePath 是 URL,方法会下载文件,将其存为临时文件,然后打开它。
load(file_path, load_options)从指定的文件路径或 URL 加载新图像。
如果 filePath 是文件路径,方法仅打开该文件。
如果 filePath 是 URL,方法会下载文件,将其存为临时文件,然后打开它。
load(stream)从指定的流加载新图像。
load(stream, load_options)从指定的流加载新图像。
load_stream(stream)从指定的流加载新图像。
load_stream_with_options(stream, load_options)从指定的流加载新图像。
load_with_options(file_path, load_options)从指定的文件路径或 URL 加载新图像。
如果 filePath 是文件路径,方法仅打开该文件。
如果 filePath 是 URL,方法会下载文件,将其存为临时文件,然后打开它。
remove_metadata()移除元数据。
resize(new_width, new_height)调整图像大小。使用默认的 ResizeType.NEAREST_NEIGHBOUR_RESAMPLE
resize(new_width, new_height, resize_type)调整图像大小。
resize(new_width, new_height, settings)调整图像大小。
resize_by_settings(new_width, new_height, settings)调整图像大小。
resize_by_type(new_width, new_height, resize_type)调整图像大小。
resize_height_proportionally(new_height)按比例调整高度。默认使用 ResizeType.NEAREST_NEIGHBOUR_RESAMPLE
resize_height_proportionally(new_height, resize_type)按比例调整高度。
resize_height_proportionally(new_height, settings)按比例调整高度。
resize_height_proportionally_settings(new_height, settings)按比例调整高度。
resize_width_proportionally(new_width)按比例调整宽度。默认使用 ResizeType.NEAREST_NEIGHBOUR_RESAMPLE
resize_width_proportionally(new_width, resize_type)按比例调整宽度。
resize_width_proportionally(new_width, settings)按比例调整宽度。
resize_width_proportionally_settings(new_width, settings)按比例调整宽度。
rotate(angle)围绕中心旋转图像。
rotate_flip(rotate_flip_type)旋转、翻转或同时旋转和翻转图像。
save()将图像数据保存到底层流中。
save(file_path)将图像保存到指定的文件位置。
save(file_path, options)根据保存选项,将对象的数据保存到指定文件位置的指定文件格式中。
save(file_path, options, bounds_rectangle)根据保存选项,将对象的数据保存到指定文件位置的指定文件格式中。
save(file_path, over_write)将对象的数据保存到指定的文件位置。
save(stream)将对象的数据保存到指定的流中。
save(stream, options_base)根据保存选项,将图像的数据保存到指定流中的指定文件格式。
save(stream, options_base, bounds_rectangle)根据保存选项,将图像的数据保存到指定流中的指定文件格式。
save_to_stream(stream)将对象的数据保存到指定的流中。
save_to_stream_with_options(stream, options_base)根据保存选项,将图像的数据保存到指定流中的指定文件格式。
save_to_stream_with_options_rect(stream, options_base, bounds_rectangle)根据保存选项,将图像的数据保存到指定流中的指定文件格式。
save_with_options(file_path, options)根据保存选项,将对象的数据保存到指定文件位置的指定文件格式中。
save_with_options_rect(file_path, options, bounds_rectangle)根据保存选项,将对象的数据保存到指定文件位置的指定文件格式中。
set_palette(palette, update_colors)设置图像调色板。
try_set_metadata(metadata)尝试设置 metadata 实例,如果此 Image 实例支持并实现 IImageMetadataFormat 类型。

Property: use_palette

获取一个值,指示是否使用图像调色板。

See also:

Example # 1: Determine if the palette is used by the image.

Method: can_load(file_path) [static]

 can_load(file_path) 

确定是否可以从指定的文件路径加载图像。

Parameters:

参数TypeDescription
file_pathstring文件路径。

Returns

TypeDescription
booltrue 如果可以从指定文件加载图像;否则为 false

See also:

Example # 1: This example determines whether image can be loaded from a file.

Method: can_load(file_path, load_options) [static]

 can_load(file_path, load_options) 

确定是否可以从指定的文件路径加载图像,并可选地使用指定的打开选项。

Parameters:

参数TypeDescription
file_pathstring文件路径。
load_optionsLoadOptions加载选项。

Returns

TypeDescription
booltrue 如果可以从指定文件加载图像;否则为 false

Method: can_load(stream) [static]

 can_load(stream) 

确定是否可以从指定的流加载图像。

Parameters:

参数TypeDescription
_io.BufferedRandom要加载的流。

Returns

TypeDescription
booltrue 如果可以从指定流加载图像;否则为 false

See also:

Example # 1: This example determines whether image can be loaded from a file stream.

Method: can_load(stream, load_options) [static]

 can_load(stream, load_options) 

确定是否可以从指定的流加载图像,并可选地使用指定的 loadOptions

Parameters:

参数TypeDescription
_io.BufferedRandom要加载的流。
load_optionsLoadOptions加载选项。

Returns

TypeDescription
booltrue 如果可以从指定流加载图像;否则为 false

Method: can_load_stream(stream) [static]

 can_load_stream(stream) 

确定是否可以从指定的流加载图像。

Parameters:

参数TypeDescription
_io.BufferedRandom要加载的流。

Returns

TypeDescription
booltrue 如果可以从指定流加载图像;否则为 false

Method: can_load_stream_with_options(stream, load_options) [static]

 can_load_stream_with_options(stream, load_options) 

确定是否可以从指定的流加载图像,并可选地使用指定的 loadOptions

Parameters:

参数TypeDescription
_io.BufferedRandom要加载的流。
load_optionsLoadOptions加载选项。

Returns

TypeDescription
booltrue 如果可以从指定流加载图像;否则为 false

Method: can_load_with_options(file_path, load_options) [static]

 can_load_with_options(file_path, load_options) 

确定是否可以从指定的文件路径加载图像,并可选地使用指定的打开选项。

Parameters:

参数TypeDescription
file_pathstring文件路径。
load_optionsLoadOptions加载选项。

Returns

TypeDescription
booltrue 如果可以从指定文件加载图像;否则为 false

Method: can_save(options)

 can_save(options) 

确定是否可以将图像保存为由传入的保存选项表示的指定文件格式。

Parameters:

参数TypeDescription
optionsImageOptionsBase要使用的保存选项。

Returns

TypeDescription
booltrue 如果图像可以保存为由传入的保存选项表示的指定文件格式;否则为 false

See also:

Example # 1: This example shows how to determine whether image can be saved to the specifi…

Method: create(files) [static]

 create(files) 

创建包含指定文件的多页图像。

Parameters:

参数TypeDescription
文件string[]文件。

Returns

TypeDescription
Image多页图像

Method: create(files, throw_exception_on_load_error) [static]

 create(files, throw_exception_on_load_error) 

创建包含指定文件的多页图像。

Parameters:

参数TypeDescription
文件string[]文件。
throw_exception_on_load_errorbool如果设置为 true [throw exception on load error]。

Returns

TypeDescription
Image多页图像

Method: create(image_options, width, height) [static]

 create(image_options, width, height) 

使用指定的创建选项创建新图像。

Parameters:

参数TypeDescription
image_optionsImageOptionsBase图像选项。
widthint宽度。
heightint高度。

Returns

TypeDescription
Image新创建的图像。

See also:

Example # 1: This example creates a new Image file at some disk location as specified by S…

Method: create(image_options, width, height, pixels) [static]

 create(image_options, width, height, pixels) 

创建一个来自提供的像素数组的 RasterImage 实例。

验证指定的宽度和高度是否与像素数据的维度匹配。
仅当库处于授权模式时才能使用此方法。

Parameters:

参数TypeDescription
image_optionsImageOptionsBase用于创建 RasterImage 的选项。
widthintRasterImage 的宽度。
heightintRasterImage 的高度。
像素int[]用于填充图像的像素值数组。

Returns

TypeDescription
Image使用提供的像素数据填充的 RasterImage

Method: create(images) [static]

 create(images) 

使用指定的图像作为页面创建新图像。

Parameters:

参数TypeDescription
imagesImage[]图像。

Returns

TypeDescription
ImageImage 作为 IMultipageImage

Method: create(images, dispose_images) [static]

 create(images, dispose_images) 

创建一个新图像,将指定的图像作为页面。

Parameters:

参数TypeDescription
imagesImage[]图像。
dispose_imagesbool如果设置为 true [dispose images]。

Returns

TypeDescription
ImageImage 作为 IMultipageImage

Method: create(multipage_create_options) [static]

 create(multipage_create_options) 

创建指定的多页创建选项。

Parameters:

参数TypeDescription
multipage_create_optionsMultipageCreateOptions多页创建选项。

Returns

TypeDescription
Image多页图像

Method: create_from_files(files) [static]

 create_from_files(files) 

创建包含指定文件的多页图像,将其作为延迟加载页面。

Parameters:

参数TypeDescription
文件string[]文件。

Returns

TypeDescription
Image多页图像

Method: create_from_files(files, throw_exception_on_load_error) [static]

 create_from_files(files, throw_exception_on_load_error) 

创建包含指定文件的多页图像,将其作为延迟加载页面。

Parameters:

参数TypeDescription
文件string[]文件。
throw_exception_on_load_errorbool如果设置为 true 抛出加载错误异常。

Returns

TypeDescription
Image多页图像

Method: create_from_images(images) [static]

 create_from_images(images) 

使用指定的图像作为页面创建新图像。

Parameters:

参数TypeDescription
imagesImage[]图像。

Returns

TypeDescription
ImageImage 作为 IMultipageImage

Method: create_from_images(images, dispose_images) [static]

 create_from_images(images, dispose_images) 

创建一个新图像,将指定的图像作为页面。

Parameters:

参数TypeDescription
imagesImage[]图像。
dispose_imagesbool如果设置为 true [dispose images]。

Returns

TypeDescription
ImageImage 作为 IMultipageImage

Method: crop(left_shift, right_shift, top_shift, bottom_shift)

 crop(left_shift, right_shift, top_shift, bottom_shift) 

使用位移裁剪图像。

Parameters:

参数TypeDescription
left_shiftint左移。
right_shiftint右移。
top_shiftint上移。
bottom_shiftint下移。

Method: crop(rectangle)

 crop(rectangle) 

裁剪指定的矩形。

Parameters:

参数TypeDescription
rectangleRectangle矩形。

Method: get_default_options(args)

 get_default_options(args) 

获取默认选项。

Parameters:

参数TypeDescription
argsSystem.Object参数。

Returns

TypeDescription
ImageOptionsBase默认选项

Method: get_file_format(file_path) [static]

 get_file_format(file_path) 

获取文件格式。

Parameters:

参数TypeDescription
file_pathstring文件路径。

Returns

TypeDescription
FileFormat确定的文件格式。

See also:

Example # 1: This example shows how to determine the image format without loading the enti…

Method: get_file_format(stream) [static]

 get_file_format(stream) 

获取文件格式。

Parameters:

参数TypeDescription
_io.BufferedRandom该流。

Returns

TypeDescription
FileFormat确定的文件格式。

See also:

Example # 1: This example shows how to determine the image format without loading the enti…

Method: get_file_format_of_stream(stream) [static]

 get_file_format_of_stream(stream) 

获取文件格式。

Parameters:

参数TypeDescription
_io.BufferedRandom该流。

Returns

TypeDescription
FileFormat确定的文件格式。

Method: get_fitting_rectangle(rectangle, pixels, width, height) [static]

 get_fitting_rectangle(rectangle, pixels, width, height) 

获取适合当前图像的矩形。

Parameters:

参数TypeDescription
rectangleRectangle用于获取合适矩形的矩形。
像素int[]32 位 ARGB 像素。
widthint对象宽度。
heightint对象高度。

Returns

TypeDescription
Rectangle适配矩形,若未找到适配矩形则抛出异常。

Method: get_fitting_rectangle(rectangle, width, height) [static]

 get_fitting_rectangle(rectangle, width, height) 

获取适合当前图像的矩形。

Parameters:

参数TypeDescription
rectangleRectangle用于获取合适矩形的矩形。
widthint对象宽度。
heightint对象高度。

Returns

TypeDescription
Rectangle适配矩形,若未找到适配矩形则抛出异常。

Method: get_original_options()

 get_original_options() 

根据原始文件设置获取选项。
这有助于保持原始图像的位深度和其他参数不变。
例如,如果我们加载一个每像素 1 位的黑白 PNG 图像,然后使用
DataStreamSupporter.save(file_path) 方法保存,它将生成每像素 8 位的输出 PNG 图像。
为避免这种情况并以每像素 1 位保存 PNG 图像,请使用此方法获取相应的保存选项,并将其
作为第二个参数传递给 Image.save(file_path, options) 方法。

Returns

TypeDescription
ImageOptionsBase基于原始文件设置的选项。

Method: get_proportional_height(width, height, new_width) [static]

 get_proportional_height(width, height, new_width) 

获取比例高度。

Parameters:

参数TypeDescription
widthint宽度。
heightint高度。
new_widthint新的宽度。

Returns

TypeDescription
int比例高度。

Method: get_proportional_width(width, height, new_height) [static]

 get_proportional_width(width, height, new_height) 

获取比例宽度。

Parameters:

参数TypeDescription
widthint宽度。
heightint高度。
new_heightint新的高度。

Returns

TypeDescription
int比例宽度。

Method: get_serialized_stream(image_options, clipping_rectangle, page_number)

 get_serialized_stream(image_options, clipping_rectangle, page_number) 

转换为 aps。

Parameters:

参数TypeDescription
image_optionsImageOptionsBase图像选项。
clipping_rectangleRectangle裁剪矩形。
page_numberint[]页码。

Returns

TypeDescription
_io.BufferedRandom序列化流

Method: load(file_path) [static]

 load(file_path) 

从指定的文件路径或 URL 加载新图像。
如果 filePath 是文件路径,方法仅打开该文件。
如果 filePath 是 URL,方法会下载文件,将其存为临时文件,然后打开它。

Parameters:

参数TypeDescription
file_pathstring加载图像的文件路径或 URL。

Returns

TypeDescription
Image已加载的图像。

See also:

Example # 1: This example demonstrates the loading of an existing Image file into an insta…

Method: load(file_path, load_options) [static]

 load(file_path, load_options) 

从指定的文件路径或 URL 加载新图像。
如果 filePath 是文件路径,方法仅打开该文件。
如果 filePath 是 URL,方法会下载文件,将其存为临时文件,然后打开它。

Parameters:

参数TypeDescription
file_pathstring加载图像的文件路径或 URL。
load_optionsLoadOptions加载选项。

Returns

TypeDescription
Image已加载的图像。

Method: load(stream) [static]

 load(stream) 

从指定的流加载新图像。

Parameters:

参数TypeDescription
_io.BufferedRandom加载图像的流。

Returns

TypeDescription
Image已加载的图像。

See also:

Example # 1: This example demonstrates the use of a file stream objects to load an existin…

Method: load(stream, load_options) [static]

 load(stream, load_options) 

从指定的流加载新图像。

Parameters:

参数TypeDescription
_io.BufferedRandom加载图像的流。
load_optionsLoadOptions加载选项。

Returns

TypeDescription
Image已加载的图像。

Method: load_stream(stream) [static]

 load_stream(stream) 

从指定的流加载新图像。

Parameters:

参数TypeDescription
_io.BufferedRandom加载图像的流。

Returns

TypeDescription
Image已加载的图像。

Method: load_stream_with_options(stream, load_options) [static]

 load_stream_with_options(stream, load_options) 

从指定的流加载新图像。

Parameters:

参数TypeDescription
_io.BufferedRandom加载图像的流。
load_optionsLoadOptions加载选项。

Returns

TypeDescription
Image已加载的图像。

Method: load_with_options(file_path, load_options) [static]

 load_with_options(file_path, load_options) 

从指定的文件路径或 URL 加载新图像。
如果 filePath 是文件路径,方法仅打开该文件。
如果 filePath 是 URL,方法会下载文件,将其存为临时文件,然后打开它。

Parameters:

参数TypeDescription
file_pathstring加载图像的文件路径或 URL。
load_optionsLoadOptions加载选项。

Returns

TypeDescription
Image已加载的图像。

Method: resize(new_width, new_height)

 resize(new_width, new_height) 

调整图像大小。使用默认的 ResizeType.NEAREST_NEIGHBOUR_RESAMPLE

Parameters:

参数TypeDescription
new_widthint新的宽度。
new_heightint新的高度。

See also:

Example # 1: The following example shows how to resize a metafile (WMF and EMF).

Example # 2: The following example shows how to resize SVG image and save it to PNG.

Method: resize(new_width, new_height, resize_type)

 resize(new_width, new_height, resize_type) 

调整图像大小。

Parameters:

参数TypeDescription
new_widthint新的宽度。
new_heightint新的高度。
resize_typeResizeType调整大小类型。

See also:

Example # 1: Resize image using specific Resize Type.

Method: resize(new_width, new_height, settings)

 resize(new_width, new_height, settings) 

调整图像大小。

Parameters:

参数TypeDescription
new_widthint新的宽度。
new_heightint新的高度。
settingsImageResizeSettings调整大小设置。

See also:

Example # 1: This example loads an image and resizes it using various resizing settings.

Example # 2: Resize image using specific Resize Type.

Method: resize_by_settings(new_width, new_height, settings)

 resize_by_settings(new_width, new_height, settings) 

调整图像大小。

Parameters:

参数TypeDescription
new_widthint新的宽度。
new_heightint新的高度。
settingsImageResizeSettings调整大小设置。

Method: resize_by_type(new_width, new_height, resize_type)

 resize_by_type(new_width, new_height, resize_type) 

调整图像大小。

Parameters:

参数TypeDescription
new_widthint新的宽度。
new_heightint新的高度。
resize_typeResizeType调整大小类型。

Method: resize_height_proportionally(new_height)

 resize_height_proportionally(new_height) 

按比例调整高度。默认使用 ResizeType.NEAREST_NEIGHBOUR_RESAMPLE

Parameters:

参数TypeDescription
new_heightint新的高度。

Method: resize_height_proportionally(new_height, resize_type)

 resize_height_proportionally(new_height, resize_type) 

按比例调整高度。

Parameters:

参数TypeDescription
new_heightint新的高度。
resize_typeResizeType调整大小的类型。

See also:

Example # 1: This example loads an image and resizes it proportionally using various resiz…

Method: resize_height_proportionally(new_height, settings)

 resize_height_proportionally(new_height, settings) 

按比例调整高度。

Parameters:

参数TypeDescription
new_heightint新的高度。
settingsImageResizeSettings图像调整大小设置。

Method: resize_height_proportionally_settings(new_height, settings)

 resize_height_proportionally_settings(new_height, settings) 

按比例调整高度。

Parameters:

参数TypeDescription
new_heightint新的高度。
settingsImageResizeSettings图像调整大小设置。

Method: resize_width_proportionally(new_width)

 resize_width_proportionally(new_width) 

按比例调整宽度。默认使用 ResizeType.NEAREST_NEIGHBOUR_RESAMPLE

Parameters:

参数TypeDescription
new_widthint新的宽度。

Method: resize_width_proportionally(new_width, resize_type)

 resize_width_proportionally(new_width, resize_type) 

按比例调整宽度。

Parameters:

参数TypeDescription
new_widthint新的宽度。
resize_typeResizeType调整大小的类型。

See also:

Example # 1: This example loads an image and resizes it proportionally using various resiz…

Method: resize_width_proportionally(new_width, settings)

 resize_width_proportionally(new_width, settings) 

按比例调整宽度。

Parameters:

参数TypeDescription
new_widthint新的宽度。
settingsImageResizeSettings图像调整大小设置。

Method: resize_width_proportionally_settings(new_width, settings)

 resize_width_proportionally_settings(new_width, settings) 

按比例调整宽度。

Parameters:

参数TypeDescription
new_widthint新的宽度。
settingsImageResizeSettings图像调整大小设置。

Method: rotate(angle)

 rotate(angle) 

围绕中心旋转图像。

Parameters:

参数TypeDescription
角度float旋转角度(以度为单位)。正值将顺时针旋转。

Method: rotate_flip(rotate_flip_type)

 rotate_flip(rotate_flip_type) 

旋转、翻转或同时旋转和翻转图像。

Parameters:

参数TypeDescription
rotate_flip_typeRotateFlipType旋转翻转的类型。

See also:

Example # 1: This example demonstrates the use of Rotate operation on an image. Example lo…

Example # 2: This example loads an image, rotates it by 90 degrees clockwise and optionall…

Method: save(file_path)

 save(file_path) 

将图像保存到指定的文件位置。

Parameters:

参数TypeDescription
file_pathstring保存图像的文件路径。

Method: save(file_path, options)

 save(file_path, options) 

根据保存选项,将对象的数据保存到指定文件位置的指定文件格式中。

Parameters:

参数TypeDescription
file_pathstring文件路径。
optionsImageOptionsBase选项。

See also:

Example # 1: This example shows the simple steps to save an Image. To demonstrate this ope…

Example # 2: The following example loads a BMP image from a file, then saves the image to …

Example # 3: The following example shows how to save an entire BMP image or part of it to …

Method: save(file_path, options, bounds_rectangle)

 save(file_path, options, bounds_rectangle) 

根据保存选项,将对象的数据保存到指定文件位置的指定文件格式中。

Parameters:

参数TypeDescription
file_pathstring文件路径。
optionsImageOptionsBase选项。
bounds_rectangleRectangle目标图像边界矩形。将空矩形设置为使用源边界。

See also:

Example # 1: The following example loads a BMP image from a file, then saves a rectangular…

Example # 2: The following example shows how to save an entire BMP image or part of it to …

Method: save(file_path, over_write)

 save(file_path, over_write) 

将对象的数据保存到指定的文件位置。

Parameters:

参数TypeDescription
file_pathstring保存对象数据的文件路径。
over_writebool如果设置为 true,覆盖文件内容,否则将追加。

Method: save(stream)

 save(stream) 

将对象的数据保存到指定的流中。

Parameters:

参数TypeDescription
_io.BufferedRandom用于保存对象数据的流。

Method: save(stream, options_base)

 save(stream, options_base) 

根据保存选项,将图像的数据保存到指定流中的指定文件格式。

Parameters:

参数TypeDescription
_io.BufferedRandom保存图像数据的流。
options_baseImageOptionsBase保存选项。

See also:

Example # 1: This example shows the process of saving an Image to MemoryStream. To demonst…

Example # 2: The following example loads an image from a file, then saves the image to a P…

Example # 3: The following example shows how to save an entire BMP image or part of it to …

Method: save(stream, options_base, bounds_rectangle)

 save(stream, options_base, bounds_rectangle) 

根据保存选项,将图像的数据保存到指定流中的指定文件格式。

Parameters:

参数TypeDescription
_io.BufferedRandom保存图像数据的流。
options_baseImageOptionsBase保存选项。
bounds_rectangleRectangle目标图像边界矩形。将空矩形设置为使用源边界。

See also:

Example # 1: The following example loads an image from a file, then saves a rectangular pa…

Example # 2: The following example shows how to save an entire BMP image or part of it to …

Method: save_to_stream(stream)

 save_to_stream(stream) 

将对象的数据保存到指定的流中。

Parameters:

参数TypeDescription
_io.BufferedRandom用于保存对象数据的流。

Method: save_to_stream_with_options(stream, options_base)

 save_to_stream_with_options(stream, options_base) 

根据保存选项,将图像的数据保存到指定流中的指定文件格式。

Parameters:

参数TypeDescription
_io.BufferedRandom保存图像数据的流。
options_baseImageOptionsBase保存选项。

Method: save_to_stream_with_options_rect(stream, options_base, bounds_rectangle)

 save_to_stream_with_options_rect(stream, options_base, bounds_rectangle) 

根据保存选项,将图像的数据保存到指定流中的指定文件格式。

Parameters:

参数TypeDescription
_io.BufferedRandom保存图像数据的流。
options_baseImageOptionsBase保存选项。
bounds_rectangleRectangle目标图像边界矩形。将空矩形设置为使用源边界。

Method: save_with_options(file_path, options)

 save_with_options(file_path, options) 

根据保存选项,将对象的数据保存到指定文件位置的指定文件格式中。

Parameters:

参数TypeDescription
file_pathstring文件路径。
optionsImageOptionsBase选项。

Method: save_with_options_rect(file_path, options, bounds_rectangle)

 save_with_options_rect(file_path, options, bounds_rectangle) 

根据保存选项,将对象的数据保存到指定文件位置的指定文件格式中。

Parameters:

参数TypeDescription
file_pathstring文件路径。
optionsImageOptionsBase选项。
bounds_rectangleRectangle目标图像边界矩形。将空矩形设置为使用源边界。

Method: set_palette(palette, update_colors)

 set_palette(palette, update_colors) 

设置图像调色板。

Parameters:

参数TypeDescription
paletteIColorPalette要设置的调色板。
update_colorsbool如果设置为 true,颜色将根据新调色板进行更新;否则颜色索引保持不变。请注意,未更改的索引如果在加载时没有对应的调色板条目,可能会导致图像崩溃。

Method: try_set_metadata(metadata)

 try_set_metadata(metadata) 

尝试设置 metadata 实例,如果此 Image 实例支持并实现 IImageMetadataFormat 类型。

Parameters:

参数TypeDescription
metadataIImageMetadataFormat元数据。

Returns

TypeDescription
bool如果 Image 实例支持并实现 IImageMetadataFormat 类型,则为 true;否则为 false。

Examples

This example demonstrates the loading of an existing Image file into an instance of aspose.imaging.Image using file path specified


from aspose.imaging import Image
# 创建 Image 实例并使用磁盘位置的现有图像文件进行初始化
with Image.load(r"C:\temp\sample.bmp") as image:
	# 进行一些图像处理
	pass

This example creates a new Image file at some disk location as specified by Source property of the BmpOptions instance. Several properties for BmpOptions instance are set before creating the actual image. Especially the Source property, that refers to the actual disk location in this case.


from aspose.imaging import Image
from aspose.imaging.imageoptions import BmpOptions
from aspose.imaging.sources import FileCreateSource

#创建 `BmpOptions` 实例并设置其各种属性
with BmpOptions() as bmp_options:
	bmp_options.bits_per_pixel = 24

	#创建 `FileCreateSource` 实例并将其分配为 `BmpOptions` 实例的 `source`
	#第二个 `Boolean` 参数决定要创建的文件是否为_temporal
	bmp_options.source = FileCreateSource(r"C:\temp\output.bmp", False)

	#创建 Image 实例并通过调用 Create 方法使用 BmpOptions 实例进行初始化
	with Image.create(bmp_options, 500, 500) as image:
		#进行一些图像处理
		# 保存所有更改
		image.save()

This example demonstrates the use of a file stream objects to load an existing Image file


from aspose.imaging import Image

# 创建 FileStream 实例
with open(r"C:\temp\sample.bmp", "rb"):
	#创建 Image 类实例,并通过调用 Load 方法使用 FileStream 对象加载现有文件
	with Image.load(stream) as image:
		#进行一些图像处理。
		pass

This example demonstrates the use of Rotate operation on an image. Example loads an existing image file from some disk location and performs the Rotate operation on the image according to the value of enumeration aspose.imaging.RotateFlipType


from aspose.imaging import Image, RotateFlipType
#创建 Image 类的实例,并通过文件路径使用现有图像文件进行初始化
with Image.load(r"C:\temp\sample.bmp") as image:
	# 将图像绕 X 轴旋转 180 度
	image.rotate_flip(RotateFlipType.ROTATE_180_FLIP_X)
	# 保存所有更改。
	image.save()

This example shows the simple steps to save an Image. To demonstrate this operation, we load an existing file from some disk location, performs rotate operation on the image and save the image in PSD format using file path


from aspose.imaging import Image, RotateFlipType
from aspose.imaging.imageoptions import PsdOptions
from os.path import join as path_join

directory = "c:\\temp"

#创建 Image 类的实例,并通过文件路径使用现有文件进行初始化
with Image.load(path_join(directory, "sample.bmp")) as image:
	#将图像绕 X 轴旋转 180 度
	image.rotate_flip(RotateFlipType.ROTATE_180_FLIP_X)
	#将 Image 保存为 PSD 到文件路径,使用默认的 PsdOptions 设置
	image.save(path_join(directory, "output.psd"), PsdOptions())

This example shows the process of saving an Image to MemoryStream. To demonstrate this operation, example loads an existing file from some disk location, performs rotate operation on the image and save the image in PSD format

from aspose.imaging import Image, RotateFlipType
from aspose.imaging.imageoptions import PsdOptions
from aspose.imaging.extensions import StreamExtensions as stream_ext

#创建 MemoryStream 的实例
with stream_ext.create_memory_stream() as stream:
	#创建 Image 类的实例,并通过文件路径使用现有文件进行初始化
	with Image.load(r"C:\temp\sample.bmp") as image:
		#将图像绕 X 轴旋转 180 度
		image.rotate_flip(RotateFlipType.ROTATE_180_FLIP_X)
		#将 Image 保存为 PSD 到 MemoryStream,使用默认的 PsdOptions 设置
		image.save(stream, PsdOptions())

This example determines whether image can be loaded from a file.


from aspose.imaging import Image

# 使用文件的绝对路径
can_load: bool = Image.can_load(r"c:\temp\sample.gif")

This example determines whether image can be loaded from a file stream.


from aspose.imaging import Image
from aspose.imaging.extensions import StreamExtensions as strm_ext
import os.path import join

directory = r"c:\temp"

canLoad = False

# 使用文件流
with open(join(directory, "sample.bmp"), "rb"):
	canLoad = Image.can_load(stream)

print(f"Can load the file: {canLoad}")

# 以下数据不是有效的图像流,因此 CanLoad 返回 false。
imageData = [0, 0, 0, 0, 0, 0, 0, 0]
with strm_ext.create_memory_stream_from_bytes(imageData) as stream:
	canLoad = Image.can_load(stream)

print(f"Can load the byte buffer: {canLoad}")

This example shows how to determine the image format without loading the entire image from a file.


from aspose.imaging import Image
from os.path import join as path_join

directory = "c:\\temp\\"

# 使用文件的绝对路径
file_format = Image.get_file_format(path_join(directory, "sample.gif"))
print(f"The file format is {file_format}")

This example shows how to determine the image format without loading the entire image from a file stream.


from aspose.imaging import Image
from aspose.imaging.extensions import StreamExtensions as strm_ex
from os.path import join as path_join

directory = "c:\\temp\\"

# 使用文件流
with open(path_join(directory, "sample.bmp"), "rb") as stream:
	file_format = Image.get_file_format(stream)
	print(f"The file format is {file_format}")

# 以下数据不是有效的图像流,因此 get_file_format 返回 FileFormat.UNKNOWN
imageData = bytearray([0, 0, 0, 0, 0, 0, 0, 0])
with strm_ex.create_memory_stream_from_bytes(imageData) as stream:
	file_format = Image.get_file_format(stream)
	print(f"The file format is {file_format}")

This example shows how to determine whether image can be saved to the specified file format represented by the passed save options.


from aspose.imaging import Image
from aspose.imaging.imageoptions import JpegOptions
from os.path import join as path_join

directory = "c:\\temp\\"

with Image.load(path_join(directory, "sample.gif")) as image:
	saveOptions = JpegOptions()
	saveOptions.quality = 50
	# 确定图像是否可以保存为 jpeg
	canSave: bool = image.can_save(saveOptions)
	print(canSave)

This example loads an image and resizes it using various resizing settings.

from aspose.imaging import Image, ImageResizeSettings, ResizeType, ImageFilterType,\
	ColorQuantizationMethod
from os.path import join as path_join

directory = "c:\\temp\\"

resizeSettings = ImageResizeSettings()

# 基于加权混合有理函数和 lanczos3 插值的自适应算法。
resizeSettings.mode = ResizeType.ADAPTIVE_RESAMPLE
# 小矩形滤波器
resizeSettings.filter_type = ImageFilterType.SMALL_RECTANGULAR
# 调色板中的颜色数量。
resizeSettings.entries_count = 256
# 未使用颜色量化
resizeSettings.color_quantization_method = ColorQuantizationMethod.NONE

# 欧几里得方法
resizeSettings.ColorCompareMethod = ColorCompareMethod.Euclidian;

with Image.load(path_join(directory, "sample.gif")) as image:
	# 使用自适应重采样将尺寸缩小 2 倍。
	image.resize(image.width // 2, image.height // 2, resizeSettings)
	image.save(path_join(directory, "downsample.adaptive.gif"))

This example loads an image and resizes it proportionally using various resizing methods. Only the width is specified, the height is calculated automatically.

from aspose.imaging import Image, ResizeType
from os.path import join as path_join

directory = "c:\\temp\\"

with Image.load(path_join(directory, "sample.gif")) as image:
	# 使用最近邻重采样将尺寸放大 2 倍。
	image.resize_width_proportionally(image.width * 2, ResizeType.NEAREST_NEIGHBOUR_RESAMPLE)
	image.save(path_join(directory, "upsample.nearestneighbour.gif"))

with Image.load(path_join(directory, "sample.gif")) as image:
	# 使用最近邻重采样将尺寸缩小 2 倍。
	image.resize_width_proportionally(image.width // 2, ResizeType.NEAREST_NEIGHBOUR_RESAMPLE)
	image.save(path_join(directory, "downsample.nearestneighbour.gif"))

with Image.load(path_join(directory, "sample.gif")) as image:
	# 使用双线性重采样将尺寸放大 2 倍。
	image.resize_width_proportionally(image.width * 2, ResizeType.BILINEAR_RESAMPLE)
	image.save(path_join(directory, "upsample.bilinear.gif"))

with Image.load(path_join(directory, "sample.gif")) as image:
	# 使用双线性重采样将尺寸缩小 2 倍。
	image.resize_width_proportionally(image.width // 2, ResizeType.BILINEAR_RESAMPLE);
	image.save(path_join(directory, "downsample.bilinear.gif"))

This example loads an image and resizes it proportionally using various resizing methods. Only the height is specified, the width is calculated automatically.


from aspose.imaging import Image, ResizeType
from os.path import join as path_join

directory = "c:\\temp\\"

with Image.load(path_join(directory, "sample.gif")) as image:
	# 使用最近邻重采样将尺寸放大 2 倍。
	image.resize_height_proportionally(image.height * 2, ResizeType.NEAREST_NEIGHBOUR_RESAMPLE)
	image.save(path_join(directory, "upsample.nearestneighbour.gif"))

with Image.load(path_join(directory, "sample.gif")) as image:
	# 使用最近邻重采样将尺寸缩小 2 倍。
	image.resize_height_proportionally(image.height // 2, ResizeType.NEAREST_NEIGHBOUR_RESAMPLE)
	image.save(path_join(directory, "downsample.nearestneighbour.gif"))

with Image.load(path_join(directory, "sample.gif")) as image:
	# 使用双线性重采样将尺寸放大 2 倍。
	image.resize_height_proportionally(image.height * 2, ResizeType.BILINEAR_RESAMPLE)
	image.save(path_join(directory, "upsample.bilinear.gif"))

with Image.load(path_join(directory, "sample.gif")) as image:
	# 使用双线性重采样将尺寸缩小 2 倍。
	image.resize_height_proportionally(image.height // 2, ResizeType.BILINEAR_RESAMPLE)
	image.save(path_join(directory, "downsample.bilinear.gif"))

This example loads an image, rotates it by 90 degrees clockwise and optionally flips the image horizontally and(or) vertically.


from aspose.imaging import Image, RotateFlipType
from os.path import join as path_join

directory = "c:\\temp\\"

rotateFlipTypes = [RotateFlipType.ROTATE_90_FLIP_NONE, RotateFlipType.ROTATE_90_FLIP_X,
				   RotateFlipType.ROTATE_90_FLIP_XY, RotateFlipType.ROTATE_90_FLIP_Y]

for rotateFlipType in rotateFlipTypes:
	# 旋转、翻转并保存到输出文件。
	with Image.Load(path_join(directory, "sample.bmp")) as image:
		image.rotate_flip(rotateFlipType)
		image.save(path_join(directory, f"sample.{rotateFlipType}.bmp"))

The following example loads a BMP image from a file, then saves the image to a PNG file.

from aspose.imaging import Image
from aspose.imaging.imageoptions import PngOptions
from os.path import join as path_join

directory = r"c:\temp"

with Image.load(path_join(dir, "sample.bmp")) as image:
	# 将整个图像保存为 PNG 文件。
	save_options = PngOptions()
	image.save(path_join(dir, "output.png"), save_options)

The following example loads a BMP image from a file, then saves a rectangular part of the image to a PNG file.

from aspose.imaging import Image, Rectangle
from aspose.imaging.imageoptions import PngOptions
from os.path import join as path_join

directory = r"c:\temp"

with Image.load(path_join(dir, "sample.bmp")) as image:
	# 将图像的上半部分保存为 PNG 文件。
	save_options = PngOptions()
	bounds = Rectangle(0, 0, image.width, image.height // 2)
	image.save(path_join(dir, "output.png"), save_options, bounds)

The following example loads an image from a file, then saves the image to a PNG file stream.

from aspose.imaging import Image, Rectangle
from aspose.imaging.imageoptions import PngOptions
from os.path import join as path_join

directory = r"c:\temp"

with Image.load(path_join(dir, "sample.bmp")) as image:
	save_options = PngOptions()
	with open(path_join(dir, "output.png"), "w+b") as output_stream:
		# 将整个图像保存到文件流。
		image.save(output_stream, save_options)

The following example loads an image from a file, then saves a rectangular part of the image to a PNG file stream.

from aspose.imaging import Image, Rectangle
from aspose.imaging.imageoptions import PngOptions
from os.path import join as path_join

directory = r"c:\temp"

with Image.load(path_join(dir, "sample.bmp")) as image:
	save_options = PngOptions()
	bounds = Rectangle(0, 0, image.width, image.height // 2)
	with open(path_join(dir, "output.png"), "w+b") as output_stream:
		# 将图像的上半部分保存到文件流。
		image.save(output_stream, save_options, bounds)

The following example shows how to save an entire BMP image or part of it to a file or stream.


from os.path import join as path_join
from aspose.pycore import as_of
from aspose.imaging import Image, ColorPaletteHelper
from aspose.imaging.imageoptions import BmpOptions
from aspose.imaging.extensions import StreamExtensions as strm_ext

directory = "c:\\temp\\"
with Image.load(path_join(directory, "sample.bmp")) as image:
	bmpImage = as_of(image, BmpImage)
		
	# 转换为黑白图像
	bmpImage.binarize_otsu()

	# 使用默认选项保存到相同位置。
	image.save()

	saveOptions = BmpOptions()

	# 此情况下调色板仅包含两种颜色:黑色和白色。
	saveOptions.palette = ColorPaletteHelper.create_monochrome()

	# 对于所有单色图像(包括黑白图像),每像素分配 1 位即可。
	saveOptions.bits_per_pixel = 1

	# 使用指定选项保存到其他位置。
	image.save(path_join(directory, "sample.bw.palettized.bmp"), saveOptions)

	# 仅保存图像的中心部分。
	bounds = Rectangle(image.width // 4, image.height // 4, image.width // 2, image.height // 2)
	image.save(path_join(directory, "sample.bw.palettized.part.bmp"), saveOptions, bounds)

	# 将整个图像保存到内存流
	with strm_ext.create_memory_stream() as stream:
		image.save(stream, saveOptions);
		print("The size of the whole image in bytes:", stream.tell())

	# 将图像的中心部分保存到内存流中
	with strm_ext.create_memory_stream() as stream:
		image.save(stream, saveOptions, bounds)
		print("The size of the central part of the image in bytes: ", stream.tell())

#输出可能如下所示:
#整个图像的字节大小:24062
#图像中心部分的字节大小:6046

The following example shows how to resize a metafile (WMF and EMF).


import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.fileformats.emf import MetaImage
from os.path import join

dir_ = "c:\\temp"
file_names = ["image3.emf", "image4.wmf"]
for file_name in file_names:
	input_file_path = join(dir_, file_name)
	output_file_path = join(dir_, "Downscale_" + file_name)
	with aspycore.as_of(Image.load(input_file_path), MetaImage) as image:
		image.resize(image.width // 4, image.height // 4)
		image.save(output_file_path)

The following example shows how to resize SVG image and save it to PNG.


from aspose.imaging import PointF, Image
from aspose.imaging.imageoptions import PngOptions
from os import path

dir_ = "c:\\aspose.imaging\\net\\issues\\3549"
file_names = ["Logotype.svg", "sample_car.svg", "rg1024_green_grapes.svg", "MidMarkerFigure.svg", "embeddedFonts.svg"]
scales = [PointF(0.5, 0.5), PointF(1.0, 1.0), PointF(2.0, 2.0), PointF(3.5, 9.2)]
for input_file in file_names:
	for scale in scales:
		output_file = "{0}_{1}_{2}.png".format(input_file, str(scale.x), str(scale.y))
		with Image.load(path.join(dir_, input_file)) as image:
			image.resize(int(image.width * scale.x), int(image.height * scale.y))
			image.save(path.join(dir_, output_file), PngOptions())

Resize image using specific Resize Type.

from aspose.imaging import Image, ResizeType, ImageResizeSettings, ImageFilterType

with Image.load("Photo.jpg") as image:
	image.resize(640, 480, ResizeType.CATMULL_ROM)
	image.save("ResizedPhoto.jpg")
	image.resize(1024, 768, ResizeType.CUBIC_CONVOLUTION)
	image.save("ResizedPhoto2.jpg")
	resize_settings = ImageResizeSettings()
	resize_settings.mode = ResizeType.CUBIC_BSPLINE
	resize_settings.filter_type = ImageFilterType.SMALL_RECTANGULAR
	image.resize(800, 800, resize_settings)
	image.save("ResizedPhoto3.jpg")

Determine if the palette is used by the image.


from aspose.imaging import Image

with Image.load("Sample.bmp") as image:
	if image.use_palette:
		print("The palette is used by the image")