2017-06-19 94 views
0

我正在渲染同一網頁的兩個版本,並希望知道最有效的空間使用。我的目標是通過將頁面輸出爲圖像並查看白色頁面的百分比來實現此目的。白色越少,效率越高(我的標準!)使用imagemagick輸出圖像的白色內容百分比R

R中的imagemagick如何用於輸出圖像的%白色內容?我看到是有可能通過命令行,但是如果你想要的是白色的(不淺灰色或什麼的,太)不能做這裏面R.

回答

2

發現了什麼,你可以做這樣的:

download.file("https://www.gravatar.com/avatar/b1cdf616876083f7c5ec1a49fc357530?s=328&d=identicon&r=PG", tf <- tempfile(fileext = ".png"), mode="wb") 
f <- function(fn) { 
    require(magick) 
    img <- image_read(tf) 
    r <- as.raster(img) 
    sum(substr(tolower(r), 1, 7) == "#ffffff")/length(r) * 100 
} 
f(tf) 
# [1] 44.33931 
+0

真的很整齊的答案,謝謝! – pluke

2

對不起,我不能幫你的東西R的一面,但在命令行適當的命令,你可能能夠適應如下:

convert image.png -fill black +opaque white -format "%[fx:mean*100]" info: 

與黑色代替(-fill black )一切都不是白色的(+opaque white ),然後計算結果的白色百分比。

例子

convert -size 100x10 xc:white -bordercolor red -border 50 a.png   # Generate test image 
convert a.png -fill black +opaque white -format "%[fx:mean*100]" info: # Determine whites 
4.5454 

enter image description here


convert -size 100x10 xc:white -bordercolor blue -border 10 a.png   # Generate test image 
convert a.png -fill black +opaque white -format "%[fx:mean*100]" info: # Determine whites 
27.7778 

enter image description here

+0

對不起,馬克。我現在看到你的第一行只是創建測試圖像。所以你一直都在糾正。我對這個疏忽表示歉意。我後來的帖子是不需要的。 – fmw42

1

在ImageMagick的命令行中,馬克的第二線工程,而無需知道其他的顏色。所以它適用於兩個圖像沒有他的第一行代碼。

convert VQZ9Y.png -fill black +opaque white -format "%[fx:100*mean]\%\n" info: 
4.54545% 

convert Qpdg8.png -fill black +opaque white -format "%[fx:100*mean]\%\n" info: 
27.7778%