Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

psqlも-cオプションでSQL文を直接叩きつけることができる。

psqlも-cオプションでSQL文を直接叩きつけることができます。

実際のところ

基本的な使い方

postgresというDBに対し、"select byte_data from image where id = 0;"というSQL文を叩きつけたい場合は以下の通り。

$ psql postgres -c "select byte_data from image where id = 0;"
 byte_data  
------------
 \xdeadbeef
(1 row)

独自コマンド

postgresqlの独自コマンドを叩くこともできます。
例えば、\dコマンドでimageテーブルを見たい場合は以下のようにします。

$ psql postgres -c "\d image"
                           Table "public.image"
  Column   |  Type   |                     Modifiers                      
-----------+---------+----------------------------------------------------
 byte_data | bytea   | 
 id        | integer | not null default nextval('image_id_seq'::regclass)
Indexes:
    "id" PRIMARY KEY, btree (id)

Python系の導入時に"BUILD FAILED (OS X x.x.x using python-build y.y.y)"などと出る場合、xcode-selectがうまく導入できていないのかも

今回はmacOSのお話。
多分、他のOS Xでも同じです。

Python系の導入時に"BUILD FAILED (OS X x.x.x using python-build y.y.y)"などと出る場合、xcode-selectがうまく導入できていないのかも知れません。

実際のところ

背景

postgresqlpythonで使うpsycopg2を導入しようとした時の事。
いつも通りpip経由で導入しようとやったところ……

ld: library not found for -lssl


clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'clang' failed with exit status 1

おかしいですね。
何時もならすんなり行くんですが。

じゃぁPythonを3.5.2系にアップデートしたら解決するかとやってみると、以下のように出る始末。

BUILD FAILED (OS X 10.12.1 using python-build 1.0.4)

他のログも見てみると、ここでもc-lang系がやられているようです。

xcode-selectをちゃんと入れ直す

色々と調べてみた所、xcode-selectがうまく入っていないのが原因な模様。
そういえば、アプリ開発の為に一度クリーンインストールしたんでしたっけね……

$ xcode-select --install

xcode-select: note: install requested for command line developer tools

とでて、完了です。

この状態で

$ pip install psycopg2

とやったら素直にいってくれました。

バイナリファイルをHEX文字化してローカルに保存

xxdコマンドをつかうと、バイナリファイルをHEX文字化することができます。
そのままファイルとしてローカルに保存する事も可能です。

実際のところ

ここで大活躍するのがxxdコマンド。

$ xxd -p image.jpg | tr -d '\n' > dump.hex

オプションpは以下にある通り、平文で返してくれる子です。

-p | -ps | -postscript | -plain
output in postscript continuous hexdump style. Also known as
plain hexdump style.

改行コード削除は必要?

はい。xxd -pは気を使って60字で改行してくれちゃうので。

$ xxd -p image.jpg | wc -l
   25517
$ xxd -p image.jpg | tr -d '\n' | wc -l
       0

postgresをbrewで入れ直し

postgresをbrewで入れ直し

実際のところ

$ brew cleanup postgresql
$ brew remove postgresql
$ brew install postgresql

DBを新造

$ rm -rf /usr/local/var/postgres
$ initdb /usr/local/var/postgres -E utf8
$ pg_ctl -D /usr/local/var/postgres -l logfile start

必要ならば、

$ createuser -a -d -U postgres -P shuzo_kino
$ psql -l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | 
...
$ psql postgres
psql (9.5.3)
Type "help" for help.

postgres=#

pillowで画像を回転させる

# 時計回りに45度
out = im.rotate(45)
#左右反転
out = im.transpose(Image.FLIP_LEFT_RIGHT)
#上下反転
out = im.transpose(Image.FLIP_TOP_BOTTOM)

#90度、180度、270度回転
out = im.transpose(Image.ROTATE_90)
out = im.transpose(Image.ROTATE_180)
out = im.transpose(Image.ROTATE_270)

python3のprintf関数のオプション

python3のprintf関数のオプションでは改行文字や、イテレータ形式のくっつける文字を指定できたりします。
これはPEP3105文章で設定された話です。

実際のところ

くっつける文字はsepで指定。

str = "hoge"
print(*str, sep="+")
#>> h+o+g+e

終端文字をつけることも。これはend

print(*ary, sep=",", end="@")
#>> 1,2,3,4@

python2で

from __future__ import print_function

python3でも使える画像操作ライブラリPillow

Pillowはpython3でも使える画像操作ライブラリです。

参考もと

サムネイル

from __future__ import print_function
import os, sys
from PIL import Image

size = (128, 128)

for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size)
            im.save(outfile, "JPEG")
        except IOError:
            print("cannot create thumbnail for", infile)

使い方は

$ python make_thumbnail.py image.jpeg
$ ls 
make_thumbnail.py
image.jpeg
image.jpeg.thumbnail

コントラストを弄る

黒埋めする。

out = im.point(lambda i: i * 0)
out.save(outfile, "JPEG")