{"route":"/en-US-v0.13.1/reference/visualize/image/","title":"Image","description":"Documentation for the `image` function.","part":null,"outline":[{"id":"summary","name":"Summary","children":[]},{"id":"example","name":"Example","children":[]},{"id":"parameters","name":"Parameters","children":[{"id":"parameters-source","name":"source","children":[]},{"id":"parameters-format","name":"format","children":[]},{"id":"parameters-width","name":"width","children":[]},{"id":"parameters-height","name":"height","children":[]},{"id":"parameters-alt","name":"alt","children":[]},{"id":"parameters-fit","name":"fit","children":[]},{"id":"parameters-scaling","name":"scaling","children":[]},{"id":"parameters-icc","name":"icc","children":[]}]},{"id":"definitions","name":"Definitions","children":[{"id":"definitions-decode","name":"Decode Image","children":[{"id":"definitions-decode-data","name":"data","children":[]},{"id":"definitions-decode-format","name":"format","children":[]},{"id":"definitions-decode-width","name":"width","children":[]},{"id":"definitions-decode-height","name":"height","children":[]},{"id":"definitions-decode-alt","name":"alt","children":[]},{"id":"definitions-decode-fit","name":"fit","children":[]},{"id":"definitions-decode-scaling","name":"scaling","children":[]}]}]}],"body":{"kind":"func","content":{"path":[],"name":"image","title":"Image","keywords":[],"oneliner":"A raster or vector graphic.","element":true,"contextual":false,"deprecation":null,"details":"<p>A raster or vector graphic.</p>\n<p>You can wrap the image in a <a href=\"/en-US-v0.13.1/reference/model/figure/\" title=\"`figure`\"><code>figure</code></a> to give it a number and caption.</p>\n<p>Like most elements, images are <em>block-level</em> by default and thus do not\nintegrate themselves into adjacent paragraphs. To force an image to become\ninline, put it into a <a href=\"/en-US-v0.13.1/reference/layout/box/\" title=\"`box`\"><code>box</code></a>.</p>\n<h2 id=\"example\">Example</h2>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-func\">#</span><span class=\"typ-func\">figure</span><span class=\"typ-punct\">(</span>\n  <span class=\"typ-func\">image</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;molecular.jpg&quot;</span><span class=\"typ-punct\">,</span> width<span class=\"typ-punct\">:</span> <span class=\"typ-num\">80%</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n  caption<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">[</span>\n    A step in the molecular testing\n    pipeline of our lab.\n  <span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/ce75a73e1e074f91aba6411c6e77cec4.png\" alt=\"Preview\"></div></div>","example":null,"self":false,"params":[{"name":"source","details":"<p>A <a href=\"/en-US-v0.13.1/reference/syntax/#paths\">path</a> to an image file or raw bytes making up an\nimage in one of the supported <a href=\"/en-US-v0.13.1/reference/visualize/image/#parameters-format\">formats</a>.</p>\n<p>Bytes can be used to specify raw pixel data in a row-major,\nleft-to-right, top-to-bottom format.</p>","example":"<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> original <span class=\"typ-op\">=</span> <span class=\"typ-func\">read</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;diagram.svg&quot;</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> changed <span class=\"typ-op\">=</span> original<span class=\"typ-punct\">.</span><span class=\"typ-func\">replace</span><span class=\"typ-punct\">(</span>\n  <span class=\"typ-str\">&quot;#2B80FF&quot;</span><span class=\"typ-punct\">,</span> <span class=\"typ-comment\">// blue</span>\n  green<span class=\"typ-punct\">.</span><span class=\"typ-func\">to-hex</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">image</span><span class=\"typ-punct\">(</span><span class=\"typ-func\">bytes</span><span class=\"typ-punct\">(</span>original<span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">image</span><span class=\"typ-punct\">(</span><span class=\"typ-func\">bytes</span><span class=\"typ-punct\">(</span>changed<span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/8c4900221f249ebaf7a8713761a0df51.png\" alt=\"Preview\"></div></div>","types":["str","bytes"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"format","details":"<p>The image's format.</p>\n<p>By default, the format is detected automatically. Typically, you thus\nonly need to specify this when providing raw bytes as the\n<a href=\"/en-US-v0.13.1/reference/visualize/image/#parameters-source\"><code>source</code></a> (even then, Typst will try to figure out the\nformat automatically, but that's not always possible).</p>\n<p>Supported formats are <code><span class=\"typ-str\">&quot;png&quot;</span></code>, <code><span class=\"typ-str\">&quot;jpg&quot;</span></code>, <code><span class=\"typ-str\">&quot;gif&quot;</span></code>, <code><span class=\"typ-str\">&quot;svg&quot;</span></code> as well\nas raw pixel data. Embedding PDFs as images is\n<a href=\"https://github.com/typst/typst/issues/145\">not currently supported</a>.</p>\n<p>When providing raw pixel data as the <code>source</code>, you must specify a\ndictionary with the following keys as the <code>format</code>:</p>\n<ul>\n<li><code>encoding</code> (<a href=\"/en-US-v0.13.1/reference/foundations/str/\" title=\"str\">str</a>): The encoding of the pixel data. One of:\n<ul>\n<li><code><span class=\"typ-str\">&quot;rgb8&quot;</span></code> (three 8-bit channels: red, green, blue)</li>\n<li><code><span class=\"typ-str\">&quot;rgba8&quot;</span></code> (four 8-bit channels: red, green, blue, alpha)</li>\n<li><code><span class=\"typ-str\">&quot;luma8&quot;</span></code> (one 8-bit channel)</li>\n<li><code><span class=\"typ-str\">&quot;lumaa8&quot;</span></code> (two 8-bit channels: luma and alpha)</li>\n</ul>\n</li>\n<li><code>width</code> (<a href=\"/en-US-v0.13.1/reference/foundations/int/\" title=\"int\">int</a>): The pixel width of the image.</li>\n<li><code>height</code> (<a href=\"/en-US-v0.13.1/reference/foundations/int/\" title=\"int\">int</a>): The pixel height of the image.</li>\n</ul>\n<p>The pixel width multiplied by the height multiplied by the channel count\nfor the specified encoding must then match the <code>source</code> data.</p>","example":"<div class=\"previewed-code\"><pre><code><span class=\"typ-func\">#</span><span class=\"typ-func\">image</span><span class=\"typ-punct\">(</span>\n  <span class=\"typ-func\">read</span><span class=\"typ-punct\">(</span>\n    <span class=\"typ-str\">&quot;tetrahedron.svg&quot;</span><span class=\"typ-punct\">,</span>\n    encoding<span class=\"typ-punct\">:</span> <span class=\"typ-key\">none</span><span class=\"typ-punct\">,</span>\n  <span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n  format<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;svg&quot;</span><span class=\"typ-punct\">,</span>\n  width<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2cm</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">image</span><span class=\"typ-punct\">(</span>\n  <span class=\"typ-func\">bytes</span><span class=\"typ-punct\">(</span><span class=\"typ-func\">range</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">16</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">map</span><span class=\"typ-punct\">(</span>x <span class=\"typ-op\">=&gt;</span> x <span class=\"typ-op\">*</span> <span class=\"typ-num\">16</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n  format<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">(</span>\n    encoding<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;luma8&quot;</span><span class=\"typ-punct\">,</span>\n    width<span class=\"typ-punct\">:</span> <span class=\"typ-num\">4</span><span class=\"typ-punct\">,</span>\n    height<span class=\"typ-punct\">:</span> <span class=\"typ-num\">4</span><span class=\"typ-punct\">,</span>\n  <span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n  width<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2cm</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/10ff96fc3a10fde3da637ec12b56637a.png\" alt=\"Preview\"></div></div>","types":["auto","str","dictionary"],"strings":[{"string":"png","details":"<p>Raster format for illustrations and transparent graphics.</p>"},{"string":"jpg","details":"<p>Lossy raster format suitable for photos.</p>"},{"string":"gif","details":"<p>Raster format that is typically used for short animated clips. Typst can\nload GIFs, but they will become static.</p>"},{"string":"svg","details":"<p>The vector graphics format of the web.</p>"}],"default":"<code><span class=\"typ-key\">auto</span></code>","positional":false,"named":true,"required":false,"variadic":false,"settable":true},{"name":"width","details":"<p>The width of the image.</p>","example":null,"types":["auto","relative"],"strings":[],"default":"<code><span class=\"typ-key\">auto</span></code>","positional":false,"named":true,"required":false,"variadic":false,"settable":true},{"name":"height","details":"<p>The height of the image.</p>","example":null,"types":["auto","relative","fraction"],"strings":[],"default":"<code><span class=\"typ-key\">auto</span></code>","positional":false,"named":true,"required":false,"variadic":false,"settable":true},{"name":"alt","details":"<p>A text describing the image.</p>","example":null,"types":["none","str"],"strings":[],"default":"<code><span class=\"typ-key\">none</span></code>","positional":false,"named":true,"required":false,"variadic":false,"settable":true},{"name":"fit","details":"<p>How the image should adjust itself to a given area (the area is defined\nby the <code>width</code> and <code>height</code> fields). Note that <code>fit</code> doesn't visually\nchange anything if the area's aspect ratio is the same as the image's\none.</p>","example":"<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">set</span> <span class=\"typ-func\">page</span><span class=\"typ-punct\">(</span>width<span class=\"typ-punct\">:</span> <span class=\"typ-num\">300pt</span><span class=\"typ-punct\">,</span> height<span class=\"typ-punct\">:</span> <span class=\"typ-num\">50pt</span><span class=\"typ-punct\">,</span> margin<span class=\"typ-punct\">:</span> <span class=\"typ-num\">10pt</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">image</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;tiger.jpg&quot;</span><span class=\"typ-punct\">,</span> width<span class=\"typ-punct\">:</span> <span class=\"typ-num\">100%</span><span class=\"typ-punct\">,</span> fit<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;cover&quot;</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">image</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;tiger.jpg&quot;</span><span class=\"typ-punct\">,</span> width<span class=\"typ-punct\">:</span> <span class=\"typ-num\">100%</span><span class=\"typ-punct\">,</span> fit<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;contain&quot;</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">image</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;tiger.jpg&quot;</span><span class=\"typ-punct\">,</span> width<span class=\"typ-punct\">:</span> <span class=\"typ-num\">100%</span><span class=\"typ-punct\">,</span> fit<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;stretch&quot;</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/a194706a6a99674a7fb55f288a8631c6.png\" alt=\"Preview\"></div></div>","types":["str"],"strings":[{"string":"cover","details":"<p>The image should completely cover the area (preserves aspect ratio by\ncropping the image only horizontally or vertically). This is the\ndefault.</p>"},{"string":"contain","details":"<p>The image should be fully contained in the area (preserves aspect\nratio; doesn't crop the image; one dimension can be narrower than\nspecified).</p>"},{"string":"stretch","details":"<p>The image should be stretched so that it exactly fills the area, even if\nthis means that the image will be distorted (doesn't preserve aspect\nratio and doesn't crop the image).</p>"}],"default":"<code><span class=\"typ-str\">&quot;cover&quot;</span></code>","positional":false,"named":true,"required":false,"variadic":false,"settable":true},{"name":"scaling","details":"<p>A hint to viewers how they should scale the image.</p>\n<p>When set to <code><span class=\"typ-key\">auto</span></code>, the default is left up to the viewer. For PNG\nexport, Typst will default to smooth scaling, like most PDF and SVG\nviewers.</p>\n<p><em>Note:</em> The exact look may differ across PDF viewers.</p>","example":null,"types":["auto","str"],"strings":[{"string":"smooth","details":"<p>Scale with a smoothing algorithm such as bilinear interpolation.</p>"},{"string":"pixelated","details":"<p>Scale with nearest neighbor or a similar algorithm to preserve the\npixelated look of the image.</p>"}],"default":"<code><span class=\"typ-key\">auto</span></code>","positional":false,"named":true,"required":false,"variadic":false,"settable":true},{"name":"icc","details":"<p>An ICC profile for the image.</p>\n<p>ICC profiles define how to interpret the colors in an image. When set\nto <code><span class=\"typ-key\">auto</span></code>, Typst will try to extract an ICC profile from the image.</p>","example":null,"types":["auto","str","bytes"],"strings":[],"default":"<code><span class=\"typ-key\">auto</span></code>","positional":false,"named":true,"required":false,"variadic":false,"settable":true}],"returns":["content"],"scope":[{"path":["image"],"name":"decode","title":"Decode Image","keywords":[],"oneliner":"Decode a raster or vector graphic from bytes or a string.","element":false,"contextual":false,"deprecation":"`image.decode` is deprecated, directly pass bytes to `image` instead","details":"<p>Decode a raster or vector graphic from bytes or a string.</p>","example":null,"self":false,"params":[{"name":"data","details":"<p>The data to decode as an image. Can be a string for SVGs.</p>","example":null,"types":["str","bytes"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"format","details":"<p>The image's format. Detected automatically by default.</p>","example":null,"types":["auto","str","dictionary"],"strings":[{"string":"png","details":"<p>Raster format for illustrations and transparent graphics.</p>"},{"string":"jpg","details":"<p>Lossy raster format suitable for photos.</p>"},{"string":"gif","details":"<p>Raster format that is typically used for short animated clips. Typst can\nload GIFs, but they will become static.</p>"},{"string":"svg","details":"<p>The vector graphics format of the web.</p>"}],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false},{"name":"width","details":"<p>The width of the image.</p>","example":null,"types":["auto","relative"],"strings":[],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false},{"name":"height","details":"<p>The height of the image.</p>","example":null,"types":["auto","relative","fraction"],"strings":[],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false},{"name":"alt","details":"<p>A text describing the image.</p>","example":null,"types":["none","str"],"strings":[],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false},{"name":"fit","details":"<p>How the image should adjust itself to a given area.</p>","example":null,"types":["str"],"strings":[{"string":"cover","details":"<p>The image should completely cover the area (preserves aspect ratio by\ncropping the image only horizontally or vertically). This is the\ndefault.</p>"},{"string":"contain","details":"<p>The image should be fully contained in the area (preserves aspect\nratio; doesn't crop the image; one dimension can be narrower than\nspecified).</p>"},{"string":"stretch","details":"<p>The image should be stretched so that it exactly fills the area, even if\nthis means that the image will be distorted (doesn't preserve aspect\nratio and doesn't crop the image).</p>"}],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false},{"name":"scaling","details":"<p>A hint to viewers how they should scale the image.</p>","example":null,"types":["auto","str"],"strings":[{"string":"smooth","details":"<p>Scale with a smoothing algorithm such as bilinear interpolation.</p>"},{"string":"pixelated","details":"<p>Scale with nearest neighbor or a similar algorithm to preserve the\npixelated look of the image.</p>"}],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false}],"returns":["content"],"scope":[]}]}}}