Creating, Opening and Saving Images

Creating Image Files

Aspose.Imaging for Python via .NET allows developers to create their own images. Use the static Create method exposed by the Image class to create new images. All you need to do is to supply the appropriate object of one of the classes from the ImageOptions namespace for the desired output image format. To create an image file, first create an instance of one of the classes from the ImageOptions namespace. These classes determine output image format. Below are some classes from the ImageOptions namespace:

  • BmpOptions sets the options for creating a BMP file
  • GifOptions sets the options for creating a GIF file
  • JpegOptions sets the options for creating a JPEG file
  • PngOptions sets the options for creating a PNG file
  • TiffOptions sets the options for creating a TIFF file
  • PsdOptions sets the options for creating a PSD file

Image files can be created setting an output path or by setting a stream.

Creating by Setting Path

Create an object of any desired class from the ImageOptions namespace and set the various properties. The most important property to set is the Source property. This property specifies where the image data resides (in a file or a stream). In the example below, the source is a file. After setting the properties, pass the object to one of the static Create methods along with width and height parameter.The width and height are defined in pixels. In the example below, we are creating a BMP file so we need to create an instance of ImageOptions.BmpOptions.

import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.imageoptions import BmpOptions
from aspose.imaging.sources import FileCreateSource
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
# Creates an instance of BmpOptions and set its various properties
with BmpOptions() as image_options:
image_options.bits_per_pixel = 24
# Define the source property for the instance of BmpOptions Second boolean parameter determines if the file is temporal or not
image_options.source = FileCreateSource(os.path.join(data_dir, "result1.bmp"), False)
# Creates an instance of Image and call Create method by passing the BmpOptions object
with Image.create(image_options, 500, 500) as image:
image.save(os.path.join(data_dir, "result2.bmp"))
if delete_output:
os.remove(os.path.join(data_dir, "result1.bmp"))
os.remove(os.path.join(data_dir, "result2.bmp"))

Creating Using Stream

The process for creating an image using a stream is same as for using a path. The only difference is that you need to create an instance of StreamSource by passing a Stream object to its constructor and assigning it to the Source property. The following code snippet shows you how to create Using Stream.

from aspose.pyio import FileMode
import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.imageoptions import BmpOptions
from aspose.imaging.sources import StreamSource
from aspose.imaging.extensions import StreamExtensions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
# Creates an instance of BmpOptions and set its various properties
with BmpOptions() as image_options:
image_options.bits_per_pixel = 24
# Create an instance of System.IO.Stream
stream = StreamExtensions.create_file_stream(os.path.join(data_dir, "result1.bmp"), FileMode.CREATE)
# Define the source property for the instance of BmpOptions Second boolean parameter determines if the Stream is disposed once get out of scope
image_options.source = StreamSource(stream, True)
# Creates an instance of Image and call Create method by passing the BmpOptions object
with Image.create(image_options, 500, 500) as image:
# Do some image processing
image.save(os.path.join(data_dir, "result2.bmp"))
if delete_output:
os.remove(os.path.join(data_dir, "result1.bmp"))
os.remove(os.path.join(data_dir, "result2.bmp"))

Opening Image Files

Developers can use Aspose.Imaging for Python via .NET API to open existing image files for different purposes, like adding effects to the image or to convert an existing file to another format. Whatever the purpose is, Aspose.Imaging provides two standard ways to open existing files: from file or from a stream.

Opening from Disk

Open an image file by passing the path and file name as a parameter to the static method load exposed by the Image class. The following code snippet shows you how to open image files from disk.

import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.fileformats.bmp import BmpImage
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.bmp")), BmpImage) as image:
pass

Opening using a Stream

Sometimes the image that we need to open is stored as a stream. In such cases, use the overloaded version of the load method. This accepts a Stream object as an argument to open the image. The following code snippet shows you how to open image files using a stream.

import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.fileformats.bmp import BmpImage
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with open(os.path.join(data_dir, "template.bmp"), "rb") as fs:
with aspycore.as_of(Image.load(fs), BmpImage) as image:
pass

Saving Image Files

Aspose.Imaging lets you create image files from scratch. It also provides the means to edit existing image files. Once the image is created or modified, the file is usually saved to disk. Aspose.Imaging provides you with methods for saving images to a disk by specifying a path or using a Stream object.

Saving to Disk

The Image class represents an image object, so this class provides all the tools needed to create, load and save an image file. Use the Image class save method to save images. One overloaded version of the save method accepts the file location as a string. The following code snippet shows you how to save image files to disk.

import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.fileformats.bmp import BmpImage
from aspose.imaging.imageoptions import PngOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with open(os.path.join(data_dir, "template.bmp"), "rb") as fs:
with aspycore.as_of(Image.load(fs), BmpImage) as image:
image.save(os.path.join(data_dir, "result.png"), PngOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))

Saving to a Stream

Another overloaded version of the Save method accepts the Stream object as an argument and saves the image file to the stream. The following code snippet shows you how to save image files to disk using a stream.

import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.fileformats.bmp import BmpImage
from aspose.imaging.imageoptions import PngOptions
from aspose.imaging.extensions import StreamExtensions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with open(os.path.join(data_dir, "template.bmp"), "rb") as fs:
with aspycore.as_of(Image.load(fs), BmpImage) as image:
with StreamExtensions.create_memory_stream() as mem:
image.save(mem, PngOptions())
print("Output length:", mem.seek(0, 2), "bytes")

If the image is created by specifying any of the CreateOptions in the Image constructor, the image is automatically saved to the path or stream supplied during the initialization of the Image class by calling the Save method that doesn’t accept any parameter.

Save image file extension aware

Using Aspose.Imaging file extension maps to appropriate image options if you did not specified them.

from aspose.imaging import Image
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
input_file = os.path.join(data_dir, "template.jpg")
output_file = os.path.join(data_dir, "result.png")
with Image.load(input_file) as image:
image.save(output_file)
if delete_output:
os.remove(output_file)

Setting for Replacing Missing Fonts

Developers can use Aspose.Imaging for Python via .NET API to load existing image files for different purposes and this default font should be used as a replacement for all missing fonts (fonts that are not found in current Operating System). Once the image is modified, the file is will be saved to disk.

import aspose.pycore as aspycore
from aspose.imaging import FontSettings, Image
from aspose.imaging.imageoptions import PngOptions, EmfRasterizationOptions, OdgRasterizationOptions, \
SvgRasterizationOptions, WmfRasterizationOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
FontSettings.set_default_font_name("Comic Sans MS")
files = ["template.emf", "template.odg", "template.svg", "template.wmf"]
options = [EmfRasterizationOptions(), OdgRasterizationOptions(), SvgRasterizationOptions(), WmfRasterizationOptions()]
index = 0
for file in files:
out_file = os.path.join(data_dir, file + ".png")
with Image.load(os.path.join(data_dir, file)) as img:
options[index].page_width = float(img.width)
options[index].page_height = float(img.height)
export_options = PngOptions()
export_options.vector_rasterization_options = options[index]
img.save(out_file, export_options)
index += 1
if delete_output:
os.remove(out_file)