首页 > 其他分享 >Go - Resizing an Image

Go - Resizing an Image

时间:2023-10-10 16:01:43浏览次数:29  
标签:scale positions Image resized grid new Go Resizing image

Problem: You want to resize an image, making it larger or smaller.


Solution: Convert an image to a grid of pixels as the source and create a new image with the resized dimensions. Use the nearest neighbor interpolation algorithm to figure out the color of each pixel in the new image. Convert the grid of pixels back into a resized image.

 

Resizing a raster image means creating an image with a higher or lower number of pixels. Many algorithms are used for resizing images, including nearest neighbor, bilinear, bicubic, Lanczos resampling, and box sampling. Among these algorithms, the nearest neighbor is probably the simplest. However, the quality is usually not the best, and it can also introduce jaggedness in the image.
The nearest neighbor interpolation algorithm works like this:
1 Assume you are given the original image and the scale of the resize. For example, if you want to double the size of the image, the scale is 2.
2 Create a new image with the new size.
3 Take each pixel in the new image, and divide the X and Y positions by the scale to get X ' and Y ' positions. These might not be whole numbers.
4 Find the floor of X ' and Y ' . These are the corresponding X and Y positions mapped on the original image.
5 Use the X ' and Y ' positions to get the pixel in the original image and put it into the new image at the X and Y positions.

Here’s the code to implement the algorithm:

func   resize ( grid   [][] color . Color ,   scale   float64 )   ( resized   [][] color . Color )   { 
      xlen ,   ylen   :=   int ( float64 ( len ( grid )) * scale ),   int ( float64 ( len ( grid [ 0 ])) * scale ) 
      resized   =   make ([][] color . Color ,   xlen ) 
      for   i   :=   0 ;   i   <   len ( resized );   i ++   { 
          resized [ i ]   =   make ([] color . Color ,   ylen ) 
      } 
      for   x   :=   0 ;   x   <   xlen ;   x ++   { 
          for   y   :=   0 ;   y   <   ylen ;   y ++   { 
              xp   :=   int ( math . Floor ( float64 ( x )   /   scale )) 
              yp   :=   int ( math . Floor ( float64 ( y )   /   scale )) 
              resized [ x ][ y ]   =   grid [ xp ][ yp ] 
          } 
      } 
      return 
}

 

标签:scale,positions,Image,resized,grid,new,Go,Resizing,image
From: https://www.cnblogs.com/zhangzhihui/p/17754910.html

相关文章

  • Go - Converting an Image to Grayscale
    Problem: Youwanttoconverttheimagetograyscale.Solution: Convertanimagetoagridofpixels.Takeeachpixelinthegridandconvertittoagraypixelaccordingtotherelativeluminanceformula.Convertthegridofpixelsbackintoanimageto......
  • Golang chan 的实现原理
    Golangchan的实现原理Go语言中的chan(通道)是一种用于在不同的goroutines之间进行通信和同步的重要机制。chan的实现原理涉及到Go语言的运行时系统和底层的数据结构。以下是chan的主要实现原理:底层数据结构:chan的底层数据结构是一个用于存储数据的环形队列(circularqueue)或链......
  • django 设置外键的时候,related_name的值大写还是小写,规则怎样
    django设置外键的时候,related_name的值大写还是小写,规则怎样在Django中,related_name参数用于定义反向关系的名称,即通过外键字段反向查询关联模型的对象。related_name的值是一个字符串,可以是大写也可以是小写,但通常建议使用小写字母,因为它们更符合Python的命名约定(PEP8)。具体......
  • 使用mongo uri
    示例uri='mongodb://{username}:{password}@{host}:{port}/{dbname}'特殊情况username和password含有"@"和":"时,将"@"和":"进行url编码.​@==>%40:==>%3a ......
  • golang map/sync.map 实现
    mapGo中的map是一种高效的散列表(hashtable)实现,它的底层实现细节包括以下重要方面:哈希表(HashTable):map的底层数据结构是一个哈希表。哈希表是一个数组,每个元素都是一个哈希桶,用于存储键值对。哈希函数(HashFunction):Go使用哈希函数将键映射到哈希桶。这个哈希函数是内......
  • Django实战项目-学习任务系统-用户注册
    接着上期代码框架,开发第2个功能,用户注册,在原有用户模型基础上,增加一个学生用户属性表,用来关联学生用户的各种属性值,这个属性表是参考网络小说里系统属性值设计的,方便直观了解用户的能力高低,等级以及积分值等信息。 第一步:编写第二个功能-用户注册1,编辑模型文件:./mysite/study......
  • SQLServer报错: Got minus one from a read call
    用JDBC连接SqlServer数据库时,报这个错误。网上很多都说是数据库的连接已经满了。但我实际查询的时候,数据库连接数并没有满。 后来发现原因了,是代码存在疏忽。我把驱动类写成了Oracle的驱动类。所以这个错误实际上有一种可能是因为使用了错误的驱动类导致的。 ......
  • MongoDB Node.js Driver and MongoClient All In One
    MongoDBNode.jsDriverandMongoClientAllInOneThenextgenerationNode.jsdriverforMongoDB$npmimongodb#OR$npmi-Smongodb#OR$npminstallmongodb--savehttps://mongodb.github.io/node-mongodb-native/index.htmlhttps://www.mongodb.com......
  • 学习笔记426—keras中to_categorical函数解析
    keras中to_categorical函数解析1.to_categorical的功能简单来说,to_categorical就是将类别向量转换为二进制(只有0和1)的矩阵类型表示。其表现为将原有的类别向量转换为独热编码的形式。先上代码看一下效果:fromkeras.utils.np_utilsimport*#类别向量定义b=[0,1,2,3,4,5,6,7......
  • Django RestFramework、Celery及Channels
    DjangoRESTFramework什么是RESTfulAPIRESTfulAPI是一种基于HTTP协议的接口设计风格,它使用统一的接口和资源的概念来定义和操作网络应用程序的功能和数据。RESTfulAPI使用HTTP动词(GET、POST、PUT、DELETE等)来表示操作类型,并使用URL来标识资源。传统风格的HTTP接口常用授......