首页 > 其他分享 >Go - Loading an Image from a File

Go - Loading an Image from a File

时间:2023-10-10 10:13:27浏览次数:34  
标签:Loading img err NRGBA Image file Go image

Problem: You want to load an image from an image file.


Solution: Use image.Decode to decode data from an image file into an implementation of image.Image .

 

If you want to work with an image from a file, you have to open up the file and then decode its content. To do this, you should already know what kind of file you are dealing with and register the type accordingly:

func   load ( filePath   string )   (*image.NRGBA, error)   { 
      imgFile ,   err   :=   os . Open ( filePath ) 
      if   err   !=   nil   { 
          log . Println ( "Cannot  read  file:" ,   err ) 
      } 
      defer   imgFile . Close () 

      img ,   _ ,   err   :=   image . Decode ( imgFile ) 
      if   err   !=   nil   { 
          log . Println ( "Cannot  decode  file:" ,   err ) 
      } 
      rimg ,   ok   :=   img .( * image . NRGBA ) 
      if   ok   { 
          return   rimg ,   nil 
      } 
      return   nil ,   errors . New ( "cannot  type  assert  image" ) 
}

First, you need to open a file. Then using image.Decode , you decode it into the img variable. The Decode method returns three values: the first is what you want to get, the image.Image value; the second is a string signifying the format of the image; and the third is the usual error value.

Take a closer look at the img value. Since you get an Image value, why do you want to type assert to image.NRGBA before you return it? This is because Image is an interface, and NRGBA is the actual implementation:

type   NRGBA   struct   { 
      Pix   [] uint8 
      Stride   int 
      Rect   Rectangle 
}

In other words, you can manipulate the NRGBA but you can’t manipulate an Image . Of course, you can still call methods on Image but you can’t directly manipulate it unless you get a hold of the underlying implementation.

 

标签:Loading,img,err,NRGBA,Image,file,Go,image
From: https://www.cnblogs.com/zhangzhihui/p/17753892.html

相关文章

  • MongoDB可视化管理工具-MongoDB Compass【转】
    一、引言在使用MongoDB过程中,如果单单依靠命令行操作MongoDB数据库,效率不高而且查看不方便。因此MongoDB官网提供的一个可视化管理工具,叫MongoDBCompass,它集创建数据库、管理集合和文档、运行临时查询、评估和优化查询、性能图表、构建地理查询等功能为一体,很方便。二、......
  • Go - Finding the Shortest Path on a Graph
    Problem: Youwanttofindtheshortestpathbetweentwonodesonaweightedgraph.Solution: UseDijkstra’salgorithmtofindtheshortestpathbetweentwonodes.Dijkstra’salgorithmalsousesapriorityqueue,whichcanbeimplementedusingaminheap.......
  • 启动vue项目报错——ERROR Error loading vue.config.js: ERROR TypeError: defineCon
    问题描述在我引入echarts模块之前是ok的,引入之后就启动失败了;问题解决一般情况下,都是该项目的版本与本机cmd里面的版本不对应导致的;只需要使用这个命令npmupgrade,更新版本,一直yes下去,就能够解决这个问题啦!......
  • 论文阅读(一)—— Adding Conditional Control to Text-to-Image Diffusion Models
    ......
  • 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将实现该功能......
  • Go字符串实战操作大全!
    在本篇文章中,我们深入探讨了Go语言中字符串的魅力和深度。从基础定义、操作、字符编码到复杂的类型转换,每个环节都带有实例和代码示例来深化理解。通过这些深入的解析,读者不仅能够掌握字符串在Go中的核心概念,还能洞察Go设计哲学背后的思考。关注公众号【TechLeadCloud】,分享互......