如何以可重現的方式(沒有時間戳記)使用 ImageMagick 轉換?

如何以可重現的方式(沒有時間戳記)使用 ImageMagick 轉換?

我正在嘗試在腳本中使用 ImageMagick 來轉換大量檔案並調整其大小以進行版本控制。我需要每次都同時轉換文件,這樣 git 就不會提交剛剛更新時間戳記的文件。不幸的是,ImageMagick 堅持為每個圖像添加建立和修改時間戳,這使得 git 再次重新提交每個檔案。

我已經搜索了很多關於這個問題的信息,並且嘗試了以下標誌:

-define png:exclude-chunks=date
+set date:create +set date:modify
-strip

這些都沒有產生可重現的過程:

-定義 png:排除區塊=日期

stephen@Saturn ~/test (git)-[master] % convert input.png -define png:exclude-chunks=date -resize 100x100 1.png
stephen@Saturn ~/test (git)-[master] % convert input.png -define png:exclude-chunks=date -resize 100x100 2.png
stephen@Saturn ~/test (git)-[master] % diff 1.png 2.png
Binary files 1.png and 2.png differ
stephen@Saturn ~/test (git)-[master] % cmp -l 1.png 2.png
  125  41  42
  126  67   0
  127 322 101
  128 321 101
  129  35 353
  130  64 370

+設定日期:建立 +設定日期:修改

stephen@Saturn ~/test (git)-[master] % convert input.png +set date:create +set date:modify -resize 100x100 1.png
stephen@Saturn ~/test (git)-[master] % convert input.png +set date:create +set date:modify -resize 100x100 2.png
stephen@Saturn ~/test (git)-[master] % diff 1.png 2.png
Binary files 1.png and 2.png differ
stephen@Saturn ~/test (git)-[master] % cmp -l 1.png 2.png
  125  51  52
  126  71   0
  127 375 211
  128 260 230
  129 272 141
  130  73 360

-條

stephen@Saturn ~/test (git)-[master] % convert input.png -strip -resize 100x100 1.png
stephen@Saturn ~/test (git)-[master] % convert input.png -strip -resize 100x100 2.png
stephen@Saturn ~/test (git)-[master] % diff 1.png 2.png
Binary files 1.png and 2.png differ
stephen@Saturn ~/test (git)-[master] % cmp -l 1.png 2.png
  110  41  45
  111 241 246
  112 235 360
  113 264 160
  114 252 263

如何使用 ImageMagick 完成可重複的轉換?

答案1

您需要將 ImageMagick 更新到版本 6.9.1-3 或更高版本,然後您問題中的所有命令都將建立可重現的映像。

我在中找到了以下內容變更日誌:

2015-04-20 6.9.1-3 克里斯蒂 <quetzlzacatenango@image...>
  * 支援 -define compose:clamp=false 選項(參考
    https://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=26946)。
  * 不要在 SeekBlob() 中擴展任何用戶提供的圖像緩衝區(錯誤報告
    來自a.chernij@corp...)。
  * 改進的可重複建造(參考
    https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=783933)。
  * 畫一個寬高為1的矩形(參考
    https://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=24874)。

在發現 ArchLinux 上的 ImageMagick 工作正常(與 Ubuntu 16.04 上的 ImageMagick 不同)後,我找到了變更日誌條目。

ArchLinux(良好的、可複製的圖像):

$ convert --version
Version: ImageMagick 6.9.8-8 Q16 x86_64 2017-05-30 http://www.imagemagick.org

Ubuntu 16.04(不好,每次都有不同的映像):

$ convert --version
Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-05-26 http://www.imagemagick.org

答案2

我發現-define png:exclude-chunks=date,time是必要的。僅排除該date區塊是不夠的。

單一選項也足夠了;我不需要-strip使用或匯出特定的時間戳SOURCE_DATE_EPOCH(這已在其他地方建議過)。

最後,查看差異diff <(xxd 1.png) <(xxd 2.png)是我的首選方式,同時查看二進位和 ascii(有助於查看區塊 ID 和時間戳出現的位置)。

作為參考,僅排除date

8,9c8,9
< 00000070: 0000 0774 494d 4507 e503 030a 1d0f bd0c  ...tIME.........
< 00000080: 01f4 0000 8000 4944 4154 78da ecdd 7578  ......IDATx...ux
---
> 00000070: 0000 0774 494d 4507 e503 030a 1c19 50c3  ...tIME.......P.
> 00000080: 85e4 0000 8000 4944 4154 78da ecdd 7578  ......IDATx...ux

答案3

我已經放棄了讓 ImageMagick 正常工作,轉而使用 GraphicsMagick,它似乎具有 ImageMagick 的所有功能,但減去了這個時間戳錯誤:

stephen@Saturn ~/test (git)-[master] % gm convert -resize 100x100 input.png 1.png
stephen@Saturn ~/test (git)-[master] % gm convert -resize 100x100 input.png 2.png
stephen@Saturn ~/test (git)-[master] % diff 1.png 2.png
stephen@Saturn ~/test (git)-[master] % cmp -l 1.png 2.png

識別顯示 2 個不同的時間戳,但它是從文件屬性而不是嵌入的元資料獲取的,並且 diff / cmp 顯示文件是相同的。

相關內容