4d — Image file formats

TIFF, HDF5, MRC, EER, PNG, JPEG, GIF · 4d User Manual

← Back to 4D-STEM imaging and reconstruction methods · 4d User Manual

Relevance of the file format

A file format is not only a container: it determines which properties of the measurement are preserved. Four properties are relevant for electron microscopy data and form the basis of the following comparison.

Overview of the formats

File sizes are given for a single 1024 × 1024 image. "Raw" denotes the pixel data alone. The compressed sizes assume noisy detector data, which compress considerably less than a photograph, since noise is incompressible. This is a frequent source of unexpectedly large archives of micrographs.

FormatCompressionBit depthsOrigin Metadata in the header1024 × 1024
HDF5 Optional, lossless, per chunk: gzip, Blosc2, bitshuffle+LZ4, Zstd, LZF any: 8/16/32/64-bit integer or float, complex no convention; determined by the writing software unlimited, user-defined attributes at any node of the tree 2.1 MB at 16 bit raw; counting data typically 5–10× smaller compressed
MRC None in the format (files are frequently gzipped as a whole) int8, int16, uint16, float16, float32, complex bottom-left fixed 1024-byte header plus optional extended header 2.1 MB at 16 bit, 4.2 MB at 32-bit float, plus 1 KB header
TIFF Optional, lossless: LZW, Deflate, PackBits (JPEG-in-TIFF is lossy) 1–64 bit, integer or float, any number of samples top-left (Orientation tag 274 may specify otherwise) tags: size, bit depth, resolution, description, date, private tags 2.1 MB at 16 bit raw; ≈ 1.6–2.0 MB with LZW/Deflate on noisy data
EER Lossless, event-based; the defining property of the format event positions rather than pixel values top-left (TIFF container) TIFF tags plus an XML metadata block scales with the number of electrons, not pixels; see below
PNG Lossless, Deflate with per-row prediction filters 8 and 16 bit, greyscale or RGB(A); no float top-left size, bit depth, optional physical pixel size and text chunks ≈ 1.5–2.0 MB at 16 bit, ≈ 0.7–1.0 MB at 8 bit, for noisy data
JPEG Lossy, DCT-based, quality-dependent 8 bit per channel top-left (EXIF Orientation may rotate or mirror) JFIF and EXIF blocks: camera, date, orientation, thumbnail ≈ 0.2–0.7 MB at quality 90; noise increases the size
GIF Lossless LZW, restricted to a 256-colour palette 8-bit palette (≤ 256 colours) top-left screen descriptor, palette, per-frame delays ≈ 0.6–1.0 MB after quantisation to 256 grey levels
For reference, the uncompressed pixel data of a 1024 × 1024 image occupy 1.05 MB at 8 bit, 2.10 MB at 16 bit and 4.19 MB at 32-bit float. A format that yields a substantially smaller file has either discarded information or exploited genuine redundancy in the data; the distinction should be established for each case.

Image origin conventions

Top-left versus bottom-left image origin, and the vertical flip between them
Image origin conventions and the resulting vertical mirror. Conventional picture formats store the top row first. MRC data are by convention displayed with row 0 at the bottom, the crystallographic convention followed by its viewers (IMOD, RELION, EMAN). The same data therefore appear mirrored top to bottom depending on which convention the reading software applies. A vertical mirror reverses the handedness, so that defocus gradients, tilt axes, drift directions and the chirality of a reconstruction are all inverted. If an image appears correct but a derived quantity has the wrong sign, this is the first point to verify.

HDF5 (.h5, .hdf5): the native format of 4d

HDF5 is a hierarchical container rather than an image format. A file holds a tree of groups (analogous to directories), datasets (n-dimensional arrays of any type) and attributes (metadata attached to either). This generality allows a complete 4D-STEM acquisition, including scan positions, diffraction patterns, masks and precomputed maps, to be stored in one file.

CompressionPer-chunk filters, all lossless: gzip/Deflate, Blosc2, bitshuffle + LZ4, Zstd, LZF, with optional byte-shuffle pre-filters. The chunking also determines the read speed: a chunk shaped like one diffraction pattern allows 4d to load a single pattern without reading the remainder of the dataset.
Bit depthAny machine type, including 64-bit floats and complex numbers. Datasets can be extendable and sparse.
OriginUndefined by the format. The convention is determined by the layout used by the writing software.
HeaderA superblock with B-trees and heaps, handled by the library. Scientific metadata are stored in user-defined attributes, so the layout must be agreed upon by convention: the 4d convention, EMD, NeXus.
1024 × 1024Raw size plus a few kilobytes of structure. Compression of counting-mode data, in which most pixels are zero, routinely yields factors of 5–10; for dense 16-bit data, factors of 1.1–2 are typical.

