You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为HALCON .hdict(二进制字典)文件启用Git Diff?

让Git Diff对含图像的.hdict文件显示可读差异

默认情况下Git会将.hdict识别为二进制文件,仅提示Binary files a/Labels.hdict and b/Labels.hdict differ,可以通过配置Git自定义diff驱动实现可读差异展示,以下是两种可行方案:

方案一:转XML格式对比(快速实现)

.hdict本质是二进制属性列表(plist),可通过系统自带的plutil工具转换为XML文本格式,再进行diff对比:

  1. 配置Git自定义diff驱动
    在全局.gitconfig(~/.gitconfig)或项目本地.git/config中添加:

    [diff "hdict"]
        command = bash -c 'plutil -convert xml1 -o - "$1" > /tmp/hdict_old.xml; plutil -convert xml1 -o - "$2" > /tmp/hdict_new.xml; diff -u /tmp/hdict_old.xml /tmp/hdict_new.xml' -
    
  2. 关联文件类型
    在项目根目录的.gitattributes文件中添加(无则新建):

    *.hdict diff=hdict
    

配置完成后,终端执行git diff会显示转换后的XML文本差异,图像数据会以Base64编码形式展示;Git Extensions等GUI工具会自动遵循该配置,展示可读差异。

方案二:自定义脚本解析(更友好的图像差异展示)

如果希望避免冗长的Base64编码,可编写脚本解析.hdict,提取文本内容与图像元数据(尺寸、格式等)后再对比:

  1. 编写解析脚本
    新建hdiff.py脚本(路径自行调整):

    import plistlib
    import sys
    import os
    import difflib
    
    def process_hdict(file_path):
        with open(file_path, 'rb') as f:
            plist = plistlib.load(f)
        output = []
        for key, value in plist.items():
            if isinstance(value, bytes):
                try:
                    tmp_img = f"/tmp/hdict_tmp_{os.path.basename(file_path)}_{key}"
                    with open(tmp_img, 'wb') as img_f:
                        img_f.write(value)
                    # 用sips工具提取图像元数据(macOS自带)
                    img_info = os.popen(f"sips -g format -g pixelWidth -g pixelHeight {tmp_img}").read().strip()
                    output.append(f"{key}: [图像数据]")
                    output.append(img_info)
                    os.unlink(tmp_img)
                except Exception:
                    output.append(f"{key}: [二进制数据,长度: {len(value)} 字节]")
            else:
                output.append(f"{key}: {repr(value)}")
        return '\n'.join(output)
    
    if __name__ == '__main__':
        if len(sys.argv) != 3:
            sys.exit(1)
        old_content = process_hdict(sys.argv[1])
        new_content = process_hdict(sys.argv[2])
        diff_result = difflib.unified_diff(old_content.splitlines(), new_content.splitlines())
        print('\n'.join(diff_result))
    
  2. 配置Git调用脚本
    修改.gitconfig或项目.git/config中的diff驱动:

    [diff "hdict"]
        command = python3 /path/to/hdiff.py
    
  3. 关联文件类型
    同方案一,在.gitattributes中添加*.hdict diff=hdict。

此方案会将图像数据替换为可读的元信息,文本内容直接展示,终端和GUI工具都能显示清晰的差异对比。

内容的提问来源于stack exchange,提问作者Vladimir Perković

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.01 13:12:42