b = image_name.resample(width, height);
b = image_name.resample(ʃwidth := width,ʅ
ʃheight := heightʅ);
b = image_name.resample(ʃmaxWidth := max_width,ʅ
ʃmaxHeight := max_heightʅ);
The
Image.resample function resamples the image held by the
Image object. Resampling modifies the dimensions of the image by either downsampling or upsampling the image. A loss of image quality may occur during the resampling process.
In the first version of the function, two numeric expressions,
width and
height, specify the new dimensions, in pixels, of the image. If one of the arguments is
notappl, then the other dimension will be calculated by maintaining the original image's aspect ratio (the ratio of the width to the height). For example, if an image is 800 x 600, each of the following function calls results in a 400 x 300 image:
The second and third versions of the function, accessed by using
named arguments, provide additional ways to control how the image is resampled. Specifying both
width and
height is equivalent to the first version of the function; specifying only one of the arguments is as if
notappl were specified for the other argument. Providing a
max_width and/or
max_height will conditionally resample an image only if the image's width or height exceeds the provided maximum. Using the 800 x 600 example from above:
The first function call results in a resampling to 400 x 300. The second function call does not result in a resampling because the original height, 600, does not exceed 1200. In this case, the function still returns 1 even though the image was not resampled.
The function returns a logical value of 1 (
true) if the image was resampled, 0 (
false) if there was an error, and
default if the image object does not contain an image.
// create thumbnails for every image in the Photos directory,
// placing the thumbnails in the Thumbnails directory
List string image_listing;
dirlist(image_listing, "Photos", "*.jpg;*.png");
do numeric counter = 1 while counter <= image_listing.length()
Image thumbnail_image;
if thumbnail_image.load(image_listing(counter)) then
// create a thumbnail at 25% of the original image size
thumbnail_image.resample(thumbnail_image.width() / 4, thumbnail_image.height() / 4);
// prefix the thumbnail filename with the text "tn"
string thumbnail_filename = Path.concat("Thumbnails", "tn" + Path.getFileName(image_listing(counter)));
thumbnail_image.save(thumbnail_filename);
endif;
enddo;
Image roof_photo;
if roof_photo.takePhoto("Take a photo of the household's roof.") then
// in case the device's camera takes photos with an unnecessarily
// large resolution, resample the image to a more reasonable size
roof_photo.resample(maxWidth := 1600, maxHeight := 1200);
// save the image using the household key...
string base_filename = Path.concat(application, "Roof Photos", key(HH_DICT));
// ...with 90 quality to prevent the JPEG from being too large
roof_photo.save(base_filename + ".jpg", quality := 90);
endif;