Overview
    • PDF

    Overview

    • PDF

    Article summary

    Image Resizer is a .NET command-line tool designed for resizing images. By specifying a target file size and output folder, users can adjust images while preserving their visual integrity.

    The tool ensures minimal perceptible differences, maintaining similarity within the limits of human vision. Its output aims for a visual similarity score of 90% or higher, measured using a cross-correlation algorithm.

    Import / Export Integration

    The Glasswall Engine's Export function processes supported files, generating a ZIP archive containing all images extracted from the original document. Users can customize these images and reinsert them into the document using the import function.

    Key integration considerations include:

    • Image file names must remain unchanged, as altering them prevents successful reinsertion.
    • File names are case-sensitive.
    • Images exceeding their original file size may fail during import.

    To ensure seamless reintegration, the tool allows users to resize images to a target size equal to or smaller than the original.

    Usage

    gw_image_resizer [options]

    Options

      -i, --in <filename> (REQUIRED)                               Input file name
      -o, --out <filename> (REQUIRED)                              Output file name
      -t, --target <max size in bytes> (REQUIRED)                  Target maximum output file size
      -l, --memory-limit <max memory limit percentage> (OPTIONAL)  An optional number that specifies the maximum amount of system memory percent the image resize is limited to [default: 50]
      -T, --temp-dir <path> (OPTIONAL)                             Defines the path to an existing directory where temporary files are created during processing [default: $TMPDIR/.gw-image-resizer]
      -?, -h, --help                                               Show help and usage information
      --version                                                    Show version information
    

    Exit Codes

    Exit CodeDescription
    0Successfully resized or the file was already the target size
    1The input file target size is less than zero
    2The input file could not be found
    3The input file could not be loaded as it is corrupt or unsupported
    4The input file could not be loaded for an unknown reason
    5Part of the output path does not exist
    6Could not write the output file for an unknown reason
    7General error in the magick library when trying to resize
    8Could not reach the desired target size
    9Unexpected error
    10Memory limit option was not between 1-100
    11Temporary directory path could not be found
    12Could not write to temporary directory

    Temporary directory

    In order to ensure the successful resizing of files, during processing the image resizer tool may use a temporary directory located at "$TMPDIR/.gw-image-resizer" or "/tmp/.gw.image-resizer". This can be overridden to an existing writeable directory with the optional command line option "--temp-dir" (or "-T" for shorthand.)

    Note: If the temporary directory is not writeable (e.g. the current user does not have adequate permission) then the application will fail when image processing data needs to be cached. It is recommended to test that the user is able to write to the directory to avoid sporadic issues during operation.

    Limiting Memory

    By default the memory allocation used for resizing images is limited to 50% of system memory. This can be overridden to a value between 1-100 with the optional command line option "-l".

    Supported File Types

    The tool currently supports the following:

    • JPEG
    • PNG
    • BMP
    • TIFF (see GeoTiff known issue)
    • GIF

    Other files supported by ImageMagick may load correctly but have not explicitly been implemented.

    Resizing Strategy

    All supported image files go through a set of methods to reach the target size. For example:

    Compression Quality

    JPEG / JPG files have quality set to 25.

    PNG files are set to 100.

    Other file types remain unchanged, however the compression method is always set to LZW.

    GIF Optimization

    Since GIFs can be comprised of multiple images and layers, there are potential optimizations to be made in order to reduce file size.

    If the different layers of an image are of different sizes, Optimization is done by Image Magick to reduce the file size.

    Otherwise the GIF is Coalesced, which merges similarities in images in the sequence to reduce size.

    Transparency is also optimized at this point, replacing duplicate pixels data across the sequence with transparency.

    Quantize

    All images will then be Quantized, which will cap the number of colors to 128, not changing the value if less than this already.

    Downscaling

    If the target is not yet reached, the image will then be resized until it is. Resolution is reduced to 95% of the original continuously until the target is reached or it cannot reduce the resolution any further.

    Third Party Libaries

    Libary NameLibary VersionTypeLicense TypeLicense UrlProject UrlSource Url
    System.CommandLine2.0.0-beta4.22272.1LibaryMITLinkLinkLink
    System.CommandLine.Hosting0.4.0-alpha.22272.1LibaryMITLinkLinkLink
    Magick.NET-Q8-AnyCPU13.10.0LibaryApache-2.0LinkLinkLink

    Glasswall's Image Resizer tool utilizes the "Magick.NET-Q8-AnyCPU" NuGet package which is a dotnet wrapper for ImageMagick.

    ImageMagick itself is a free open source tool that is used for editing and manipulating digital images.

    Known Issues

    Processing can remove locational data from the pixels in GeoTIFF. In addition, regular TIFF's may have colors flipping in grayscale images.


    Was this article helpful?