4d stores datasets in its version 2.0 convention with Blosc2 compression. The group layout is described in §7 of the main manual.

MRC (.mrc, .mrcs, .map)

MRC is the standard format of cryo-EM. It consists of a fixed 1024-byte header, an optional extended header whose length is stated in the fixed header, and the raw pixel data. It stores 2D images, image stacks (.mrcs) and 3D volumes (.map) in the same structure, so that a reconstruction and a particle stack are read identically.

CompressionNone. The format stores raw values, which makes it fast to memory-map and produces large files. Whole files are frequently gzipped for transfer (.mrc.gz); variants such as MRCZ add Blosc compression but are not part of the standard.
Bit depthDetermined by the mode field: 0 = int8, 1 = int16, 2 = float32, 3 and 4 = complex, 6 = uint16, 12 = float16. Mode 2 is the default for reconstructions, mode 6 for raw camera frames.
OriginBottom-left by convention; see the figure above.
Header1024 bytes of fixed fields: image size (nx, ny, nz), mode, start indices, sampling (mx, my, mz), the unit-cell dimensions from which the pixel size is derived, axis order, minimum, maximum, mean and RMS of the data, space group, size of the extended header, the origin, the MAP stamp, a machine stamp specifying the byte order, and up to ten 80-character text labels. An optional extended header follows, used by Thermo Fisher (FEI1/FEI2) and SerialEM for per-frame metadata such as tilt angle, dose and stage position.
1024 × 10241.05 MB (mode 0), 2.10 MB (modes 1/6), 4.19 MB (mode 2), each plus the 1 KB header.
Header specification. The current definition is MRC2014, maintained by CCP-EM: www.ccpem.ac.uk/mrc_format/mrc2014.php provides the field-by-field table (word number, type, meaning). It is described in Cheng et al. (2015), Journal of Structural Biology, "MRC2014: Extensions to the MRC format header for electron cryo-microscopy and tomography". The closely related CCP4 map format is documented at www.ccp4.ac.uk/html/maplib.html. If a file opens with implausible values, the machine stamp should be checked: MRC files can be written in big- or little-endian byte order, and older files occasionally declare the wrong one.
In 4d, an MRC map or stack with more than one section opens in the File Preview of the Manual Processing tab as a rotatable 3D volume — surface, projection or slice, with animation; see the volume viewer.

TIFF (.tif, .tiff)

TIFF is a tagged container rather than a single format. An 8-byte header points to a chain of image file directories, each a list of tags describing one image. This flexibility makes it the usual export format of camera software, and it is also the reason that two TIFF files may require entirely different code to read.

CompressionOptional and normally lossless: LZW, Deflate (ZIP), PackBits run-length; newer writers add Zstd and LERC. JPEG compression within a TIFF is possible and lossy; it should not be used for data. On noisy micrographs, lossless compression typically reduces the size by only 5–20 %.
Bit depth1 to 64 bits, unsigned, signed or floating point, with one or more samples per pixel. TIFF is one of the few general-purpose formats that stores 32-bit float directly.
OriginTop-left by default. Tag 274 (Orientation) can declare any of eight arrangements; many scientific readers ignore it, so a file that depends on it may appear mirrored.
HeaderByte-order mark (II or MM), magic number 42 (43 for BigTIFF), and the offset of the first directory. Tags then specify width, height, bits per sample, sample format, compression, strip or tile layout, X/Y resolution and unit. Free-text tags (ImageDescription, Software, DateTime) are used by ImageJ, SerialEM and camera vendors to store pixel size, dose and acquisition settings, often as XML or in a private tag block.
1024 × 10241.05 / 2.10 / 4.19 MB raw at 8 / 16 / 32 bit, plus a few hundred bytes of tags; ≈ 1.6–2.0 MB for a noisy 16-bit image with Deflate.
Files above 4 GB require BigTIFF, which uses 64-bit offsets. Classical TIFF cannot address beyond this limit, which is why long movies fail to save without warning in some software.

EER (.eer): Electron Event Representation

EER is based on a different representation of the image. Instead of integrating the electrons arriving during a frame into pixel counts, a Falcon 4-class detector records each detected electron as an event: its position, refined to a sub-pixel grid, and the frame in which it arrived. The file is a losslessly compressed list of these events.

