2013-07-19 31 views
1

我使用ImageMagick(使用Python中的魔杖)來轉換圖像並從中獲取縮略圖。但是,我注意到我需要提前驗證文件是否爲圖像。我應該使用Identify來做到這一點嗎?ImageMagick驗證圖像完整性

所以我會假設檢查文件的完整性需要將整個文件讀入內存。是更好地嘗試和轉換文件,如果有錯誤,那麼我們知道該文件不好。

回答

5

好像你回答了你自己的問題

$ ls -l *.png 
-rw-r--r-- 1 jsp jsp 526254 Jul 20 12:10 image.png 
-rw-r--r-- 1 jsp jsp 10000 Jul 20 12:12 image_with_error.png 
$ identify image.png &> /dev/null; echo $? 
0 
$ identify image_with_error.png &> /dev/null; echo $? 
0 
$ convert image.png /dev/null &> /dev/null ; echo $? 
0 
$ convert image_with_error.png /dev/null &> /dev/null ; echo $? 
1 
+1

這是另一種使用身份識別但不進行轉換的解決方案: 'identify -verbose * .png 2>&1 | grep「損壞的圖像」 'identify:corrupt image'image_with_error.png'@ error/png.c/ReadPNGImage/4051.' –

-2

我使用標識:

$ identify image.tif 
00000005.tif TIFF 4741x6981 4741x6981+0+0 8-bit DirectClass 4.471MB 0.000u 0:00.010 
$ echo $? 
+0

即使對於損壞的圖像,識別也將返回0。 –

1

下面是使用識別另一種解決方案,但沒有轉換:

identify -verbose *.png 2>&1 | grep "corrupt image"

identify: corrupt image 'image_with_error.png' @ error/png.c/ReadPNGImage/4051.