Manipulating JPEG Images
Using ExifData Class to Read and Modify Jpeg EXIF Tags
Almost all digital cameras (including smartphones), scanners and other systems handling image save images with EXIF (Exchangeable Image File) information. Camera settings and scene information are recorded by the camera into the image file. EXIF data also include shutter speed, date and time a photo was taken, focal length, exposure compensation, metering pattern and if a flash was used. Aspose.Imaging APIs has made possible to extract the EXIF information from a given image in a very easy and simple manner. Developers may also write EXIF data to the images or modify the existing information as per their requirement. Aspose.Imaging has provided ExifData class for reading, writing and modifying the EXIF data, where as aspose.imaging.exif.enums module contains the relevant enumerations used in the process.
Reading EXIF Data
Aspose.Imaging APIs provide means to read EXIF data from a given image. Below provided steps illustrate the usage of ExifData class to read the EXIF information from an image.
- Load an image into an instance of Image using the factory method load.
- Create and initialize an instance of ExifData class.
- Fetch the required information and write it to console.
Alternatively, developers may also get the specific information using the following code snippet.
Writing & Modifying EXIF Data
Using Aspose.Imaging APIs, developers can write new EXIF information and modify existing EXIF data of an image. Both processes (Writing & Modifying) requires loading of an image and getting the EXIF data into an instance of ExifData class. Then one can access properties exposed by ExifData class to set them accordingly. Sample code to demonstrate the usage is as follow:
import aspose.pycore as aspycore | |
from aspose.imaging import Image | |
from aspose.imaging.fileformats.jpeg import JpegImage | |
from aspose.imaging.exif.enums import ExifWhiteBalance, ExifFlash | |
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 | |
# Load an image using the factory method Load exposed by Image class | |
with Image.load(os.path.join(data_dir, "template.jpg")) as image: | |
# Initialize an object of ExifData and fill it will image's EXIF information | |
exif = (aspycore.as_of(image, JpegImage)).exif_data | |
if exif is not None: | |
# Set LensMake, WhiteBalance, Flash information Save the image | |
exif.lens_make = "Sony" | |
exif.white_balance = ExifWhiteBalance.AUTO | |
exif.flash = ExifFlash.FIRED | |
image.save(os.path.join(data_dir, "result.jpg")) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.jpg")) |
Creating Thumbnails from JPEG Images
Thumbnails are reduced-size versions of pictures, used to display a significant part of the picture instead of the full frame. Some image files (especially the ones shot with a digital camera) have a thumbnail image embedded in the file. In such cases, Aspose.Imaging API retrieves the embedded thumbnail image and allows you to store it separately on disk. With the release of Aspose.Imaging 2.3.1, the JpegImage class contains the ExifData.Thumbnail property that can retrieve the thumbnail information from a JPEG image file. The code snippet provided below demonstrates how to use it.
from aspose.imaging import Image, ResizeType | |
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 | |
# Load an image using the factory method Load exposed by Image class | |
with Image.load(os.path.join(data_dir, "template.jpg")) as image: | |
image.resize(100, 100, ResizeType.LANCZOS_RESAMPLE) | |
image.save(os.path.join(data_dir, "result.jpg")) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.jpg")) |
If you wish to generate thumbnails from other image formats such as BMP & PNG, please refer to the Resizing Images.
Adding Thumbnails to JFIF and EXIF Segments of JPEG Images
The release of Aspose.Imaging 2.3.1 enabled developers to create thumbnails from JPEG images using the ExifData.thumbnail property. Starting from Aspose.Imaging 2.4.0, it is possible to add thumbnails to the JFIF and EXIF segments of JPEG images. There are additional thumbnail properties in the ExifData and Jfif classes, which are of the JpegImage type, that can can be used to store additional thumbnail images inside the original JPEG image.
Add Thumbnail to JFIF Segment
The code snippet below demonstrates how to use the Jfif.thumbnail property to add a thumbnail image to the JFIF segment of a new JPEG image.
import aspose.pycore as aspycore | |
from aspose.imaging import Image | |
from aspose.imaging.fileformats.jpeg import JpegImage, JFIFData | |
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 JpegImage(100, 100) as thumbnail_image: | |
with JpegImage(1000, 1000) as image: | |
image.jfif = JFIFData() | |
image.jfif.thumbnail = thumbnail_image | |
image.save(os.path.join(data_dir, "result.jpg")) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.jpg")) |
Thumbnail images with other segment data cannot occupy more than 65,545 bytes because of the JPEG format specifications. In cases where large images are to be set as a thumbnail, exception may arise.
Add Thumbnail to EXIF Segment
The code snippet below demonstrate how to use the ExifData.thumbnail property to add a thumbnail image to the EXIF segment of a new JPEG image.
import aspose.pycore as aspycore | |
from aspose.imaging import Image | |
from aspose.imaging.fileformats.jpeg import JpegImage, JFIFData | |
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 JpegImage(100, 100) as thumbnail_image: | |
with JpegImage(1000, 1000) as image: | |
image.jfif = JFIFData() | |
image.jfif.thumbnail = thumbnail_image | |
image.save(os.path.join(data_dir, "result.jpg")) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.jpg")) |
In this case, the Aspose.Imaging API cannot estimate the thumbnail image size, but it can check the size of the entire EXIF data segment. This cannot be bigger than 65,535 bytes.
Using JpegExifData Class to Read and Modify Jpeg EXIF Tags
Aspose.Imaging APIs provide JpegExifData class that is exclusive to Jpeg image formats to retrieve & update EXIF information. This article demonstrates the usage of JpegExifData class to achieve the same. aspose.imaging.exif.JpegExifData class serves as EXIF data container for Jpeg images, and provide means to retrieve standard Jpeg EXIF tags as demonstrated below:
Complete List of EXIF Tags
The above code snippet reads a few EXIF Tags using the properties offered by aspose.imaging.exif.JpegExifData class. Complete list of these properties is available here. Following code will read all the EXIF tags using the inspect.getmembers function from inspect package.
Support for JPEG-LS
Aspose.Imaging for Python via .NET API now provide support for JPEG-LS. The code snippet below demonstrates how to use that support for JPEG-LS image and decode that and save into PNG.
import aspose.pycore as aspycore | |
from aspose.imaging import Image, Rectangle | |
from aspose.imaging.fileformats.jpeg import JpegImage, JpegCompressionMode | |
from aspose.imaging.fileformats.png import PngImage | |
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 | |
# Decoding | |
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.jpg")), JpegImage) as jpeg_image: | |
jpeg_options = jpeg_image.jpeg_options | |
# You can read new options: | |
print("Compression type: ", jpeg_options.compression_type) | |
print("Allowed lossy error (NEAR):", jpeg_options.jpeg_ls_allowed_lossy_error) | |
print("Interleaved mode (ILV): ", jpeg_options.jpeg_ls_interleave_mode) | |
# Save the original JPEG-LS image to PNG. | |
jpeg_image.save(os.path.join(data_dir, "result.png"), PngOptions()) | |
# Save the bottom-right quarter of the original JPEG-LS to PNG | |
quarter = Rectangle(jpeg_image.width // 2, jpeg_image.height // 2, | |
jpeg_image.width // 2, jpeg_image.height // 2) | |
jpeg_image.save(os.path.join(data_dir, "result2.png"), PngOptions(), quarter) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.png")) | |
os.remove(os.path.join(data_dir, "result2.png")) |
Support for JPEG-LS with CMYK and YCCK
Aspose.Imaging for Python via .NET API now provide support for CMYK and YCCK color models with JPEG-LS. The code snippet below demonstrates how to use that support for JPEG-LS.
import aspose.pycore as aspycore | |
from aspose.imaging import Image | |
from aspose.imaging.fileformats.jpeg import JpegImage, JpegCompressionMode, JpegCompressionColorMode | |
from aspose.imaging.imageoptions import JpegOptions, 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 StreamExtensions.create_memory_stream() as jpeg_ls_stream: | |
# Save to CMYK JPEG-LS | |
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.jpg")), JpegImage) as image: | |
options = JpegOptions() | |
# Just replace one line given below in examples to use YCCK instead of CMYK | |
# options.ColorType = JpegCompressionColorMode.Cmyk; | |
options.color_type = JpegCompressionColorMode.CMYK | |
options.compression_type = JpegCompressionMode.JPEG_LS | |
# The default profiles will be used. | |
options.rgb_color_profile = None | |
options.cmyk_color_profile = None | |
image.save(jpeg_ls_stream, options) | |
# Load from CMYK JPEG-LS | |
jpeg_ls_stream.seek(0) | |
with aspycore.as_of(Image.load(jpeg_ls_stream), JpegImage) as image: | |
image.save(os.path.join(data_dir, "result.png"), PngOptions()) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.png")) |
import aspose.pycore as aspycore | |
from aspose.imaging import Image | |
from aspose.imaging.fileformats.jpeg import JpegImage, JpegCompressionMode, JpegCompressionColorMode | |
from aspose.imaging.fileformats.png import PngImage | |
from aspose.imaging.imageoptions import PngOptions, JpegOptions | |
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 | |
with StreamExtensions.create_memory_stream() as jpeg_stream: | |
with open(os.path.join(data_dir, "iccprofiles\\eciRGB_v2.icc"), "rb") as rgb_profile_stream, \ | |
open(os.path.join(data_dir, "iccprofiles\\ISOcoated_v2_FullGamut4.icc"), "rb") as cmyk_profile_stream: | |
rgb_color_profile = StreamSource(rgb_profile_stream) | |
cmyk_color_profile = StreamSource(cmyk_profile_stream) | |
# Save to JPEG Lossless CMYK | |
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.jpg")), JpegImage) as image: | |
options = JpegOptions() | |
options.color_type = JpegCompressionColorMode.CMYK | |
options.compression_type = JpegCompressionMode.LOSSLESS | |
# The custom profiles will be used. | |
options.rgb_color_profile = rgb_color_profile | |
options.cmyk_color_profile = cmyk_color_profile | |
image.save(jpeg_stream, options) | |
# Load from JPEG Lossless CMYK | |
jpeg_stream.seek(0) | |
rgb_profile_stream.position = 0 | |
cmyk_profile_stream.position = 0 | |
with aspycore.as_of(Image.load(jpeg_stream), JpegImage) as image: | |
image.rgb_color_profile = rgb_color_profile | |
image.cmyk_color_profile = cmyk_color_profile | |
image.save(os.path.join(data_dir, "result.png"), PngOptions()) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.png")) |
Support for 2-7 bits per sample in JPEG-LS images
Aspose.Imaging for Python via .NET API now provide support for 2-7 bits per sample JPEG-LS images. The code snippet below demonstrates how to use that support for JPEG-LS.
import aspose.pycore as aspycore | |
from aspose.imaging import Image | |
from aspose.imaging.fileformats.jpeg import JpegImage, JpegCompressionMode | |
from aspose.imaging.fileformats.png import PngImage | |
from aspose.imaging.imageoptions import PngOptions, JpegOptions | |
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 | |
bpp = 2 | |
# The origin PNG with 8 bits per sample | |
origin_png_file_name = os.path.join(data_dir, "template.png") | |
# The output JPEG-LS with 2 bits per sample. | |
output_jpeg_file_name = os.path.join(data_dir, "result.jls") | |
with aspycore.as_of(Image.load(origin_png_file_name), PngImage) as png_image: | |
with JpegOptions() as jpeg_options: | |
jpeg_options.bits_per_channel = bpp | |
jpeg_options.compression_type = JpegCompressionMode.JPEG_LS | |
png_image.save(output_jpeg_file_name, jpeg_options) | |
# The output PNG is produced from JPEG-LS to check image visually. | |
output_png_file_name = os.path.join(data_dir, "result.png") | |
with aspycore.as_of(Image.load(output_jpeg_file_name), JpegImage) as jpeg_image: | |
jpeg_image.save(output_png_file_name, PngOptions()) | |
if delete_output: | |
os.remove(output_png_file_name) | |
os.remove(output_jpeg_file_name) |
Setting ColorType and CompressionType for JPEG images
Aspose.Imaging for Python via .NET API now provide support for Color Type and compression Type and set them as gray scale and progressive for JPEG images. The code snippet below demonstrates how to use that support.
import aspose.pycore as aspycore | |
from aspose.imaging import Image | |
from aspose.imaging.fileformats.jpeg import JpegCompressionColorMode, JpegCompressionMode | |
from aspose.imaging.imageoptions import JpegOptions | |
import os | |
import inspect | |
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 Image.load(os.path.join(data_dir, "template.gif")) as original: | |
jpeg_options = JpegOptions() | |
jpeg_options.color_type = JpegCompressionColorMode.GRAYSCALE | |
jpeg_options.compression_type = JpegCompressionMode.PROGRESSIVE | |
original.save(os.path.join(data_dir, "result.jpg"), jpeg_options) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.jpg")) |
Convert TIFF to JPEG image
Using Aspose.Imaging for Python via .NET, developers can convert TIFF to JPEG format. This topic explains the approach to load existing TIFF image and convert it to JPEG using JpegOptions class.
The steps to convert TIFF image to JPEG are as simple as below:
- Create an instance of the TiffImage and load image using load method of Image class
- Iterate over the collection of frames of type TiffFrame
- Create an instance of JpegOptions class and set ResolutionSettings
- Set the resolution unit explicitly
- Call TiffFrame.save method with destination path and an instance JpegOptions
Below provided sample code demonstrate how to convert TIFF to JPEG.
import aspose.pycore as aspycore | |
from aspose.imaging import Image, ResolutionSetting, ResolutionUnit | |
from aspose.imaging.fileformats.tiff import TiffImage | |
from aspose.imaging.fileformats.tiff.enums import TiffResolutionUnits | |
from aspose.imaging.imageoptions import JpegOptions | |
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.tiff")), TiffImage) as tiff_image: | |
i = 0 | |
for tiff_frame in tiff_image.frames: | |
save_options = JpegOptions() | |
save_options.resolution_settings = ResolutionSetting(tiff_frame.horizontal_resolution, tiff_frame.vertical_resolution) | |
if tiff_frame.frame_options is not None: | |
# Set the resolution unit explicitly. | |
tmp_switch = tiff_frame.frame_options.resolution_unit | |
if tmp_switch == TiffResolutionUnits.NONE: | |
save_options.resolution_unit = ResolutionUnit.NONE | |
elif tmp_switch == TiffResolutionUnits.INCH: | |
save_options.resolution_unit = ResolutionUnit.INCH | |
elif tmp_switch == TiffResolutionUnits.CENTIMETER: | |
save_options.resolution_unit = ResolutionUnit.CM | |
else: | |
raise System.NotSupportedException() | |
tiff_frame.save(os.path.join(data_dir, "result.jpg"), save_options) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.jpg")) |
Memory Strategy optimization
Loading and creating of JPEG images can be proceeded using memory strategy optimization - i.e. limiting memory buffer size for operation.
import aspose.pycore as aspycore | |
from aspose.imaging import Image, LoadOptions | |
from aspose.imaging.sources import FileCreateSource | |
from aspose.imaging.imageoptions import JpegOptions | |
from aspose.imaging.fileformats.jpeg import JpegCompressionMode, JpegCompressionColorMode, JpegLsInterleaveMode | |
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 | |
# Setting a memory limit of 50 megabytes for target loaded image | |
obj_init = LoadOptions() | |
obj_init.buffer_size_hint = 50 | |
with Image.load(os.path.join(data_dir, "template.jpg"), obj_init) as image: | |
obj_init2 = JpegOptions() | |
obj_init2.compression_type = JpegCompressionMode.BASELINE | |
obj_init2.quality = 100 | |
image.save(os.path.join(data_dir, "result.jpg"), obj_init2) | |
obj_init3 = JpegOptions() | |
obj_init3.compression_type = JpegCompressionMode.PROGRESSIVE | |
image.save(os.path.join(data_dir, "result2.jpg"), obj_init3) | |
obj_init4 = JpegOptions() | |
obj_init4.color_type = JpegCompressionColorMode.Y_CB_CR | |
obj_init4.compression_type = JpegCompressionMode.LOSSLESS | |
obj_init4.bits_per_channel = 4 | |
image.save(os.path.join(data_dir, "result3.jpg"), obj_init4) | |
obj_init5 = JpegOptions() | |
obj_init5.color_type = JpegCompressionColorMode.Y_CB_CR | |
obj_init5.compression_type = JpegCompressionMode.JPEG_LS | |
obj_init5.jpeg_ls_interleave_mode = JpegLsInterleaveMode.NONE | |
obj_init5.jpeg_ls_allowed_lossy_error = 3 | |
obj_init5.jpeg_ls_preset = None | |
image.save(os.path.join(data_dir, "result4.jpg"), obj_init5) | |
# Setting a memory limit of 50 megabytes for target created image | |
with JpegOptions() as create_options: | |
create_options.compression_type = JpegCompressionMode.PROGRESSIVE | |
create_options.buffer_size_hint = 50 | |
create_options.source = FileCreateSource(os.path.join(data_dir, "result5.jpg"), False) | |
with Image.create(create_options, 1000, 1000) as image: | |
image.save() | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.jpg")) | |
os.remove(os.path.join(data_dir, "result2.jpg")) | |
os.remove(os.path.join(data_dir, "result3.jpg")) | |
os.remove(os.path.join(data_dir, "result4.jpg")) | |
os.remove(os.path.join(data_dir, "result5.jpg")) |
Jpeg saved quality estimation
Using Aspose.Imaging you can easily estimate JPEG quality and work with it.
import aspose.pycore as aspycore | |
from aspose.imaging import Image | |
from aspose.imaging.fileformats.jpeg import JpegImage | |
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.jpg")), JpegImage) as image: | |
quality = image.jpeg_options.quality | |
print("quality", quality) |