首页 > 其他分享 >Go - Creating Images

Go - Creating Images

时间:2023-10-10 11:14:35浏览次数:23  
标签:slice Creating image random bytes Images Go 100 rect

Problem: You want to create an image from scratch.


Solution: Create one of the Image implementation structs (e.g., NRGBA ) and populate it with the appropriate data.

 

As you remember from earlier, NRGBA has three attributes:
Pix
• A slice of bytes that contains the pixels in the image (it’s just a slice of color.Color )
Stride
• The distance between the two vertically adjacent pixels
Rect
• The dimensions of the image

func   main ()   { 
      rect   :=   image . Rect ( 0 ,   0 ,   100 ,   100 ) 
      img   :=   createRandomImage ( rect ) 
      save ( "random.png" ,   img ) 
} 

func   createRandomImage ( rect   image . Rectangle )   ( created   * image . NRGBA )   { 
      pix   :=   make ([] uint8 ,   rect . Dx () * rect . Dy () * 4 ) 
      rand . Read ( pix ) 
      created   =   & image . NRGBA {
          Pix :      pix , 
          Stride :   rect . Dx ()   *   4 , 
          Rect :     rect , 
      } 
      return 
}

Say you want to create an image that is 100 × 100 pixels. First, you need to create a Rect with the correct dimensions. Next, the Pix should be a slice of bytes of size 100 × 100 × 4 = 40,000 because each pixel is represented by 4 bytes (R, G, B, and A). Lastly, the Stride is the distance between two vertical pixels, which is the width of the image, multiplied by 4, which is 100 × 4 = 400.

The example code created a random image with each pixel a random color by populating the Pix slice of bytes with random bytes using rand.Read . You can fill it up with anything else, of course.

标签:slice,Creating,image,random,bytes,Images,Go,100,rect
From: https://www.cnblogs.com/zhangzhihui/p/17754134.html

相关文章

  • Go通道机制与应用详解
    本文深入探讨了Go语言中通道(Channel)的各个方面,从基础概念到高级应用。文章详细解析了通道的类型、操作方法以及垃圾回收机制,更进一步通过具体代码示例展示了通道在数据流处理、任务调度和状态监控等多个实际应用场景中的作用。本文旨在为读者提供一个全面而深入的理解,以更有效地......
  • go gomail.v2发送邮件报错unencrypted connection
    实现Auth接口typeauthstruct{hoststringusernamestringpasswordstring}func(a*auth)Start(server*smtp.ServerInfo)(protostring,toServer[]byte,errerror){if!server.TLS{advertised:=falsefor_,mechanis......
  • Go - Loading an Image from a File
    Problem: Youwanttoloadanimagefromanimagefile.Solution: Useimage.Decodetodecodedatafromanimagefileintoanimplementationofimage.Image. Ifyouwanttoworkwithanimagefromafile,youhavetoopenupthefileandthendecodeits......
  • MongoDB可视化管理工具-MongoDB Compass【转】
    一、引言在使用MongoDB过程中,如果单单依靠命令行操作MongoDB数据库,效率不高而且查看不方便。因此MongoDB官网提供的一个可视化管理工具,叫MongoDBCompass,它集创建数据库、管理集合和文档、运行临时查询、评估和优化查询、性能图表、构建地理查询等功能为一体,很方便。二、......
  • Go - Finding the Shortest Path on a Graph
    Problem: Youwanttofindtheshortestpathbetweentwonodesonaweightedgraph.Solution: UseDijkstra’salgorithmtofindtheshortestpathbetweentwonodes.Dijkstra’salgorithmalsousesapriorityqueue,whichcanbeimplementedusingaminheap.......
  • MongoDB常用查询
    1.数据库数据说明#集合:school#文档:stu#结合字段:id,学号、姓名、电话、性别、年龄、学历、备注#初始化20条数据useschoolfor(varnum=1;num<=20;num++){db.stu.insert({id:num,no:"SN"+num,name:"name"+num,tel:"111......
  • MongoDB基础知识
    1.简介MongoDB官方文档菜鸟教程1、NoSQL(NotOnlySQL),不仅仅是SQL,主要是指非关系型数据库,是对不同与传统的关系型数据库的数据管理系统的统称2、NoSQL用于超大规模数据的存储,这些类型的数据存储吧需要固定的模式,无需多余的操作就可以横向扩展1.2NoSQL和RDBMS的区分......
  • Rust cargo常用命令
    目录设置国内镜像创建新项目构建项目运行项目检查项目,但不构建可执行文件运行项目的测试发布项目更新依赖查看项目依赖关系树创建新的库项目文档生成设置国内镜像cd~/.cargo#创建config文件vimconfig#添加如下镜像源[source.crates-io]registry="https://github.com/......
  • Django 数据库--values_list 指定字段取值及 distinct 去重处理
    通过QuerySet会返回model的所有字段,通过obj.field_name即可获取对应字段的数据values():获取某一个或者某几个字段的数据指定字段使用values()指定参数可以仅仅返回相应字段的字典列表,如:name_dict_list=Project.objects.values('name')则name_dict_list= <Q......
  • Google Guava 库用法整理
    参考:(2,3,4)http://blog.publicobject.com更多用法参考http://ajoo.iteye.com/category/119082以前这么用:Java代码Map<String,Map<Long,List<String>>>map=newHashMap<String,Map<Long,List<String>>>();现在这么用(JDK7将实现该功能......