Quickly probe the size of various image formats without reading the entire file.
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Amy Mae 9d0a7de83a
Some checks failed
CI / check-test (push) Has been cancelled
Make clippy happy
2026-07-09 11:01:13 -07:00
.github/workflows Add CI 2022-07-13 14:34:40 +08:00
benches Cargo fmt 2024-06-16 23:02:08 -05:00
src Make clippy happy 2026-07-09 11:01:13 -07:00
tests Support astc/eac/etc2/pvrtc formats (#47) 2025-10-18 20:52:04 -05:00
.gitignore Add exr support 2023-05-03 11:00:55 +08:00
.travis.yml Add travis config 2018-03-17 12:16:50 -05:00
Cargo.toml Bump version to 0.15.0 2026-07-09 09:56:38 -07:00
LICENSE Initial commit 2017-04-30 20:11:29 -05:00
README.md Remove weird readme comment 2026-07-09 10:20:01 -07:00

crates.io version docs-badge

imagesize

Quickly probe the size of various image formats without reading the entire file.

The goal of this crate is to be able to read the dimensions of a supported image without loading unnecessary data, and without pulling in more dependencies. Most reads only require 16 bytes or less, and more complex formats take advantage of skipping junk data.

Features

  • Fast: Reads only the necessary bytes to determine image dimensions
  • Lightweight: Minimal dependencies
  • Texture Format Support: Detects compression algorithms in DDS, PowerVR, PKM, and other texture containers
  • Cross-Container Queries: Helper methods to identify compression families across different container formats

Usage

Add the following to your Cargo.toml:

[dependencies]
imagesize = "0.15"

Supported Image Formats

Simple Image Formats

  • Aseprite
  • Avif
  • BMP
  • EXR
  • Farbfeld
  • GIF
  • HDR
  • HEIC / HEIF
  • ICO*
  • ILBM (IFF)
  • JPEG
  • JPEG XL
  • PNG
  • PNM (PBM, PGM, PPM)
  • PSD / PSB
  • QOI
  • TGA
  • TIFF
  • VTF
  • WEBP

Texture Container Formats with Compression Detection

  • DDS - DirectDraw Surface with BC1-7 (DXT1-5) compression detection
  • PKM - ETC1/ETC2/EAC compressed textures
  • PowerVR - PVRTC, ETC2, and EAC compressed textures
  • ATC - Adaptive Texture Compression (Qualcomm Adreno)
  • ASTC - Adaptive Scalable Texture Compression
  • KTX2 - Khronos Texture Container

If you have a format you think should be added, feel free to create an issue.

*ICO files can contain multiple images, imagesize will give the dimensions of the largest one.

Examples

From a file

match imagesize::size("example.webp") {
    Ok(size) => println!("Image dimensions: {}x{}", size.width, size.height),
    Err(why) => println!("Error getting dimensions: {:?}", why)
}

From a vector

let data = vec![0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x64, 0x00, 0x64, 0x00];
match imagesize::blob_size(&data) {
    Ok(size) => println!("Image dimensions: {}x{}", size.width, size.height),
    Err(why) => println!("Error getting dimensions: {:?}", why),
}

Texture Format Detection

For texture container formats, you can detect both the container type and compression algorithm:

use imagesize::{image_type, ImageType, CompressionFamily};

let data = std::fs::read("texture.dds").unwrap();
match image_type(&data) {
    Ok(ImageType::Dds(compression)) => {
        println!("DDS texture with {:?} compression", compression);
    }
    Ok(ImageType::Pvrtc(compression)) => {
        println!("PowerVR texture with {:?} compression", compression);
    }
    Ok(other) => println!("Other format: {:?}", other),
    Err(e) => println!("Error: {:?}", e),
}

Cross-Container Compression Queries

Use helper methods to query compression information across different container formats:

use imagesize::{image_type, CompressionFamily};

let data = std::fs::read("texture.pvr").unwrap();
if let Ok(img_type) = image_type(&data) {
    // Group related compression algorithms regardless of container
    match img_type.compression_family() {
        Some(CompressionFamily::Etc) => println!("ETC family compression"),
        Some(CompressionFamily::BlockCompression) => println!("BC/DXT compression"),
        Some(CompressionFamily::Pvrtc) => println!("PVRTC compression"),
        _ => println!("Other or no compression"),
    }
    
    // Query container and compression properties
    if img_type.is_block_compressed() {
        println!("Uses block compression (BC1-7)");
    }
    
    if let Some(container) = img_type.container_format() {
        println!("Container format: {}", container);
    }
    
    if img_type.is_multi_compression_container() {
        println!("Container supports multiple compression types");
    }
}