LoginSignup
2
2

More than 5 years have passed since last update.

IRuby notebook で String 変数に格納しているPNG 画像データをインラインで表示する方法

Posted at

画像がインライン表示されなかった

IRuby notebook の example をみると、画像がインラインで表示されているので、画像データが変数にあるときにどのようすると表示できるのか調べた。

例えば、PNG画像を返すREST APIがあるとして、rest-client で GET すると、画像が String として取得される。


png_addr = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/2244/PNG"
png = RestClient.get(png_addr)
png #=> "\x89PNG\r\n\u001A\n\u0000\u0000\u0000\rIHDR\u0000\u0000\u0001,\u0000\u0000\u0001,\b\u0003\u0000\u0000\u00019\xA4N\xD1\u0000\u0000\u0001\u0011PLTE\xF5\xF5\xF5/OO\xFF\u0000\u0000f\x8B\x8B\xA6\xB3\xB3~\x91\x91Wpp\xCD\xD4Ԡ(省略)

この文字列は、ファイルに保存するとPNG画像として扱うことができる。しかし、ファイルを経由せずに表示したい。

IRuby.display

さて、これをインラインで画像表示するには、IRuby.display で MIME タイプをimage/pngと指定すると良いことが display.rb のコードIRuby-Examples.ipynbを見るとわかる。

PUG_RestClient.png

このようにPNG画像がインラインで表示される。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2