{"id":626,"date":"2005-03-24T23:25:18","date_gmt":"2005-03-24T23:25:18","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2005\/03\/grayscaling_images_with_perl-shtml\/"},"modified":"2005-03-24T23:25:18","modified_gmt":"2005-03-24T23:25:18","slug":"grayscaling_images_with_perl-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/grayscaling_images_with_perl-shtml\/","title":{"rendered":"Grayscaling Images With Perl"},"content":{"rendered":"<p>\nOne thing that caught my interest today was how to convert a colour image into grayscale.\n<\/p>\n<p>\nIt turns out the basic algorithm is very simple. Basically it&#8217;s just&#8230;\n<\/p>\n<div class=\"code\"><code>grey = 0.15 * red + 0.55 * green + 0.30 * blue;<br \/>\n<\/code><\/div>\n<p>\nThis can be turned into a <a href=\"http:\/\/www.perl.com\/\">Perl<\/a> subroutine using the following code.\n<\/p>\n<div class=\"code\"><code>sub grayscale {<br \/>\nmy ($r, $g, $b) = @_;<br \/>\nmy $s = 0.15 * $r + 0.55 * $g + 0.30 * $b;<br \/>\nreturn int($s);<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\nHere we pass in the <acronym title=\"Red Green Blue\">RGB<\/acronym> values of the colour we want to turn into gray. We apply the algorithm and return the integer value of gray.\n<\/p>\n<p>\nThe value we get for gray is used to replace each of the values for red, green and blue.\n<\/p>\n<p>\nWe can test this subroutine out with the help of the Perl GD module (available for free on <a href=\"http:\/\/www.cpan.org\/\" title=\"comprehensive perl archive network\">CPAN<\/a>).\n<\/p>\n<div class=\"code\"><code>#!\/usr\/bin\/perl -w<br \/>\nuse GD;<br \/>\n## grayscale subroutine<br \/>\nsub grayscale {<br \/>\nmy ($r, $g,$b) = @_;<br \/>\nmy $s = 0.15 * $r + 0.55 * $g + 0.30 * $b;<br \/>\nreturn int($s);<br \/>\n}<br \/>\n## create a new GD object with the data passed via STDIN<br \/>\nmy $image = new GD::Image(*STDIN);<br \/>\n## iterate over the number of colours in the colour table<br \/>\nfor (my $i = 0; $i &lt; $image-&gt;colorsTotal(); $i++) {<br \/>\n## get the RGB values for the colour at index $i<br \/>\nmy ($r, $g, $b) = $image-&gt;rgb($i);<br \/>\n## convert the RGB to grayscale<br \/>\nmy $gray = grayscale($r,$g,$b);<br \/>\n## remove the original colour from the colour table<br \/>\n$image-&gt;colorDeallocate($i);<br \/>\n## add in the new gray<br \/>\n$image-&gt;colorAllocate($gray,$gray,$gray);<br \/>\n}<br \/>\n## make sure we output binary<br \/>\nbinmode STDOUT;<br \/>\n## pass the image as a raw GIF to STDOUT<br \/>\nprint $image-&gt;gif;<br \/>\n<\/code><\/div>\n<p>\nThis code takes an image piped in from STDIN and outputs a grayscale GIF version of the image to STDOUT.\n<\/p>\n<p>\nIf the code was called <code>convert.pl<\/code> it would be called as <code>.\/convert.pl &lt;test.gif &gt>test_result.gif<\/code>.\n<\/p>\n<p>\nHere&#8217;s a conversion I did earlier of a GIF image of Kitt, Bev and Justin at the Emap Performance Awards 2004 using the above Perl code.\n<\/p>\n<p>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/robblog\/images\/grayscale_before.gif\" width=\"300\" height=\"200\" alt=\"Kitt, Bev and Justin in colour\" class=\"blogimage\" \/>\n<\/p>\n<p>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/robblog\/images\/grayscale_after.gif\" width=\"300\" height=\"200\" alt=\"Kitt, Bev and Justin in grayscale\" class=\"blogimage\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One thing that caught my interest today was how to convert a colour image into grayscale. It turns out the basic algorithm is very simple. Basically it&#8217;s just&#8230; grey = 0.15 * red + 0.55 * green + 0.30 * blue; This can be turned into a Perl subroutine using the following code. sub grayscale &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/grayscaling_images_with_perl-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Grayscaling Images With Perl&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[17,47],"class_list":["post-626","post","type-post","status-publish","format-standard","hentry","category-dev","tag-dev","tag-perl"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Grayscaling Images With Perl - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/grayscaling_images_with_perl-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Grayscaling Images With Perl - Robert Price\" \/>\n<meta property=\"og:description\" content=\"One thing that caught my interest today was how to convert a colour image into grayscale. It turns out the basic algorithm is very simple. Basically it&#8217;s just&#8230; grey = 0.15 * red + 0.55 * green + 0.30 * blue; This can be turned into a Perl subroutine using the following code. sub grayscale &hellip; Continue reading &quot;Grayscaling Images With Perl&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/grayscaling_images_with_perl-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2005-03-24T23:25:18+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/grayscaling_images_with_perl-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/grayscaling_images_with_perl-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Grayscaling Images With Perl\",\"datePublished\":\"2005-03-24T23:25:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/grayscaling_images_with_perl-shtml\\\/\"},\"wordCount\":166,\"keywords\":[\"Dev\",\"Perl\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/grayscaling_images_with_perl-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/grayscaling_images_with_perl-shtml\\\/\",\"name\":\"Grayscaling Images With Perl - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2005-03-24T23:25:18+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/grayscaling_images_with_perl-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/grayscaling_images_with_perl-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/grayscaling_images_with_perl-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Grayscaling Images With Perl\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Grayscaling Images With Perl - Robert Price","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":"https:\/\/www.robertprice.co.uk\/robblog\/grayscaling_images_with_perl-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Grayscaling Images With Perl - Robert Price","og_description":"One thing that caught my interest today was how to convert a colour image into grayscale. It turns out the basic algorithm is very simple. Basically it&#8217;s just&#8230; grey = 0.15 * red + 0.55 * green + 0.30 * blue; This can be turned into a Perl subroutine using the following code. sub grayscale &hellip; Continue reading \"Grayscaling Images With Perl\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/grayscaling_images_with_perl-shtml\/","og_site_name":"Robert Price","article_published_time":"2005-03-24T23:25:18+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/grayscaling_images_with_perl-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/grayscaling_images_with_perl-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Grayscaling Images With Perl","datePublished":"2005-03-24T23:25:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/grayscaling_images_with_perl-shtml\/"},"wordCount":166,"keywords":["Dev","Perl"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/grayscaling_images_with_perl-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/grayscaling_images_with_perl-shtml\/","name":"Grayscaling Images With Perl - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2005-03-24T23:25:18+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/grayscaling_images_with_perl-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/grayscaling_images_with_perl-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/grayscaling_images_with_perl-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Grayscaling Images With Perl"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=626"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/626\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}