{"id":2363,"date":"2015-03-13T02:23:29","date_gmt":"2015-03-13T02:23:29","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=2363"},"modified":"2015-03-13T02:23:29","modified_gmt":"2015-03-13T02:23:29","slug":"creating-a-png-image-in-golang","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2015\/03\/creating-a-png-image-in-golang\/","title":{"rendered":"Creating a PNG image in golang"},"content":{"rendered":"<p>The following very simple example creates a PNG image file in golang. It first creates a Image structure and then populates this with random data. <\/p>\n<p>A file is then creating using os.Create, this is used as a io.Writer to which golang&#8217;s PNG encoder in image\/png writes the encoded PNG data via the png.Encode function.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\npackage main\r\n\r\nimport (\r\n    &quot;image&quot;\r\n    &quot;image\/png&quot;\r\n    &quot;image\/color&quot;\r\n    &quot;os&quot;\r\n    &quot;math\/rand&quot;\r\n)\r\n\r\n\r\nfunc main() {\r\n\r\n  myimage := image.NewRGBA(image.Rectangle{image.Point{0,0},image.Point{100,100}})\r\n\r\n  \/\/ This loop just fills the image with random data\r\n  for x := 0; x &lt; 100; x++ {\r\n    for y := 0; y &lt; 100; y++ {\r\n      c := color.RGBA{uint8(rand.Intn(255)),uint8(rand.Intn(255)),uint8(rand.Intn(255)),255}\r\n      myimage.Set(x,y,c)\r\n    }\r\n  }\r\n\r\n  myfile, _ := os.Create(&quot;test.png&quot;)\r\n\r\n  png.Encode(myfile, myimage)\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The following very simple example creates a PNG image file in golang. It first creates a Image structure and then populates this with random data. A file is then creating using os.Create, this is used as a io.Writer to which golang&#8217;s PNG encoder in image\/png writes the encoded PNG data via the png.Encode function. package [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-2363","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-C7","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=2363"}],"version-history":[{"count":1,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2363\/revisions"}],"predecessor-version":[{"id":2364,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2363\/revisions\/2364"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=2363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=2363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=2363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}