如何对 cairo 创建的图像和从文件加载的图像进行逐像素比较
我在 nim 中有一些代码,使用 Cairo 创建图片(https://github.com/nim-lang /开罗)。我想使用 diffimg 将该图片与另一张图片进行比较(https://github.com/SolitudeSF/diffimg,https://github.com/SolitudeSF/imageman)。
但内存图像类型似乎没有一个标准。有没有什么方法可以做到这一点,而不需要先将图像保存到文件中?
I have some code in nim that creates a picture using Cairo (https://github.com/nim-lang/cairo). I would like to compare that picture to another using diffimg (https://github.com/SolitudeSF/diffimg, https://github.com/SolitudeSF/imageman).
But there does not seem to be a standard in memory image type. Is there any way to do this that does not involve saving the image to a file first?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
令人惊讶的是,最简单的方法可能是自己实现 diffimg 算法。查看
diffimg
的源代码显示比较算法是 大约20行代码:加载图像的实际麻烦留给了
imageman
,但总而言之似乎是减去像素分量两个图像之间的值并创建某种平均值/比率。由于 cairo 库似乎为图像生成了自己的、可能不兼容的内存布局,因此您很可能希望忽略imageman
并将要与自己进行比较的图像加载到 cairo 内存缓冲区中,然后复制比较算法迭代每个 caro 图像的像素。或者可以将 cairo 缓冲区转换为 imageman 缓冲区,然后让 diffimg 实现发挥其魔力。Probably the most easy way is surprisingly to implement yourself the
diffimg
algorithm. Looking at the source ofdiffimg
shows the comparison algoritm is about 20 lines of code:The actual trouble of loading the image is left to
imageman
, but all in all it seems to substract pixel component values between the two images and create some kind of average/ratio. Since the cairo library seems to generate its own, possibly incompatible, memory layout for images, most likely you want to ignoreimageman
and load the image you want to compare to yourself into a cairo memory buffer, then replicate the diffing algorithm iterating through the pixels of each caro image. Or maybe convert the cairo buffer to animageman
buffer and let thediffimg
implementation do its magic.