<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>taichino.com &#187; helloworld</title>
	<atom:link href="http://taichino.com/tag/helloworld/feed" rel="self" type="application/rss+xml" />
	<link>http://taichino.com</link>
	<description>永遠のネバーランド</description>
	<lastBuildDate>Sat, 04 Feb 2012 13:33:04 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>hello twisted world</title>
		<link>http://taichino.com/programming/1228</link>
		<comments>http://taichino.com/programming/1228#comments</comments>
		<pubDate>Tue, 24 Nov 2009 21:05:45 +0000</pubDate>
		<dc:creator>taichino</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[helloworld]]></category>
		<category><![CDATA[twisted]]></category>

		<guid isPermaLink="false">http://taichino.com/?p=1228</guid>
		<description><![CDATA[気になっていたtwistedをちょっとだけ試してみました。名前からtwitterのライブラリか何かだと思っていたのは僕だけですかそうですか。実際はネットワークライブラリで、専ら、簡単に各種プロトコルを利用したアプリケーションを実装する事ができるという噂です。 インストールはいつものeasy_installで楽々です。zope.interfaceは何故か自動でインストールされなかったので別でインストールしました。 $ easy_install zope.insterface $ easy_install twisted さてインストールも完了して、とっかかりが欲しい訳ですが、apiリファレンスを見てもあまりの膨大さにめまいがします。なので先ずは幾つか用意されているサンプルコードを見るのが良いかと思います。 上記のサンプルコードの中にあるエコーサーバを少しだけいじって見ました。以下がそのコードになります。ReverseEchoというのがエコーサーバの本体で、データを受け取った時の挙動をdataReceivedに記述します。ここでは受け取ったデータを反転して出力しています。残りのコードで作成したエコーサーバにポートを指定して走らせています。 from twisted.internet import reactor, protocol &#160; class ReverseEcho&#40;protocol.Protocol&#41;: def dataReceived&#40;self, data&#41;: self.transport.write&#40;data.strip&#40;&#41;&#91;::-1&#93; + &#34;\n&#34;&#41; &#160; if __name__ == '__main__': server = protocol.ServerFactory&#40;&#41; server.protocol = ReverseEcho reactor.listenTCP&#40;10000, server&#41; reactor.run&#40;&#41; 異様に簡単ですね。telnetを使って挙動を確認してみましょう。 $ python rev_echo.py &#38; $ telnet 127.0.0.1 10000 hello twisted world dlrow detsiwt olleh ちゃんと動いています。Cでソケット叩くとこうは行かないです。便利な世の中になりましたね。 次に「Python [...]]]></description>
			<content:encoded><![CDATA[<p>気になっていたtwistedをちょっとだけ試してみました。名前からtwitterのライブラリか何かだと思っていたのは僕だけですかそうですか。実際はネットワークライブラリで、専ら、簡単に各種プロトコルを利用したアプリケーションを実装する事ができるという噂です。</p>
<p><span id="more-1228"></span></p>
<p>インストールはいつものeasy_installで楽々です。zope.interfaceは何故か自動でインストールされなかったので別でインストールしました。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ easy_install zope.insterface
$ easy_install twisted</pre></div></div>

<p>さてインストールも完了して、とっかかりが欲しい訳ですが、<a href="http://twistedmatrix.com/documents/current/api/classIndex.html">apiリファレンス</a>を見てもあまりの膨大さにめまいがします。なので先ずは幾つか用意されている<a href="http://twistedmatrix.com/projects/core/documentation/examples/">サンプルコード</a>を見るのが良いかと思います。</p>
<p>上記のサンプルコードの中にあるエコーサーバを少しだけいじって見ました。以下がそのコードになります。ReverseEchoというのがエコーサーバの本体で、データを受け取った時の挙動をdataReceivedに記述します。ここでは受け取ったデータを反転して出力しています。残りのコードで作成したエコーサーバにポートを指定して走らせています。</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor, protocol
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> ReverseEcho<span style="color: black;">&#40;</span>protocol.<span style="color: black;">Protocol</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">def</span> dataReceived<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, data<span style="color: black;">&#41;</span>:
    <span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>data.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
  server = protocol.<span style="color: black;">ServerFactory</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  server.<span style="color: black;">protocol</span> = ReverseEcho
  reactor.<span style="color: black;">listenTCP</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10000</span>, server<span style="color: black;">&#41;</span>
  reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>異様に簡単ですね。telnetを使って挙動を確認してみましょう。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ python rev_echo.py <span style="color: #000000; font-weight: bold;">&amp;</span>
$ telnet 127.0.0.1 <span style="color: #000000;">10000</span>
hello twisted world
dlrow detsiwt olleh</pre></div></div>

<p>ちゃんと動いています。Cでソケット叩くとこうは行かないです。便利な世の中になりましたね。<br />
次に「<a href="http://lowlife.jp/yasusii/stories/24.html">Python と Twisted できみにも書ける Web サーバ</a>」という記事にあおられてtwisted.webを使ってみました。</p>
<p>先ほどのエコーサーバの様に基本的にはProtocolのサブクラスでデータの受け渡し処理を実装するようですが、メジャーなプロトコルに対しては既に専用のモジュール群が用意されていて、httpの場合はtwisted.webを使えば良さそうです。先ほどのページを元にして以下のコードを書いてみました。実質わずか4行ですが、静的ファイルしか扱えないものの立派なhttpdサーバとして動作します。</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> static, server
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
&nbsp;
DOC_ROOT = <span style="color: #483d8b;">'/tmp/www/html'</span>
PORT = <span style="color: #ff4500;">10001</span>
&nbsp;
res  = static.<span style="color: black;">File</span><span style="color: black;">&#40;</span>DOC_ROOT<span style="color: black;">&#41;</span>
<span style="color: #dc143c;">site</span> = server.<span style="color: black;">Site</span><span style="color: black;">&#40;</span>res<span style="color: black;">&#41;</span>
reactor.<span style="color: black;">listenTCP</span><span style="color: black;">&#40;</span>PORT, <span style="color: #dc143c;">site</span><span style="color: black;">&#41;</span>
reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>ドキュメントルートを/tmp/www/htmlに設定しています。適当なファイルを置いてブラウザからhttp://localhost:10001にアクセスしてみましょう。置いたファイルがブラウザで見れれば成功です。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>html
$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;hello twisted world&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>html<span style="color: #000000; font-weight: bold;">/</span>index.html
$ python static_web.py <span style="color: #000000; font-weight: bold;">&amp;</span>
$ w3m http:<span style="color: #000000; font-weight: bold;">//</span>localhost:<span style="color: #000000;">10001</span></pre></div></div>

<p>こんなに簡単にwebサーバを作り始められるとは思いませんでした。twisted恐るべしですね。サンプルコード眺めるだけでも十分面白いと思いました。</p>
]]></content:encoded>
			<wfw:commentRss>http://taichino.com/programming/1228/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello OpenCV World</title>
		<link>http://taichino.com/programming/867</link>
		<comments>http://taichino.com/programming/867#comments</comments>
		<pubDate>Tue, 01 Sep 2009 22:31:44 +0000</pubDate>
		<dc:creator>taichino</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[helloworld]]></category>
		<category><![CDATA[opencv]]></category>

		<guid isPermaLink="false">http://taichino.com/?p=867</guid>
		<description><![CDATA[ずっと&#8221;後でやる&#8221;状態だったOpenCVでヒストグラムを表示してみました。 インストール そろそろ最新版がリリースされるという噂があるので、リポジトリのヘッドを使おうとしたのですが、全然ビルドが成功しないので、仕方なしに1.1pre版を使いました。環境はosx 10.5.4です。 $ wget http://sourceforge.net/projects/opencvlibrary/files/opencv-linux/1.1pre1/opencv-1.1pre1.tar.gz/download $ tar zxvf opencv-1.1pre1.tar.gz $ cd opencv-1.1pre1 $ ./make &#38;&#38; make install ヒストグラムの表示 opencv.jpのサンプルコードを参考にしながら書きました。描画処理が面倒だったので、画像を白黒でロードして１次元のヒストグラムをベタ書きしています。 #include &#60; stdio .h &#62; #include &#60; cv .h &#62; #include &#60; highgui .h &#62; &#160; IplImage* createHistgram&#40;IplImage* srcImg, CvSize&#38; imageSize&#41;; &#160; int main&#40;int argc, char** argv&#41; &#123; // 画像読み込み(グレースケールで読み込み) char* imageFilename = &#34;sample.jpg&#34;; [...]]]></description>
			<content:encoded><![CDATA[<p>ずっと&#8221;後でやる&#8221;状態だったOpenCVでヒストグラムを表示してみました。</p>
<p><div id="attachment_878" class="wp-caption alignnone" style="width: 310px"><a href="http://taichino.com/wp-content/uploads/2009/09/Picture-10.png"><img src="http://taichino.com/wp-content/uploads/2009/09/Picture-10-300x134.png" alt="OpenCVでヒストグラム" title="Picture 10" width="300" height="134" class="size-medium wp-image-878" /></a><p class="wp-caption-text">OpenCVでヒストグラム</p></div><br />
<span id="more-867"></span></p>
<p><strong>インストール</strong><br />
そろそろ最新版がリリースされるという噂があるので、リポジトリのヘッドを使おうとしたのですが、全然ビルドが成功しないので、仕方なしに1.1pre版を使いました。環境はosx 10.5.4です。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>sourceforge.net<span style="color: #000000; font-weight: bold;">/</span>projects<span style="color: #000000; font-weight: bold;">/</span>opencvlibrary<span style="color: #000000; font-weight: bold;">/</span>files<span style="color: #000000; font-weight: bold;">/</span>opencv-linux<span style="color: #000000; font-weight: bold;">/</span>1.1pre1<span style="color: #000000; font-weight: bold;">/</span>opencv-1.1pre1.tar.gz<span style="color: #000000; font-weight: bold;">/</span>download
$ <span style="color: #c20cb9; font-weight: bold;">tar</span> zxvf opencv-1.1pre1.tar.gz
$ <span style="color: #7a0874; font-weight: bold;">cd</span> opencv-1.1pre1
$ .<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p><strong>ヒストグラムの表示</strong><br />
opencv.jpの<a href="http://opencv.jp/sample/histogram.html">サンプルコード</a>を参考にしながら書きました。描画処理が面倒だったので、画像を白黒でロードして１次元のヒストグラムをベタ書きしています。</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt; stdio .h &gt;</span>
<span style="color: #339900;">#include &lt; cv .h &gt;</span>
<span style="color: #339900;">#include &lt; highgui .h &gt;</span>
&nbsp;
IplImage<span style="color: #000040;">*</span> createHistgram<span style="color: #008000;">&#40;</span>IplImage<span style="color: #000040;">*</span> srcImg, CvSize<span style="color: #000040;">&amp;</span> imageSize<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span> argv<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// 画像読み込み(グレースケールで読み込み)</span>
    <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> imageFilename <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;sample.jpg&quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>argc <span style="color: #000080;">==</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    	imageFilename <span style="color: #000080;">=</span> argv<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    IplImage<span style="color: #000040;">*</span> img <span style="color: #000080;">=</span> cvLoadImage<span style="color: #008000;">&#40;</span>imageFilename, CV_LOAD_IMAGE_GRAYSCALE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>img<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    	<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;@E failed to load image %s @<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, imageFilename<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    	<span style="color: #0000dd;">exit</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #666666;">// Window作成</span>
    <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> windowTitle      <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;Original Image&quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> histWindowTitle  <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;Histgram&quot;</span><span style="color: #008080;">;</span>
    cvNamedWindow<span style="color: #008000;">&#40;</span>windowTitle, CV_WINDOW_AUTOSIZE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    cvNamedWindow<span style="color: #008000;">&#40;</span>histWindowTitle, CV_WINDOW_AUTOSIZE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// 元画像とヒストグラム表示</span>
    CvSize size <span style="color: #000080;">=</span> cvSize<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">256</span>, <span style="color: #0000dd;">256</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    IplImage<span style="color: #000040;">*</span> histImg <span style="color: #000080;">=</span> createHistgram<span style="color: #008000;">&#40;</span>img, size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    cvShowImage<span style="color: #008000;">&#40;</span>histWindowTitle, histImg<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    cvShowImage<span style="color: #008000;">&#40;</span>windowTitle, img<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// キー入力待ち</span>
    cvWaitKey<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// 破棄</span>
    cvReleaseImage<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>img<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    cvReleaseImage<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>histImg<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    cvDestroyAllWindows<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
IplImage<span style="color: #000040;">*</span> createHistgram<span style="color: #008000;">&#40;</span>IplImage<span style="color: #000040;">*</span> srcImg, CvSize<span style="color: #000040;">&amp;</span> imageSize<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">// ヒストグラム作成</span>
	<span style="color: #0000ff;">int</span>    channelSize<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">256</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">float</span>  levelRange<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>  <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">256</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">float</span><span style="color: #000040;">*</span> range<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>       <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> levelRange <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
	CvHistogram<span style="color: #000040;">*</span> hist    <span style="color: #000080;">=</span> cvCreateHist<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, channelSize, CV_HIST_ARRAY, range, <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	cvCalcHist<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>srcImg, hist<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// ヒストグラム出力用画像作成</span>
	IplImage<span style="color: #000040;">*</span> dstImg <span style="color: #000080;">=</span> cvCreateImage<span style="color: #008000;">&#40;</span>imageSize, IPL_DEPTH_8U, <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	cvSet<span style="color: #008000;">&#40;</span>dstImg, cvScalarAll<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">255</span><span style="color: #008000;">&#41;</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// ヒストグラムのスケール変換</span>
	<span style="color: #0000ff;">float</span> max <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	cvGetMinMaxHistValue<span style="color: #008000;">&#40;</span>hist, <span style="color: #0000dd;">0</span>, <span style="color: #000040;">&amp;</span>max, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	cvScale<span style="color: #008000;">&#40;</span>hist<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bins, hist<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bins, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span><span style="color: #008000;">&#41;</span>dstImg<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>height<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> max, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// 画像に出力</span>
	<span style="color: #0000ff;">int</span> binWidth <span style="color: #000080;">=</span> cvRound<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span><span style="color: #008000;">&#41;</span>imageSize.<span style="color: #007788;">width</span> <span style="color: #000040;">/</span> channelSize<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> channelSize<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	    <span style="color: #0000ff;">int</span>     histValue  <span style="color: #000080;">=</span> cvRound<span style="color: #008000;">&#40;</span>cvQueryHistValue_1D<span style="color: #008000;">&#40;</span>hist, i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	    CvPoint bottomLeft <span style="color: #000080;">=</span> cvPoint<span style="color: #008000;">&#40;</span>i <span style="color: #000040;">*</span> binWidth, dstImg<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>height<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	    CvPoint topRight   <span style="color: #000080;">=</span> cvPoint<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>i<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> binWidth, dstImg<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>height <span style="color: #000040;">-</span> histValue<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	    cvRectangle<span style="color: #008000;">&#40;</span>dstImg, bottomLeft, topRight, cvScalarAll<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>, CV_FILLED, <span style="color: #0000dd;">8</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #666666;">// ヒストグラム破棄</span>
	cvReleaseHist<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>hist<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> dstImg<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>これをビルドすれば冒頭の画面が表示されます。普段スクリプト言語使ってると本当にビルドが面倒です。幾つかライブラリをリンクしないと駄目です。</p>

<div class="wp_syntax"><div class="code"><pre class="make" style="font-family:monospace;">TARGET <span style="color: #004400;">=</span> helloworld
SRCS   <span style="color: #004400;">=</span> helloworld<span style="color: #004400;">.</span>cpp
INC    <span style="color: #004400;">=</span> <span style="color: #004400;">/</span>usr<span style="color: #004400;">/</span>local<span style="color: #004400;">/</span><span style="color: #666622; font-weight: bold;">include</span><span style="color: #004400;">/</span>opencv
LIB    <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>lcxcore <span style="color: #004400;">-</span>lcv <span style="color: #004400;">-</span>lhighgui
&nbsp;
all<span style="color: #004400;">:$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TARGET</span><span style="color: #004400;">&#41;</span>
<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TARGET</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">:$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">SRCS</span><span style="color: #004400;">&#41;</span>
	g<span style="color: #004400;">++</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">SRCS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>o <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TARGET</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>I<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">INC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LIB</span><span style="color: #004400;">&#41;</span>
run<span style="color: #004400;">:</span>all
	<span style="color: #004400;">./$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TARGET</span><span style="color: #004400;">&#41;</span>
clean<span style="color: #004400;">:</span>
	rm <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TARGET</span><span style="color: #004400;">&#41;</span></pre></div></div>

<p><strong>まとめ</strong><br />
<a href="http://opencv.jp/opencv-1.1.0/document/index.html">ドキュメント</a>の質が非常に良くて、サンプルも豊富にあるので、割とスムーズにコーディングできました。感触としては画像の解析能力は思ったよりも強力で、サンプルを実行するだけで楽しいです。ただ表示に必要な出力系APIが貧弱なので、もっぱら解析用に使うのが良さそうだと思いました。</p>
]]></content:encoded>
			<wfw:commentRss>http://taichino.com/programming/867/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Coroで効率良くスクレイピング</title>
		<link>http://taichino.com/programming/858</link>
		<comments>http://taichino.com/programming/858#comments</comments>
		<pubDate>Tue, 25 Aug 2009 09:01:40 +0000</pubDate>
		<dc:creator>taichino</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[coro]]></category>
		<category><![CDATA[helloworld]]></category>

		<guid isPermaLink="false">http://taichino.com/?p=858</guid>
		<description><![CDATA[スクレイピング処理をしていると大量のリクエストを発行する事が多いので、サーバの応答待ち時間がもったいないと感じていたのですが、最近巷でよく目にするCoroというモジュールを調べてみた所、非同期処理が割と簡単に書けるという事で試してみました。 以下では複数のURLに対してCoroを使って非同期でリクエストを投げる関数と、一つ一つリクエストを処理していく関数で、申し訳程度ですが比較ベンチマークを取っています。基本的にはasyncブロックの中に書いた処理が非同期になります。この例で注意するのはasyncブロック内のgetを非同期的に動かす為にCoro::LWPをuseしないと駄目だと言う事です。 #!/usr/bin/perl &#160; use strict; use warnings; use Perl6::Say; use Benchmark qw&#40;timethese cmpthese&#41;; &#160; use LWP::Simple; use Web::Scraper; use YAML; &#160; use Coro; use Coro::AnyEvent; use Coro::Handle; use Coro::LWP; # これが無いとLWP::Simple::getがそのまま同期処理になる &#160; # 候補リンクを取得する my $scraper = scraper &#123; process '//a', 'links[]' =&#62; '@href'; &#125;; my $res = $scraper-&#62;scrape&#40;URI-&#62;new&#40;'http://www.google.co.jp/'&#41;&#41;; my @links = @&#123; $res-&#62;&#123;links&#125; [...]]]></description>
			<content:encoded><![CDATA[<p>スクレイピング処理をしていると大量のリクエストを発行する事が多いので、サーバの応答待ち時間がもったいないと感じていたのですが、最近巷でよく目にする<a href="http://search.cpan.org/search?query=coro&amp;mode=all">Coro</a>というモジュールを調べてみた所、非同期処理が割と簡単に書けるという事で試してみました。</p>
<p><span id="more-858"></span></p>
<p>以下では複数のURLに対してCoroを使って非同期でリクエストを投げる関数と、一つ一つリクエストを処理していく関数で、申し訳程度ですが比較ベンチマークを取っています。基本的にはasyncブロックの中に書いた処理が非同期になります。この例で注意するのはasyncブロック内のgetを非同期的に動かす為に<a href="http://search.cpan.org/search?query=coro&amp;mode=all">Coro::LWP</a>をuseしないと駄目だと言う事です。</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Perl6<span style="color: #339933;">::</span><span style="color: #006600;">Say</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Benchmark <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>timethese cmpthese<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> LWP<span style="color: #339933;">::</span><span style="color: #006600;">Simple</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Web<span style="color: #339933;">::</span><span style="color: #006600;">Scraper</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> YAML<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> Coro<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Coro<span style="color: #339933;">::</span><span style="color: #006600;">AnyEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Coro<span style="color: #339933;">::</span><span style="color: #006600;">Handle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Coro<span style="color: #339933;">::</span><span style="color: #006600;">LWP</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;"># これが無いとLWP::Simple::getがそのまま同期処理になる</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># 候補リンクを取得する</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$scraper</span> <span style="color: #339933;">=</span> scraper <span style="color: #009900;">&#123;</span>
    process <span style="color: #ff0000;">'//a'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'links[]'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'@href'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$res</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">$scraper</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">scrape</span><span style="color: #009900;">&#40;</span>URI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'http://www.google.co.jp/'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@links</span>   <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>links<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
STDOUT<span style="color: #339933;">-&gt;</span><span style="color: #006600;">autoflush</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$result</span> <span style="color: #339933;">=</span> timethese<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    Blocking    <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'get_by_blocking'</span><span style="color: #339933;">,</span>
    NonBlocking <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'get_by_nonblocking'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
cmpthese<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
STDOUT<span style="color: #339933;">-&gt;</span><span style="color: #006600;">autoflush</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># 同期処理で取得する</span>
<span style="color: #000000; font-weight: bold;">sub</span> get_by_blocking <span style="color: #009900;">&#123;</span>
    say <span style="color: #ff0000;">&quot;@ BLOCKING @&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@content</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$link</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@links</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        say <span style="color: #ff0000;">&quot;retrieving $link&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@content</span><span style="color: #339933;">,</span> get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$link</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;"># 本来はここでスクレイピング</span>
        say <span style="color: #ff0000;">&quot;complete $link&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">return</span> <span style="color: #0000ff;">@content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># 非同期処理で取得する</span>
<span style="color: #000000; font-weight: bold;">sub</span> get_by_nonblocking <span style="color: #009900;">&#123;</span>
    say <span style="color: #ff0000;">&quot;@ NON BLOCKING @&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@cvs</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@content</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$link</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@links</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cv</span> <span style="color: #339933;">=</span> AnyEvent<span style="color: #339933;">-&gt;</span><span style="color: #006600;">condvar</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@cvs</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$cv</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        async <span style="color: #009900;">&#123;</span>
            say <span style="color: #ff0000;">&quot;retrieving $link&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #000066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@content</span><span style="color: #339933;">,</span> get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$link</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># 本来はここでスクレイピング</span>
            say <span style="color: #ff0000;">&quot;complete $link&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #0000ff;">$cv</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">send</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cv</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@cvs</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$cv</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">recv</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">return</span> <span style="color: #0000ff;">@content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>以下が実行結果になります。Googleのトップページのリンクを辿っているだけなので参考程度ですが、wallclockの値が結構大きく変わっているので、やはりサーバの応答待ち時間はバカに出来ない雰囲気です。少なくともこの例ではブロッキング処理にかかる時間のうち75%は応答待ちという事になります。これからは大量のリクエストは非同期で！</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Blocking:    59 wallclock secs ( 1.81 usr +  0.38 sys =  2.19 CPU) @  4.57/s (n=10)
NonBlocking: 14 wallclock secs ( 1.48 usr +  0.31 sys =  1.79 CPU) @  5.59/s (n=10)
&nbsp;
              Rate    Blocking NonBlocking
Blocking    4.57/s          --        -18%
NonBlocking 5.59/s         22%          --</pre></div></div>

<p>あと非同期で処理した方が何故かCPU負荷が下がっているんですが、理由はわかりません。どちらかというと非同期の方がアイドル時間が短いので負荷は高いと思うんですけど、不思議です。正確にwallclockとかCPU等の数字の意味を理解していないので、追々調べようと思います。</p>
]]></content:encoded>
			<wfw:commentRss>http://taichino.com/programming/858/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello plagger World</title>
		<link>http://taichino.com/programming/846</link>
		<comments>http://taichino.com/programming/846#comments</comments>
		<pubDate>Mon, 24 Aug 2009 19:49:57 +0000</pubDate>
		<dc:creator>taichino</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[helloworld]]></category>
		<category><![CDATA[plagger]]></category>

		<guid isPermaLink="false">http://taichino.com/?p=846</guid>
		<description><![CDATA[plaggerを触ってみました。結構時間がかかったのでメモ書きです。 練習のため、はてなブックマークのトップのRSSから、300件以上ブックマークされているエントリをCSVに、更にそのうち500件以上ブックマークされているエントリをDBに格納してみました。肝心のフィードを弄るところの理解が中々進んでいませんが、とりあえずは、以下の２つを覚えておけば欲しいフィードとエントリを集められそうです。 特定の条件にてフィードを削除 Filter::Rule 特定の条件にてフィードを複製 SmartFeed global: assets_path: /usr/local/share/plagger/assets &#160; plugins: # 入力となるFeedの設定 - module: Subscription::Config config: feed: - url: http://feeds.feedburner.com/hatena/b/hotentry &#160; # ユーザ数を取得して、エントリを修正 - module: Filter::HatenaBookmarkUsersCount - module: Widget::HatenaBookmarkUsersCount &#160; # 300件以下は削除 - module: Filter::Rule rule: module: Expression expression: $args-&#62;{entry}-&#62;{meta}-&#62;{hatenabookmark_users} &#62; 300 &#160; # 500件以上は複製 - module: SmartFeed rule: module: Expression expression: $args-&#62;{entry}-&#62;{meta}-&#62;{hatenabookmark_users} &#62; [...]]]></description>
			<content:encoded><![CDATA[<p>plaggerを触ってみました。結構時間がかかったのでメモ書きです。</p>
<p>練習のため、はてなブックマークのトップのRSSから、300件以上ブックマークされているエントリをCSVに、更にそのうち500件以上ブックマークされているエントリをDBに格納してみました。肝心のフィードを弄るところの理解が中々進んでいませんが、とりあえずは、以下の２つを覚えておけば欲しいフィードとエントリを集められそうです。</p>
<p><span id="more-846"></span></p>
<ul>
<li>
<strong>特定の条件にてフィードを削除</strong><br />
Filter::Rule
</li>
<li>
<strong>特定の条件にてフィードを複製</strong><br />
SmartFeed
</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">global:
  assets_path: /usr/local/share/plagger/assets
&nbsp;
plugins:
  # 入力となるFeedの設定
  - module: Subscription::Config
    config:
      feed:
        - url:  http://feeds.feedburner.com/hatena/b/hotentry
&nbsp;
  # ユーザ数を取得して、エントリを修正
  - module: Filter::HatenaBookmarkUsersCount
  - module: Widget::HatenaBookmarkUsersCount
&nbsp;
  # 300件以下は削除
  - module: Filter::Rule
    rule:
      module: Expression
      expression: $args-&gt;{entry}-&gt;{meta}-&gt;{hatenabookmark_users} &gt; 300
&nbsp;
  # 500件以上は複製
  - module: SmartFeed
    rule:
      module: Expression
      expression: $args-&gt;{entry}-&gt;{meta}-&gt;{hatenabookmark_users} &gt; 500
    config:
      id: copy
&nbsp;
  # 出力先1
  - module: Publish::CSV
    config:
      dir: .
      encoding: utf-8
      filename: out.csv
      mode: create
      column:
       - title
       - permalink
    rule:
      expression: $args-&gt;{feed}-&gt;id ne 'copy'
&nbsp;
  # 出力先2
  - module: Store::DBIC
    config:
      schema_class: Plagger::Schema::MySQL
      connect_info: ['dbi:mysql:plagger_db', 'plagger_user', 'plagger_pass']
    rule:
      expression: $args-&gt;{feed}-&gt;id eq 'copy'</pre></div></div>

<p>まだ、巷で言われている超便利！は感じられてないですが、それでも可能性が凄くあるのは解ります。あと、複数のYAMLでチェーン出来るともっと便利だと思うのは気のせいかしら。</p>
]]></content:encoded>
			<wfw:commentRss>http://taichino.com/programming/846/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello Python Image Library</title>
		<link>http://taichino.com/programming/827</link>
		<comments>http://taichino.com/programming/827#comments</comments>
		<pubDate>Thu, 20 Aug 2009 10:52:16 +0000</pubDate>
		<dc:creator>taichino</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[helloworld]]></category>
		<category><![CDATA[pil]]></category>

		<guid isPermaLink="false">http://taichino.com/?p=827</guid>
		<description><![CDATA[GAE上で画像を弄りたいという事でPILを触ってみました。ドキュメントを見る限りかなり多機能ですが、とりあえず一般的な処理を試してみました。楽チンですね。 #!/usr/bin/python # -*- coding: utf-8 -*- &#160; import Image import ImageFilter &#160; # ロードしてjpegへ image = Image.open&#40;'lena.ppm'&#41; print image.format, image.size image.save&#40;'lena.jpg', 'JPEG'&#41; &#160; # リサイズしてPNGへ small = image.resize&#40;&#91;128, 128&#93;&#41; small.save&#40;'thumbnail.png', 'PNG'&#41; &#160; # 回転してBMPへ rotate = image.rotate&#40;45&#41; rotate.save&#40;'rotate45.bmp', 'BMP'&#41; &#160; # 上下反転してGIFへ upsideDown = image.transpose&#40;Image.FLIP_TOP_BOTTOM&#41; upsideDown.save&#40;'upside_down.gif', 'GIF'&#41; &#160; # フィルタ処理してTIFFへ filtered = image.filter&#40;ImageFilter.DETAIL&#41; filtered.save&#40;'filtered.tiff', [...]]]></description>
			<content:encoded><![CDATA[<p>GAE上で画像を弄りたいという事でPILを触ってみました。<a href="http://www.pythonware.com/library/pil/handbook/index.htm">ドキュメントを見る限り</a>かなり多機能ですが、とりあえず一般的な処理を試してみました。楽チンですね。</p>
<p><span id="more-827"></span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> Image
<span style="color: #ff7700;font-weight:bold;">import</span> ImageFilter
&nbsp;
<span style="color: #808080; font-style: italic;"># ロードしてjpegへ</span>
image = Image.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'lena.ppm'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> image.<span style="color: black;">format</span>, image.<span style="color: black;">size</span>
image.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'lena.jpg'</span>, <span style="color: #483d8b;">'JPEG'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># リサイズしてPNGへ</span>
small = image.<span style="color: black;">resize</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">128</span>, <span style="color: #ff4500;">128</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
small.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'thumbnail.png'</span>, <span style="color: #483d8b;">'PNG'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># 回転してBMPへ</span>
rotate = image.<span style="color: black;">rotate</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">45</span><span style="color: black;">&#41;</span>
rotate.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'rotate45.bmp'</span>, <span style="color: #483d8b;">'BMP'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># 上下反転してGIFへ</span>
upsideDown = image.<span style="color: black;">transpose</span><span style="color: black;">&#40;</span>Image.<span style="color: black;">FLIP_TOP_BOTTOM</span><span style="color: black;">&#41;</span>
upsideDown.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'upside_down.gif'</span>, <span style="color: #483d8b;">'GIF'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># フィルタ処理してTIFFへ</span>
filtered = image.<span style="color: #008000;">filter</span><span style="color: black;">&#40;</span>ImageFilter.<span style="color: black;">DETAIL</span><span style="color: black;">&#41;</span>
filtered.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'filtered.tiff'</span>, <span style="color: #483d8b;">'TIFF'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>各種フォーマットへの対応が凄く便利で、web上というよりは手元でちょっと画像弄るような処理がとてもし易いですね。あと話がずれますが、変換等の確認にImageMagickのdisplayとidentifyコマンドが便利です。displayは画像の表示、identifyは画像の情報取得をそれぞれターミナルから行えます。</p>
]]></content:encoded>
			<wfw:commentRss>http://taichino.com/programming/827/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

