golang 成像新手
我正在try 为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] %x\n", 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] %x\n", 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