{"id":50,"date":"2008-09-02T14:46:06","date_gmt":"2008-09-02T19:46:06","guid":{"rendered":"http:\/\/stoney.sb.org\/wordpress\/?p=50"},"modified":"2009-06-03T18:41:23","modified_gmt":"2009-06-03T23:41:23","slug":"exporting-pixel-perfect-graphics-in-mathematica","status":"publish","type":"post","link":"https:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/","title":{"rendered":"Exporting Pixel-Perfect Graphics in Mathematica"},"content":{"rendered":"<p>I keep having to figure out how to export pixel-perfect raster images from Mathematica, which I use for test images with my software. I found that it&#8217;s simpler to use ArrayPlot than Raster now, so I&#8217;m documenting this for my future use (and any one else&#8217;s).<br \/>\n<!--more-->Here&#8217;s a simple image function that generates a grid of black pixels alternating with nearly-transparent red pixels. I use this sort of image to ensure that I&#8217;m sampling correctly.<\/p>\n<pre>\nimageFunction[x_, y_] :=\n If[BitAnd[x, 1] == BitAnd[y, 1],\n  {0, 0, 0, 1},\n  {1, 0, 0, 0.01}]\n<\/pre>\n<p>Table then makes a 256 x 128 array of RGBA values from the image function. This is non-square for this example to ensure that the width and height are in the right order later.<\/p>\n<pre>\nimageData = Table[\n   imageFunction[x, y],\n   {y, 0, 127}, {x, 0, 255}];\n<\/pre>\n<p>ArrayPlot makes the pixel array viewable by converting it into a Graphics. The ColorFunction produces RGBA pixels (the default ignores alpha), PixelConstrained aligns the image to exact pixel boundaries, and ImageSize produces a result that&#8217;s exactly the right size.<\/p>\n<pre>\napic = ArrayPlot[\n  imageData,\n  ColorFunction -> RGBColor,\n  PixelConstrained -> True,\n  ImageSize -> Reverse[Take[Dimensions[imageData], 2]]]\n<\/pre>\n<p>This produces the image:<\/p>\n<p>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/stoney.sb.org\/wordpress\/wp-content\/apic.png?resize=256%2C128\" alt=\"APic.png\" border=\"0\" width=\"256\" height=\"128\" \/>\n<\/p>\n<p>Which is then exported like this:<\/p>\n<pre>\nExport[\"~\/Desktop\/APic.tif\", apic];\n<\/pre>\n<p>The result is a pixel-perfect file resulting from a raster image defined in Mathematica.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I keep having to figure out how to export pixel-perfect raster images from Mathematica, which I use for test images with my software. I found that it&#8217;s simpler to use ArrayPlot than Raster now, so I&#8217;m documenting this for my future use (and any one else&#8217;s).<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[17,13,20,14],"tags":[],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Exporting Pixel-Perfect Graphics in Mathematica - Stoney&#039;s Zone<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Exporting Pixel-Perfect Graphics in Mathematica - Stoney&#039;s Zone\" \/>\n<meta property=\"og:description\" content=\"I keep having to figure out how to export pixel-perfect raster images from Mathematica, which I use for test images with my software. I found that it&#8217;s simpler to use ArrayPlot than Raster now, so I&#8217;m documenting this for my future use (and any one else&#8217;s).\" \/>\n<meta property=\"og:url\" content=\"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/\" \/>\n<meta property=\"og:site_name\" content=\"Stoney&#039;s Zone\" \/>\n<meta property=\"article:published_time\" content=\"2008-09-02T19:46:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2009-06-03T23:41:23+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/stoney.sb.org\/wordpress\/wp-content\/apic.png\" \/>\n<meta name=\"author\" content=\"stoney\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"stoney\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/\",\"url\":\"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/\",\"name\":\"Exporting Pixel-Perfect Graphics in Mathematica - Stoney's Zone\",\"isPartOf\":{\"@id\":\"https:\/\/stoney.sb.org\/wordpress\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/stoney.sb.org\/wordpress\/wp-content\/apic.png\",\"datePublished\":\"2008-09-02T19:46:06+00:00\",\"dateModified\":\"2009-06-03T23:41:23+00:00\",\"author\":{\"@id\":\"https:\/\/stoney.sb.org\/wordpress\/#\/schema\/person\/d587bd16a4b79558c4f6e01c078fbdb1\"},\"breadcrumb\":{\"@id\":\"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/#primaryimage\",\"url\":\"http:\/\/stoney.sb.org\/wordpress\/wp-content\/apic.png\",\"contentUrl\":\"http:\/\/stoney.sb.org\/wordpress\/wp-content\/apic.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/stoney.sb.org\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Exporting Pixel-Perfect Graphics in Mathematica\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/stoney.sb.org\/wordpress\/#website\",\"url\":\"https:\/\/stoney.sb.org\/wordpress\/\",\"name\":\"Stoney's Zone\",\"description\":\"Thoughts about stuff\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/stoney.sb.org\/wordpress\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/stoney.sb.org\/wordpress\/#\/schema\/person\/d587bd16a4b79558c4f6e01c078fbdb1\",\"name\":\"stoney\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stoney.sb.org\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/47ff741e5568d1283ae1d29cfa4248fa?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/47ff741e5568d1283ae1d29cfa4248fa?s=96&d=mm&r=pg\",\"caption\":\"stoney\"},\"sameAs\":[\"http:\/\/\"],\"url\":\"https:\/\/stoney.sb.org\/wordpress\/author\/stoney\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Exporting Pixel-Perfect Graphics in Mathematica - Stoney's Zone","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/","og_locale":"en_US","og_type":"article","og_title":"Exporting Pixel-Perfect Graphics in Mathematica - Stoney's Zone","og_description":"I keep having to figure out how to export pixel-perfect raster images from Mathematica, which I use for test images with my software. I found that it&#8217;s simpler to use ArrayPlot than Raster now, so I&#8217;m documenting this for my future use (and any one else&#8217;s).","og_url":"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/","og_site_name":"Stoney's Zone","article_published_time":"2008-09-02T19:46:06+00:00","article_modified_time":"2009-06-03T23:41:23+00:00","og_image":[{"url":"http:\/\/stoney.sb.org\/wordpress\/wp-content\/apic.png","type":"","width":"","height":""}],"author":"stoney","twitter_card":"summary_large_image","twitter_misc":{"Written by":"stoney","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/","url":"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/","name":"Exporting Pixel-Perfect Graphics in Mathematica - Stoney's Zone","isPartOf":{"@id":"https:\/\/stoney.sb.org\/wordpress\/#website"},"primaryImageOfPage":{"@id":"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/#primaryimage"},"image":{"@id":"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/#primaryimage"},"thumbnailUrl":"http:\/\/stoney.sb.org\/wordpress\/wp-content\/apic.png","datePublished":"2008-09-02T19:46:06+00:00","dateModified":"2009-06-03T23:41:23+00:00","author":{"@id":"https:\/\/stoney.sb.org\/wordpress\/#\/schema\/person\/d587bd16a4b79558c4f6e01c078fbdb1"},"breadcrumb":{"@id":"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/#primaryimage","url":"http:\/\/stoney.sb.org\/wordpress\/wp-content\/apic.png","contentUrl":"http:\/\/stoney.sb.org\/wordpress\/wp-content\/apic.png"},{"@type":"BreadcrumbList","@id":"http:\/\/stoney.sb.org\/wordpress\/exporting-pixel-perfect-graphics-in-mathematica\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/stoney.sb.org\/wordpress\/"},{"@type":"ListItem","position":2,"name":"Exporting Pixel-Perfect Graphics in Mathematica"}]},{"@type":"WebSite","@id":"https:\/\/stoney.sb.org\/wordpress\/#website","url":"https:\/\/stoney.sb.org\/wordpress\/","name":"Stoney's Zone","description":"Thoughts about stuff","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/stoney.sb.org\/wordpress\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/stoney.sb.org\/wordpress\/#\/schema\/person\/d587bd16a4b79558c4f6e01c078fbdb1","name":"stoney","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stoney.sb.org\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/47ff741e5568d1283ae1d29cfa4248fa?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/47ff741e5568d1283ae1d29cfa4248fa?s=96&d=mm&r=pg","caption":"stoney"},"sameAs":["http:\/\/"],"url":"https:\/\/stoney.sb.org\/wordpress\/author\/stoney\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p76dm1-O","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/posts\/50"}],"collection":[{"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":4,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"predecessor-version":[{"id":108,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/posts\/50\/revisions\/108"}],"wp:attachment":[{"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}