卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35378本站已运行395

Golang 为 jpeg 图像生成一致的哈希值,而无需写入磁盘

golang 为 jpeg 图像生成一致的哈希值,而无需写入磁盘

在开发过程中,我们经常需要比较图像文件的相似度,以便进行图像识别、去重等操作。而生成图像的哈希值是一种常见的方法。通常,我们需要将图像写入磁盘,再读取出来进行哈希计算。然而,使用Golang编程语言,我们可以轻松地实现在生成jpeg图像的同时,直接计算出一致的哈希值,而无需写入磁盘。这为我们节省了时间和磁盘空间,提高了效率。本文将详细介绍如何在Golang中实现这一功能。

问题内容

golang 成像新手

我正在尝试为 jpeg 图像生成一致的哈希值。 当我以 JPEG 格式写入磁盘(这是预期的)后重新加载图像时,加载图像并在原始字节上生成哈希值会产生不同的哈希值。一旦我将 RBGA 作为 JPEG 写入磁盘,像素就会被修改,这会破坏我之前计算的哈希值。

仅对文件 hash("abc.jpeg") 进行哈希处理就意味着我必须写入磁盘;读回;生成哈希值等..

  • 在读取/写入时是否可以使用任何设置来控制输出 jpeg 像素的行为
  • 我是否应该使用 *image.RGBA?输入图像是 *image.YCbCr?
// Open the input image file
inputFile, _ := os.Open("a.jpg")
defer inputFile.Close()

// Decode the input image
inputImage, _, _ := image.Decode(inputFile)

// Get the dimensions of the input image
width := inputImage.Bounds().Dx()
height := inputImage.Bounds().Dy()
subWidth := width / 4
subHeight := height / 4

// Create a new image
subImg := image.NewRGBA(image.Rect(0, 0, subWidth, subHeight))
draw.Draw(subImg, subImg.Bounds(), inputImage, image.Point{0, 0}, draw.Src)

// id want the hashes to be the same for read / write but they will always differ
hash1 := sha256.Sum256(imageToBytes(subImg))
fmt.Printf("<---OUT [%s] %xn", filename, hash1)
jpg, _ := os.Create("mytest.jpg")
_ = jpeg.Encode(jpg, subImg, nil)
jpg.Close()

// upon reading it back in the pixels are ever so slightly diff
f, _ := os.Open("mytest.jpg")
img, _, _ := image.Decode(f)
jpg_input := image.NewRGBA(img.Bounds())
draw.Draw(jpg_input, img.Bounds(), img, image.Point{0, 0}, draw.Src)
hash2 := sha256.Sum256(imageToBytes(jpg_input))
fmt.Printf("--->IN  [%s] %xn", filename, hash2)

            // real world use case is..
            // generate subtile of large image plus hash
            // if hash in a dbase
            //    pixel walk to see if hash collision occurred
            //    if pixels are different
            //       deal with it...
            ///   else
            //      object.filename = dbaseb.filename
            // else
            //     add filename to dbase with hash as the lookup
            //     write to jpeg to disk

解决方法

您可以使用哈希作为编写器的目标,并使用 io.MultiWriter 在写入文件时计算哈希:

hash:=sha256.New()
jpeg.Encode(io.MultiWriter(file,hash),img,nil)
hashValue:=hash.Sum(nil)
卓越飞翔博客
上一篇: 关闭客户端连接后如何从 tcp 套接字完全读取缓冲内容?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