技術メモBlog

Monday, October 29, 2007

Plone3 でプロパティ追加

profiles/default/propertiestool.xml
<?xml version="1.0"?>

<object name="portal_properties">

  <object name="site_properties">

    <property name="test_property" type="lines">

      <element value="TEST" />

    </property>

    <property name="test2_property" type="string">

      TEST_TEST

    </property>

    <property name="test3_property" type="boolean">1</property>

  </object>

</object>

Sunday, May 13, 2007

Python Image Library(PIL)のThumb関数

PloneのImageアイテムでも使用している、サムネイル関数について調べてみた。

Image.py の 1505行目(Ver.1.1.6) def thumbnail(self, size, resample=NEAREST):
にて行っている。

結論からすると、サムネイルを作成する、sizeに与えたタプルにの x, y それぞれより、
サムネイル後の画像が大きくなることはない。また、縦横比が変わることはない。

  • x, yに同じ値を与えると、元画像の長辺が、xの値になる。
  • 元画像に正方形を与えると、x, yの大きい方のサイズの正方形が出来上がる。
  • x:600, y:480 とし、元画像が、1000, 1200 の場合は、400,480となる。

下記に、Image.py内のコードをテストするコードを書いて実験してみた。


>>> def thumb(o_size,n_size):
x, y = o_size
a, b = n_size
if x > a:
y = max(y * a /x, 1)
x = a
if y > b:
x = max(x * b /y, 1)
y = b
print x,y



>>> thumb((1000,1000), (480,480))
480 480
>>> thumb((1000,1000), (600,480))
480 480
>>> thumb((1000,1500), (600,480))
320 480
>>> thumb((1000,1200), (600,480))
400 480
>>> thumb((1000,900), (600,480))
533 480
>>> thumb((1000,900), (320,480))
320 288
>>> thumb((1000,1200), (320,480))
320 384
>>> thumb((300,400), (320,480))
300 400
>>> thumb((350,400), (320,480))
320 365
>>> thumb((420,400), (320,480))
320 304
>>> thumb((300,480), (320,480))
300 480
>>> thumb((300,500), (320,480))
288 480
>>> thumb((300,500), (600,480))
288 480
>>> thumb((520,500), (600,480))
499 480
>>> thumb((1000,999), (600,480))
480 480
>>> thumb((1000,990), (600,480))
484 480
>>>

Monday, April 09, 2007

PythonからSHコマンド実行とテンポラリファイル

>>> import commands
>>> import os
>>>
>>> program = commands.getoutput("which コマンド")
>>> program = program.strip()
>>> o,i = os.popen4(program)
>>> list = i.readlines()
>>> o.close();i.colse()


>>> import tempfile
>>> fn = tempfile.mkstemp()
>>> file_buf = open(fn[1], 'wb')
>>> file_buf.write("テキストテキストテキスト")
>>> 何かの処理。
>>> file_buf.close()
>>> os.remove(fn[1])

文字コード判定

>>> import chardet
>>> w = 'テキスト'
>>> char_dic = chardet.detect(w)
>>> {'encoding': 'utf-8', 'confidence': 0.99}


http://chardet.feedparser.org/
char_dic.get('confidence')
char_dic.get('encoding')

File Descriptor の変更

/proc/sys/fs/file-max には、128213となっていた。

sysctl -w fs.file-max=257925 と実行する

Thursday, March 15, 2007

Python2.4のサードパーティーモジュール

よく使用する、Pythonモジュール

■PIL 画像ライブラリ
http://www.pythonware.com/products/pil/

■ElementTree XMLライブラリ
http://effbot.org/zone/element-index.htm

■Feed parser RSSなどのフィードライブラリ
http://www.feedparser.org

■Chardet 文字コード判定ライブラリ
http://chardet.feedparser.org/

■Beautiful Soup HTML解析
http://www.crummy.com/software/BeautifulSoup/

Saturday, January 27, 2007

urllib2で、User-agentなどを設定

>>> import urllib2
>>> opener = urllib2.build_opener()
>>> opener.addheaders = [('User-agent', 'Python2.4.3'),('Accept-Language','ja,en-us;q=0.7,en;q=0.3')]
>>> url = 'http://www.example.com/'
>>> f = opener.open(url)

Tuesday, January 23, 2007

Python urllibでcontent-typeをとる

>>> from urllib2 import urlopen
>>> url1 = 'http://www.example.jp'
>>> f = urlopen(url1)
>>> f.info().get('content-type')
'text/html;charset=utf-8'

>>> f.info().get('content-type', 'text/html;charset=utf-8').split(';')[0]
'text/html'

Saturday, January 20, 2007

PyLuceneインストール

ダウンロード
http://downloads.osafoundation.org/PyLucene/src/
現在の最新版 PyLucene-src-2.0.0-5.tar.gz

$ tar zxvf PyLucene-src-2.0.0-5.tar.gz
$ cd PyLucene-src-2.0.0-5

PyLucene-src-2.0.0-5内に、db-4.4.20フォルダを移動

Malefileを編集
$ vi Makefile

DB_VER=4.4.20

# Linux (with gcc 3.4.4 and libgcj statically linked)
PREFIX=/usr/local
PREFIX_PYTHON=$(PREFIX)
#GCJ_HOME=/usr/local/gcc-3.4.4
GCJ_HOME=/usr
GCJ_LIBDIR=$(GCJ_HOME)/lib
GCJ_STATIC=0
LIB_INSTALL=libstdc++.so.6 libgcc_s.so.1
DB=$(PYLUCENE)/db-$(DB_VER)
PREFIX_DB=$(PREFIX)/BerkeleyDB.$(DB_LIB_VER)
ANT=ant
PYTHON=$(PREFIX_PYTHON)/bin/python


$ make
# make install

Berkeley DB 4.4.20インストール

ダウンロード
http://www.oracle.com/technology/software/products/berkeley-db/db/index.html

$ cd db-4.4.20/build_unix
$ ../dist/configure
$ make
# make install

/usr/local/BerkeleyDB.4.4 にインストールされる。

/ect/ld.so.confに下記を追加
/usr/local/BerkeleyDB.4.4/lib

# ldconfig