How do you create a new image in go?

agolangf · · 676 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I need to copy an existing image, tint it red, then output to a writer. I can copy the existing image to a new one, but it seems impossible to modify the color. I tried iterating through the pixels, copying them, and adding them to a new image, but i still can&#39;t modify the color. Is there a way to say image.At(x,y) = RGBA{r,g,b,a}? I am also having problems converting from image.Image.RGBA which is uint32 to color.RGBA which is uint8. What is this madness? Help!</p> <hr/>**评论:**<br/><br/>ctcherry: <pre><p>To write a single pixel use <code>Set(x, y int, c color.Color)</code> I found it in the docs here <a href="https://golang.org/pkg/image/" rel="nofollow">https://golang.org/pkg/image/</a></p></pre>madman2233: <pre><p>Thanks. Sort of got it working with</p> <pre><code>c := color.RGBA{uint8(r &gt;&gt; 8), uint8(g &gt;&gt; 8), uint8(b &gt;&gt; 8), uint8(a &gt;&gt; 8)} newimg.Set(x, y, c) </code></pre></pre>hobbified: <pre><p>It looks like your r, g, b, a are 16 bits each? Maybe you want to use <code>color.RGBA64{r, g, b, a}</code> then instead of chopping 8 bits off of each. Besides, it&#39;s a lot shorter.</p> <p>As for your other question, it&#39;s a bit unclear what you&#39;re really asking, but I think your problem starts with calling <code>RGBA()</code> at all. <code>image.At</code> gives you a <code>color.Color</code> object, which is what it sounds like. You can use <code>color.RGBAModel.Convert</code> to ensure that it is in fact a <code>color.RGBA</code>. Then you can fiddle its r, g, b, a fields directly and pass it back to <code>Set</code> on some other image.</p></pre>madman2233: <pre><p>Whenever i try to use Set, i get an invalid memory address runtime error. Edit:</p> <pre><code>c := img.At(x, y) c = color.RGBA64Model.Convert(c) r, g, b, a := c.RGBA() </code></pre> <p>still makes me use</p> <pre><code>cr = color.RGBA64{uint16(r &gt;&gt; 16), uint16(g &gt;&gt; 16), uint16(b &gt;&gt; 16), uint16(a &gt;&gt; 16)} </code></pre></pre>hobbified: <pre><p>The point of my post was <em>not</em> to use <code>c.RGBA()</code>. But don&#39;t worry about it.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

676 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传