CompressionLossless and intrinsic: the gaps between successive events are run-length encoded and entropy coded. Since a low-dose frame is almost entirely empty, this is far more compact than storing predominantly zero pixel arrays, by an order of magnitude or more relative to an equivalent raw movie.
Bit depthNot applicable: there are no pixel values, only event positions. The image is generated by rendering the events onto a grid of chosen sampling, either the native sensor sampling or 2× / 4× finer using the sub-pixel positions, with as many frames per rendered image as the dose requires.
OriginTop-left, as implied by the TIFF container; in practice the convention is fixed by the rendering library.
HeaderEER is wrapped in a TIFF structure, so standard TIFF tags describe the frames, private tags carry the compressed event bitstreams, and an XML block holds the acquisition metadata: sensor size, number of frames, frame rate, dose rate.
SizeProportional to the number of detected electrons, not to the pixel count; approximately one to two bytes per electron. A complete low-dose exposure typically occupies a few hundred MB, whereas the equivalent unpacked movie occupies several GB. Expressed per 1024 × 1024 area at 1 Å pixels and 40 e−/Ų, the events of the complete exposure amount to a few tens of MB, compared with 2.1 MB for the single 16-bit integrated image rendered from them.

The practical consequence is that the EER file should be retained. Dose weighting, motion correction and the choice of fractionation can all be repeated from the events, which is not possible once they have been integrated into fixed frames.

PNG (.png)

CompressionLossless Deflate, preceded by a per-row prediction filter (each row is coded as its difference from the row above or the pixel to the left). Efficient for smooth images and line art, inefficient for noise.
Bit depth8 or 16 bit, greyscale or colour, with an optional alpha channel. No floating point and no negative values, so a phase map must be scaled into an integer range before storage.
OriginTop-left; rows are stored top to bottom (optionally interlaced).
HeaderAn 8-byte signature followed by chunks. IHDR carries width, height, bit depth and colour type; pHYs can record the physical pixel size; tEXt/iTXt hold free text. There is no standard field for microscope metadata, which are therefore usually lost.
1024 × 1024≈ 0.7–1.0 MB for a noisy 8-bit micrograph, ≈ 1.5–2.0 MB at 16 bit; a smooth or largely empty image can be a tenth of that.

PNG is the appropriate format for figures, screenshots and content intended for a manuscript or a manual: it is lossless, universally readable and does not introduce artefacts as JPEG does. It is not suitable for data, since the calibration is not stored with the image.

JPEG (.jpg, .jpeg)

CompressionLossy. The image is divided into 8 × 8 blocks, cosine-transformed, and the high-frequency coefficients are quantised. At quality 90 the loss is not visible in a photograph; in a micrograph it removes the fine detail that constitutes the measurement and introduces block edges and ringing around strong features.
Bit depth8 bit per channel. A 16-bit detector image must first be scaled to 256 levels, which alone excludes the format from quantitative use.
OriginTop-left. The EXIF Orientation tag may request a rotation or mirror, and viewers are inconsistent in applying it.
HeaderMarker segments: JFIF (version, pixel density), optionally EXIF (camera, date, exposure, orientation, GPS, thumbnail), the quantisation and Huffman tables, and the frame header with the dimensions.
1024 × 1024≈ 0.2–0.7 MB at quality 90 for a noisy image. Noise reduces the compression efficiency, so a micrograph is several times larger than a photograph of the same size. Quality 50 halves the size again, with clearly visible artefacts.
JPEG is suitable for a preview, a slide or an e-mail, but not for archiving a measurement. Each re-saving of a JPEG compounds the loss.

GIF (.gif)

CompressionLossless LZW, applied only after the image has been reduced to a palette of at most 256 colours, which for a micrograph means 256 grey levels. The information loss occurs in the quantisation, not in the compression.
Bit depth8-bit palette indices. One palette entry can be transparent; there is no alpha channel and no 16-bit mode.
OriginTop-left, with each frame positioned by its own left/top offsets within the logical screen.
HeaderGIF87a/GIF89a signature, a logical screen descriptor (size, background colour, palette flags), the global colour table, and per-frame descriptors with delay times and disposal methods for animation.
1024 × 1024≈ 0.6–1.0 MB for a noisy greyscale image; LZW can exceed the raw size on pure noise. A few tens of kilobytes for a plot or a diagram.

The remaining application of GIF is a short animation that plays in any viewer, such as a tilt series or a through-focus series for a presentation. For all other purposes, PNG is superior in every respect.

Other formats

DM3 / DM4Gatan DigitalMicrograph. A tagged tree carrying the image together with comprehensive microscope metadata. DM4 uses 64-bit offsets for large files.
SER / EMIThermo Fisher (formerly FEI) TIA. The .ser file holds the data, the companion .emi file the metadata; the pair must be kept together.
EMDElectron Microscopy Datasets: a documented convention within HDF5, readable by any HDF5 tool. Also the native format of Velox.
NeXusA further HDF5 convention, originating from neutron and synchrotron science and increasingly used for electron data.
SMV / ADSCA simple header-plus-data format from X-ray detectors, used by some electron-diffraction pipelines.
TVIPSMovie format of TVIPS cameras: a header followed by concatenated frames.
OME-TIFFTIFF with a standardised XML metadata block in the ImageDescription tag; the light-microscopy solution to the metadata problem.

Recommendations