キーワードからグラフ画像が取得できるのは前回書いた通り。続いてはグラフ画像の解析ですが・・・
$ wget -O nana.png "http://d.hatena.ne.jp/keywordstats/%bf%e5%bc%f9%c6%e0%a1%b9?mode=graph&type=refcount&range=365&date=2006-12-18" $ od -N 8 -t x1 nana.png 0000000 89 50 4e 47 0d 0a 1a 0a 0000010
どうやらPNGのようです。
http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html#PNG-file-signature
The first eight bytes of a PNG file always contain the following values:
(decimal) 137 80 78 71 13 10 26 10
(hexadecimal) 89 50 4e 47 0d 0a 1a 0a
(ASCII C notation) \211 P N G \r \n \032 \n
PNGならlibpngを使って解析できそうだけど・・・。残念ながら画像処理に関しては素人なので、まずライブラリーの使い方から勉強せねば。
$ ./pngcheck -v nana.png File: nana.png (2526 bytes) chunk IHDR at offset 0x0000c, length 13 400 x 200 image, 4-bit colormap, interlaced chunk PLTE at offset 0x00025, length 18: 6 palette entries chunk tRNS at offset 0x00043, length 1: 1 transparency entry chunk IDAT at offset 0x00050, length 2426 zlib: deflated, 32K window, default compression chunk IEND at offset 0x009d6, length 0 No errors detected in nana.png (93.7% compression). $ ./pngcheck -qp nana.png File: nana.png (2526 bytes) 0: (255,255,255) = (0xff,0xff,0xff) 1: ( 0,255, 0) = (0x00,0xff,0x00) 2: (255, 0, 0) = (0xff,0x00,0x00) 3: (127,127,127) = (0x7f,0x7f,0x7f) 4: ( 0, 0, 0) = (0x00,0x00,0x00) 5: ( 82,121,231) = (0x52,0x79,0xe7) 0: 0 = 0x00
なるほど。
ここで簡単にPNGのspecificationについて。検索すればいくらでも出てくるけど、PNGは8バイトのsignatureと多くのchunkと呼ばれるデータ構造の集まりで、メタデータも実データもchunkというデータ構造としてpackされる。
http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html#Chunk-layout
- Length(4byte)
- Chunk Type(4byte)
- Chunk Data(任意)
- CRC(4bit)
で、必ず含んでいなければならないchunkは次の4つ。
http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.Critical-chunks
- 1.IHDR(Image header)
- イメージ情報
- 2.PLTE(Palette)
- パレット情報*1
- 3.IDAT(Image data)
- 画像の実データ
- 4.IEND(Image trailer)
- ファイルの終了を表すchunk
上の画像にあったtRNS(Transparency)はCritical chunksには入ってないけど、補助チャンク(Ancillary chunks)として定義されているようです。
*1:場合によってはなくてもよい