JPEG (.jpg, .jpeg)
Background & Context
-
- MIME type: image/jpeg
- JPEG raster image and compression format.
- Commonly used for photographic images by digital cameras and on the web.
- JPEG is an acronym for Joint Photographic Experts Group.
- Supports Grayscale, RGB, and CMYK color spaces.
- Stores images as bitmaps at a resolution of 8 bits per color channel.
- Binary format.
- Uses a lossy compression method based on an 8×8 block-sized discrete cosine transform.
- Supports different compression levels.
Import & Export
- Import["file.jpg"] imports a JPEG file, returning an image.
- Import["file.jpg",elem] imports the specified element from a JPEG file.
- The import format can be specified with Import["file","JPEG"] or Import["file",{"JPEG",elem,…}].
- Export["file.jpg",expr] exports an image, graphics, or any other expression as an image to a JPEG file.
- Image objects are by default exported at their full raster size. Graphics and other arbitrary expressions are exported using the same raster size as their displayed form.
- See the following reference pages for full general information:
-
Import, Export import from or export to a file CloudImport, CloudExport import from or export to a cloud object ImportString, ExportString import from or export to a string ImportByteArray, ExportByteArray import from or export to a byte array
Notebook Interface
- In the notebook front end, Insert ▶ Picture and the Open menu allow the import of a JPEG file into a cell.
- Save Selection As exports the selected part of a notebook as a JPEG file.
Import Elements
- General Import elements:
-
"Elements" list of elements and options available in this file "Summary" summary of the file "Rules" list of rules for all available elements - Data representation elements:
-
"ColorMap" color reduction palette "Data" array of pixel values "Graphics" raster image, given as a Graphics object "Image" raster image, given as an Image object "RawData" array of color map indices "Thumbnail" thumbnail of the first image - Import and Export by default use the "Image" element for the JPEG format.
- Subelements for partial data import for any element elem can take row and column specifications in the form {elem,rows,cols}, where rows and cols can be any of the following:
-
n nth row or column -n counts from the end n;;m from n through m n;;m;;s from n through m with steps of s {n1,n2,…} specific rows or columns ni - Advanced Import elements:
-
"BitDepth" bits used to represent each color channel in the file "CameraTopOrientation" orientation of the camera when the picture was taken "Channels" the number of color channels used in the file "ColorProfileData" embedded color profile, given as a ColorProfileData object "ColorSpace" color encoding used in the file "FlashUsed" whether the flash was fired "GeoPosition" latitude and longitude represented as a GeoPosition object "GPSDateTime" image creation date and time registered by GPS "RasterSize" raster dimensions "RedEyeCorrection" whether a red-eye correction was performed - Metadata elements:
-
"Exif" formatted Exif (Exchange image file format) "IPTC" formatted IPTC (International Press Telecommunications Council) "XMP" formatted XMP (Extensible metadata platform) "MetaInformation" combination of all formatted metadata present in the file - Raw metadata, as stored in the file, can be imported using "RawExif", "RawXMP" and "RawIPTC" elements which are returned in an association.
- All Exif and IPTC tags can be imported individually. Common tags include:
-
"ApertureValue" lens aperture "Artist" photographer or image creator "DateTime" image creation date and time "ExposureTime" exposure time, given in seconds "FlashInfo" flash information: flash fired, red-eye correction, etc. "FNumber" F number "FocalLength" actual focal length of the lens, given in millimeters "GPSAltitude" altitude of the GPS position "GPSLatitude" latitude of the GPS position "GPSLongitude" longitude of the GPS position "Make" manufacturer of the recording equipment "Model" model name or model number of the equipment "Software" name and version of the software or firmware of the camera or image input device used to generate the image
Import Options
- Import options include:
-
ImageResolution Automatic resolution values for import "ImageTopOrientation" Automatic orientation of the image as stored in the file IncludeMetaInformation All metadata types to import and export RasterSize Automatic dimensions of the image - Possible settings for "ImageTopOrientation" are Automatic, Top, Bottom, Left or Right.
Export Options
- Export to JPEG has the same options as Rasterize, with the following additions and changes:
-
ColorSpace "RGB" color space to be used in the file CompressionLevel 0.25 sets the JPEG compression parameter ImageFormattingWidth $ImageFormattingWidth target width when formatting an object ImageResolution Automatic resolution values for export "ImageTopOrientation" Automatic orientation of the image as stored in the file IncludeMetaInformation All metadata types to import and export "Progressive" False whether to store the image in a way that allows progressive display while the file is being loaded RasterSize Automatic dimensions of the image "Smoothing" 0 the JPEG smoothing parameters, given as a number in the range 0 (no smoothing) to 1 - Supported ColorSpace settings are: "Grayscale", "RGB", and "CMYK". Using ColorSpace->Automatic, Export attempts to create a JPEG file with the same color space.
- The value of CompressionLevel can be in the range from 0 to 1, with larger values resulting in smaller file sizes but lower-quality encodings.
Examples
open all close allBasic Examples (3)
Scope (3)
Import the image as a graphics:
Import["ExampleData/coneflower.jpg", "Graphics"]Head[%]Import["ExampleData/coneflower.jpg", "Exif"]Import["ExampleData/coneflower.jpg", "DateTime"]Import multiple metadata elements:
Import["ExampleData/coneflower.jpg", {{"Make", "Model"}}]Import Elements (35)
Available Elements (4)
Import["ExampleData/coneflower.jpg", "Elements"]List of available Exif elements:
Import["ExampleData/coneflower.jpg", {"Exif", "Elements"}]//ShortList of available IPTC elements:
Import["ExampleData/coneflower.jpg", {"IPTC", "Elements"}]//ShortGet image-related information as list of rules:
Import["ExampleData/coneflower.jpg", "Rules"]//ShortData Representation (8)
"ColorMap" (2)
Get the color-reduction palette created on import:
(map = Import["ExampleData/coneflower.jpg", "ColorMap"])//ShortVisualize the color-reduction palette:
MapThread[Rule, {Range[1, 256], RGBColor@@@(Import["ExampleData/coneflower.jpg", "ColorMap"] / 255.)}]Apply the color map to the raw data to create the stored image:
data = Import["ExampleData/coneflower.jpg", "RawData"];Image[data /. MapThread[Rule, {Range[1, 256], Import["ExampleData/coneflower.jpg", "ColorMap"]}], "Byte"]"Data" (1)
"Graphics" (1)
Import as a Graphics object:
Import["ExampleData/coneflower.jpg", "Graphics"]Head[%]"Image" (2)
Import as an Image object:
Import["ExampleData/coneflower.jpg", "Image"]Head[%]This is the default import element for JPEG:
Import["ExampleData/coneflower.jpg"]Import only the specified rows:
Import["ExampleData/coneflower.jpg", {"Image", 40 ;; 80}]Import specified rows and columns:
Import["ExampleData/coneflower.jpg", {"Image", 40 ;; 80, ;; 100}]"RawData" (1)
data = Import["ExampleData/coneflower.jpg", "RawData"]ArrayPlot[data]map = MapThread[Rule, {Range[1, 256], RGBColor@@@(Import["ExampleData/coneflower.jpg", "ColorMap"] / 255.)}]Use the color map to create the stored image:
Image[data /. map]Metadata (23)
"BitDepth" (1)
"CameraTopOrientation" (2)
Get the camera orientation at the capture time:
Import["ExampleData/girlcloseup.jpg", "CameraTopOrientation"]The image is auto-rotated on Import:
image = Import["ExampleData/girlcloseup.jpg", ImageSize -> Small]An auto-rotated image has an updated Exif tag:
(MetaInformation /. Options[image])["Exif", "Image", "Orientation", "CameraTopOrientation"]Avoid auto-rotation by setting the "ImageTopOrientation" option:
Import["ExampleData/girlcloseup.jpg", "ImageTopOrientation" -> Top, ImageSize -> Small]"Channels" (1)
"ColorProfileData" (1)
Get the embedded color profile, given as a ColorProfileData object:
Import["ExampleData/coneflower.jpg", "ColorProfileData"]The color profile is stored in the ColorSpace option of the imported image:
Options[Import["ExampleData/coneflower.jpg"], ColorSpace]"ColorSpace" (1)
Get the color encoding used on export:
Import["ExampleData/coneflower.jpg", "ColorSpace"]Use ImageColorSpace to get the color space of an imported image:
Import["ExampleData/coneflower.jpg"]//ImageColorSpace"DateTime" (1)
"Exif" (3)
Import["ExampleData/coneflower.jpg", "Exif"]Extract an individual tag from the association:
Import["ExampleData/coneflower.jpg", "Exif"]["Image", "FNumber"]Individual Exif tags can also be imported directly:
Import["ExampleData/coneflower.jpg", "FNumber"]Some Exif tags are processed to give more detailed information:
Import["ExampleData/coneflower.jpg", "FlashInfo"]Compare with the raw "Flash" tag:
Import["ExampleData/coneflower.jpg", "RawExif"]["Image", "Flash"]"FlashUsed" (1)
Import a tag that shows whether the flash fired:
Import["ExampleData/coneflower.jpg", "FlashUsed"]"FlashUsed" is a subelement of the "FlashInfo" element:
Import["ExampleData/coneflower.jpg", "Exif"]["Image", "FlashInfo"]Extract "FlashUsed" from the full "Exif":
Import["ExampleData/coneflower.jpg", "Exif"]["Image", "FlashInfo", "FlashUsed"]"GeoPosition" (2)
loc = Import["ExampleData/applepicking.jpg", "GeoPosition"]GeoGraphics[GeoMarker[loc]]Use the image thumbnail as the marker:
GeoGraphics[GeoMarker[loc, Import["ExampleData/applepicking.jpg", "Thumbnail"], "Scale" -> .05]]The image geo position may be unavailable:
Import["ExampleData/coneflower.jpg", "GeoPosition"]"GPSDateTime" (1)
"MetaInformation" (1)
"RasterSize" (1)
Get the dimensions of the image stored in the file:
Import["ExampleData/coneflower.jpg", "RasterSize"]Using ImageDimensions on an imported image gives the same result:
Import["ExampleData/coneflower.jpg"]//ImageDimensions"RawExif" (1)
Import["ExampleData/coneflower.jpg", "RawExif"]Raw Exif tags return the actual values stored in the file:
Import["ExampleData/coneflower.jpg", "RawExif"]["Image", "FocalLength"]When importing "Exif", most tags are processed:
Import["ExampleData/coneflower.jpg", "Exif"]["Image", "FocalLength"]"RawIPTC" (1)
"RawXMP" (1)
"RedEyeCorrection" (1)
"Summary" (1)
Import Options (6)
ImageResolution (1)
By default, the image resolution is extracted from the file:
Options[Import["ExampleData/coneflower.jpg"], ImageResolution]Specify the resolution of the imported image:
Options[Import["ExampleData/coneflower.jpg", ImageResolution -> 144], ImageResolution]Specify different resolutions in
and
directions:
Options[Import["ExampleData/coneflower.jpg", ImageResolution -> {72, 144}], ImageResolution]"ImageTopOrientation" (1)
By default, images are imported with the native orientation stored in the file:
Import["ExampleData/coneflower.jpg"]Specify the side that is supposed to be on the top:
Import["ExampleData/coneflower.jpg", "ImageTopOrientation" -> Left]The correct orientation can also be achieved by rotating the image to be upright:
ImageRotate[Import["ExampleData/coneflower.jpg"], Left -> Top]IncludeMetaInformation (3)
By default, all available metadata is imported:
Options[Import["ExampleData/coneflower.jpg"], MetaInformation]//ShortPossible classes of metadata for JPEG files are "Exif", "IPTC", and "XMP":
Options[Import["ExampleData/coneflower.jpg"], MetaInformation][[1, 2]]//KeysImport the image with no metadata:
Options[Import["ExampleData/coneflower.jpg", IncludeMetaInformation -> None], MetaInformation]Import the image with "XMP" metadata only:
Options[Import["ExampleData/coneflower.jpg", IncludeMetaInformation -> {"XMP"}], MetaInformation]//ShortRasterSize (1)
Export Options (7)
ColorSpace (1)
By default, the native color space of the image is used for export:
i = [image];Export["image.jpg", i]Import[%, "ColorSpace"]Export["image.jpg", i, ColorSpace -> "CMYK"]Import[%, "ColorSpace"]CompressionLevel (2)
By default, an automatic compression is used:
i = ExampleData[{"TestImage", "Apples"}];ExportString[i, "JPEG"]//ByteCountExportString[i, "JPEG", "CompressionLevel" -> 1]//ByteCountExportString[i, "JPEG", "CompressionLevel" -> 0]//ByteCountCompare the byte count for different compression levels:
Table[{c, ByteCount@ExportString[i, "JPEG", "CompressionLevel" -> c]}, {c, 0, 1, .1}]//ListLinePlotImageResolution (1)
By default, the original image resolution is used during export:
i = Import["ExampleData/coneflower.jpg"]Options[i, ImageResolution]Export["image.jpg", i]Options[Import["image.jpg"], ImageResolution]Specify the resolution of the exported image:
Export["image.jpg", i, ImageResolution -> 72]Options[Import["image.jpg"], ImageResolution]"ImageTopOrientation" (1)
By default, the upright image is exported using "ImageTopOrientation"->Top:
i = [image];Export["image.jpg", i]Import[%]Export["image.jpg", i, "ImageTopOrientation" -> Left]Import["image.jpg"]Import using the original orientation:
Import["image.jpg", "ImageTopOrientation" -> Top]IncludeMetaInformation (1)
By default, all existing meta-information is exported:
image = [image];Export["image.jpg", image]Import[%]//OptionsSpecify the metadata to include:
Export["image.jpeg", image, IncludeMetaInformation -> "XMP"]Import[%]//OptionsExport["image.jpeg", image, IncludeMetaInformation -> None]Import[%]//OptionsRasterSize (1)
By default, the image is exported in the full resolution:
i = [image];ImageDimensions[i]Export["test.jpg", i]Import[%, "RasterSize"]Specify the raster size of the exported image:
Export["test.jpg", i, RasterSize -> 47]Import[%, "RasterSize"]Tech Notes
Related Guides
History
Introduced in 1999 (4.0) | Updated in 2016 (11.0) ▪ 2020 (12.2) ▪ 2022 (13.1) ▪ 2022 (13.2) ▪ 2026 (15.0)