EXIF
How Jimp reads, rotates and writes EXIF metadata.
Cameras and phones store metadata in JPEG files as EXIF: the camera make and model, the resolution (DPI), the date, the orientation and often the GPS location. Jimp reads EXIF from JPEG images, uses it to turn photos upright, and can write it back when you save a JPEG. Other formats are read without EXIF.
Auto-rotation on read
Cameras often store the pixels sideways and record how to turn them in the EXIF Orientation tag.
When Jimp.read or Jimp.fromBuffer loads a JPEG with an orientation other than 1, Jimp rotates and flips the pixels so the image is upright, then sets the orientation to 1.
image.width and image.height are the size after rotation.
import { Jimp } from "jimp";
// Stored as 3000x4000 with orientation 6 (rotate 90° clockwise)const image = await Jimp.read("photo.jpg");
image.width; // 4000image.height; // 3000| Orientation | To show the image upright, Jimp… | Width and height swap |
|---|---|---|
1 |
does nothing | no |
2 |
mirrors it horizontally | no |
3 |
rotates it 180° | no |
4 |
mirrors it vertically | no |
5 |
mirrors it horizontally, rotates 270° clockwise | yes |
6 |
rotates it 90° clockwise | yes |
7 |
mirrors it horizontally, rotates 90° clockwise | yes |
8 |
rotates it 270° clockwise | yes |
Because the pixels are already upright, methods such as rotate, flip and crop work on the image as people see it.
Turning auto-rotation off
Pass exifRotate: false to Jimp.read or Jimp.fromBuffer to keep the pixels as they’re stored in the file.
It goes in the same options object as the decode options:
import { Jimp } from "jimp";import { getExifOrientation } from "@jimp/core";
// Stored as 3000x4000 with orientation 6const image = await Jimp.read("photo.jpg", { exifRotate: false, "image/jpeg": { maxMemoryUsageInMB: 1024 },});
image.width; // 3000: the stored size, not swappedimage.height; // 4000getExifOrientation(image); // 6: the original orientationWith exifRotate: false:
- The pixels and the
Orientationtag are kept as stored. For orientations 5 to 8,image.widthandimage.heightare the stored size, so they’re swapped compared with the default read. getExifOrientation(image)from@jimp/corereturns the file’s orientation. With the default read it returns 1.- Writing with
exif: truekeeps the original orientation, so viewers still show the image upright. rotateandflipwork on the pixels as stored. If you write withexif: true, viewers apply the orientation on top of your rotation.
Use it when you handle the orientation yourself, for example to show the stored pixels next to the tag, or to re-encode a photo without moving its pixels.
The option’s type is ReadOptions, exported from jimp. getExifOrientation comes from @jimp/core, so install it alongside jimp to import it.
exifRotate was added in 2.0.
Reading EXIF data
The parsed EXIF is on image._exif, in the format exif-parser returns.
It isn’t part of Jimp’s typed API, so TypeScript needs a cast.
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");const { tags } = (image as any)._exif;
tags.Make; // "Canon"tags.XResolution; // 240tags.Orientation; // 1, as the pixels were rotated (the stored value with exifRotate: false)_exif is only set for JPEG images that have EXIF. clone copies it.
Changing _exif doesn’t change what Jimp writes: Jimp keeps the original EXIF bytes separately and writes those.
Writing EXIF data
By default Jimp writes no EXIF: the output has none of the original metadata, and its resolution is the encoder’s default.
To keep the metadata of a JPEG you read, pass exif: true when writing a JPEG with write or getBuffer:
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
image.resize({ w: 800 });
await image.write("small.jpg", { quality: 90, exif: true });// orconst buffer = await image.getBuffer("image/jpeg", { exif: true });Jimp writes the original EXIF block with these changes:
Orientationis set to 1, as the pixels were already rotated on read. Otherwise viewers would rotate the image a second time. If you read the image withexifRotate: false, the original orientation is kept instead.PixelXDimensionandPixelYDimension(andImageWidth/ImageLengthif present) are set to the image’s current size.- The embedded thumbnail is removed, as it would show the original image, including parts you cropped out.
- The JFIF resolution is set to match the EXIF one, so readers agree on the DPI.
Everything else, including maker notes and GPS location, is copied unchanged.
The EXIF travels with the image through edits and clones, so exif: true works after any number of method calls.
It also works with the WASM JPEG encoder from @jimp/wasm-jpeg.
The option does nothing for images without EXIF, such as ones read from a PNG or created with new Jimp().
Only JPEG output can carry EXIF. PNG, WebP, TIFF, GIF and BMP output never includes it.
Stripping metadata
Because Jimp writes no EXIF unless you ask, reading and saving a JPEG removes its metadata:
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
// Upright pixels, no EXIF (no GPS, camera or date)await image.write("clean.jpg", { quality: 90 });The image is re-encoded, so pick a quality that suits you; the default is 100.
See also
- Reading and writing for the other encode options of each format
rotateandflipto change the orientation yourself- Color Spaces: the ICC profile, which Jimp keeps whether or not you write EXIF
- Formats for what each format can store