我會改變該鏈接的背景色和前景色到不以你的形象出現,並使用webkit2png
渲染頁面的顏色。然後使用ImageMagick
查找與您的鏈接顏色相對應的顏色塊以製作圖像映射。
所以,具體而言,假設你改變你的HTML設置鏈接的前景色和背景色爲紅色(#FF0000),使你的HTML看起來是這樣的:
<p>
A link to Google follows:
<a style="color:#ff0000;background:#ff0000" href="www.google.com">Google</a> - a link to Google.
</p>
然後你做
webkit2png a.html
它給你一個PNG文件是這樣的:
![enter image description here](https://i.stack.imgur.com/z6TSP.png)
然後,您使用ImageMagick
上色一切不紅黑色,像這樣:
convert fileUsersmarktmpahtml-full.png -colorspace RGB -fill black +opaque "#ff0000" x.png
那你告訴ImageMagick的修剪的背景,只留下紅塊,並查看其座標與identify
,你可以看到該鏈接將獲取圖像映射的座標。
convert out.png -trim y.png
identify y.png
y.png PNG 47x18 800x600+176+16 8-bit sRGB 2c 3.18KB 0.000u 0:00.000
您可以看到紅色塊位於圖像的偏移+ 176 + 16處。
你可能想要做一個鏈接的時間,並重新渲染,或多個鏈接。如果您執行多個鏈接,請提出另一個關於在圖像中查找多個斑點的問題,或者搜索其他答案(由我執行),其中包含connected-components
。如果您遇到問題,比如,如何選擇未使用的顏色,或找到多個斑點,只是問了另一個問題 - 它們是免費的:-)
如果你有多個環節,更是這樣的:
<!DOCTYPE html>
<html>
<body>
<p>Here comes a link...
<a style="color:#ff0000;background:#ff0000" href="www.google.com">Google</a> - a link to Google.</p>
<p>And there will be another (longer) one along shortly...
<a style="color:#ff0000;background:#ff0000" href="www.google.com">Google, but longer</a> - a link to Google.</p>
</body>
</html>
你運行webkit2png
如上然後anayse這樣的:
convert out-full.png -colorspace RGB -fuzz 10% \
-fill black +opaque red \
-fill white -opaque red \
-define connected-components:verbose=true \
-define connected-components:area-threshold=100 \
-connected-components 4 -auto-level \
output.png
輸出
Objects (id: bounding-box centroid area mean-color):
0: 800x600+0+0 399.8,301.1 476976 rgba(0,0,0,1)
2: 121x18+357+50 417.0,58.5 2178 rgba(255,255,255,1)
1: 47x18+140+16 163.0,24.5 846 rgba(255,255,255,1)
現在您可以看到與第二行和第三行中的鏈接對應的blobs
。
convert input.png -crop 121x18+357+50 firstLink.png
convert input.png -crop 47x18+140+16 secondLink.png
什麼問題?我可以在圖像中看到另一種顏色的鏈接,並且它在文本中設置得很好! – SaidbakR 2015-02-11 11:59:03
您指的是等寬字體,但您的示例顯然不是等寬字體,因此您不能假設任何字符數/行數。這取決於你的角色是什麼。 – halfer 2015-02-11 12:00:00
這樣做的原因是什麼?將文本轉換爲圖像只會增加頁面的開銷。 – 2015-02-11 12:02:46