articleHow to save a Jpg file with quality option

larryliuming's picture

The result

First let's see the results of images in different quality levels. The highest quality is 100, the lowest quality is 0.

Original image: Image:Goleta_Flower_orignal

Quality 75 image: Image:Goleta_Flower_with_quality_75

Quality 50 image: Image:Goleta_Flower_with_quality_50

Quality 25 image: Image:Goleta_Flower_with_quality_25

Quality 0 image: Image:Goleta_Flower_with_quality_0

If you compare the images above carefully, you will find noise is lesser when the quality is higher. We can also find the image size is larger when the quality is higher.

In the example above:

original image's size is: 1810kb

quality 75 image's size is: 329kb

quality 50 image's size is: 186kb

quality 25 image's size is: 127kb

quality 0 image's size is: 82kb

How to use it

save_jpg_with_quality (a_quality: NATURAL_64) is
		-- Test of usage of saving image with quality parameter
	require
		valid: 0 <= a_quality and a_quality <= 100
	local
		l_source: WEL_GDIP_BITMAP
		l_paras: WEL_GDIP_IMAGE_ENCODER_PARAMETERS
		l_para: WEL_GDIP_IMAGE_ENCODER_PARAMETER
		l_format: WEL_GDIP_IMAGE_ENCODER
	do
		create l_source.make_with_size (1, 1)
		l_source.load_image_from_file ("Goleta_Flower_orignal.jpg")
 
			-- Start create parameters used by image saving feature
		create l_paras.make (1)
 			-- We set quality parameter here.
		create l_para.make ((create {WEL_GDIP_IMAGE_ENCODER}.make_empty).quality, a_quality)
		l_paras.parameters.extend (l_para)
 
		create l_format.make_empty
		l_source.save_image_to_file_with_encoder_and_parameters (
			"Goleta_Flower_with_quality_" + a_quality.out + ".jpg", l_format.jpg, l_paras)
	end

The key is feature {WEL_GDIP_IMAGE}.save_image_to_file_with_encoder_and_parameters. This feature will use {WEL_GDIP_IMAGE_ENCODER_PARAMETERS} as last parameter. When saving a image we can not only set quality parameter, but also compression, color_depth, scan_method, version, render_method, transformation, luminance_table, chrominance_table and save_flag parameters. For the parameter type enumeration, please see {WEL_GDIP_IMAGE_ENCODER}.

PS:

Quality parameter has effects on Jpeg format images. For Png format images, the quality parameter will not work.

Saving image with parameters feature is added in revision#72923.

Syndicate content