2014/06/15(日) [n年前の日記]
#1 [python] Python 2.7.6でsetuptoolsやpipをインストールしようとしてハマった
以下の症状でハマりました。
_2013/12/19 Python-2.7.6がWindows環境でmimetypes.init()に失敗する場合がありsetuptoolsがインストールできない話 - 清水川Web
_[Python-ml-jp 5663] Windows 用の Python 2.7.6 に setuptools 2.0.1 を設置できなかった話
_[Python-ml-jp 5664] Re: Windows 用の Python 2.7.6 に setuptools 2.0.1 を設置できなかった話
_姫踊子草の楽屋裏 Windows 用の Python 2.7.6 に setuptools 2.0.1 を設置できなかった話
_Issue 9291: mimetypes initialization fails on Windows because of non-Latin characters in registry - Python tracker
_cpython: 18cfc2a42772
以下を参考にして作業。
_http://hg.python.org/cpython/raw-file/18cfc2a42772/Lib/mimetypes.py から、mimetypes.py をDLして、Python2.7.6インストールフォルダ\Lib\mimetypes.py を置き換えました。
と、ここまでやってたら、 _Windows 7 / Python 2.7.6 にて ez_setup.py にて UnicodeDecodeError が出て困ったけど解決した件 - secretbase.log という記事に遭遇。
_2013/12/19 Python-2.7.6がWindows環境でmimetypes.init()に失敗する場合がありsetuptoolsがインストールできない話 - 清水川Web
_[Python-ml-jp 5663] Windows 用の Python 2.7.6 に setuptools 2.0.1 を設置できなかった話
_[Python-ml-jp 5664] Re: Windows 用の Python 2.7.6 に setuptools 2.0.1 を設置できなかった話
_姫踊子草の楽屋裏 Windows 用の Python 2.7.6 に setuptools 2.0.1 を設置できなかった話
_Issue 9291: mimetypes initialization fails on Windows because of non-Latin characters in registry - Python tracker
_cpython: 18cfc2a42772
以下を参考にして作業。
ノート 2014/5/5追記
Python本家のバグチケット( http://bugs.python.org/issue9291 )がクローズされたようです。 http://hg.python.org/cpython/rev/18cfc2a42772 にある差分(か http://hg.python.org/cpython/raw-file/18cfc2a42772/Lib/mimetypes.py のファイル) をPython-2.7.6に適用したところ、問題が再現しなくなりました。2013/12/19 Python-2.7.6がWindows環境でmimetypes.init()に失敗する場合がありsetuptoolsがインストールできない話 - 清水川Web より
_http://hg.python.org/cpython/raw-file/18cfc2a42772/Lib/mimetypes.py から、mimetypes.py をDLして、Python2.7.6インストールフォルダ\Lib\mimetypes.py を置き換えました。
と、ここまでやってたら、 _Windows 7 / Python 2.7.6 にて ez_setup.py にて UnicodeDecodeError が出て困ったけど解決した件 - secretbase.log という記事に遭遇。
Python 2.7.7 にて下記不具合として修正されていますので、2.7.7を使いましょう。2.7.7が出てたのかー。Windows 7 / Python 2.7.6 にて ez_setup.py にて UnicodeDecodeError が出て困ったけど解決した件 - secretbase.log より
◎ Python 2.7.7 をインストール。 :
Python 2.7.6 をアンインストールして、Python 2.7.7 をインストールした。
ふと気づいたら、GIMP 2.6.12 の Python-fu が動かなくなっていた。Python 2.6.6 をアンインストールしてインストールし直したり。
相変わらず Python-fu が動かない。何度かアンインストール、再インストールを繰り返したけど問題解決せず。
ふと、別のショートカットファイルから GIMP を起動したら、Python-fu が動くことに気付いた。ショートカットファイルの設定で、作業フォルダに %USERPROFILE% を指定している場合は、Python-fu が動くらしい。…なんでだろ。
ふと気づいたら、GIMP 2.6.12 の Python-fu が動かなくなっていた。Python 2.6.6 をアンインストールしてインストールし直したり。
相変わらず Python-fu が動かない。何度かアンインストール、再インストールを繰り返したけど問題解決せず。
ふと、別のショートカットファイルから GIMP を起動したら、Python-fu が動くことに気付いた。ショートカットファイルの設定で、作業フォルダに %USERPROFILE% を指定している場合は、Python-fu が動くらしい。…なんでだろ。
[ ツッコむ ]
#2 [windows] Windows7 x64 に curl をインストール
Python 関係のパッケージ?インストール解説記事中で curl というコマンドラインのダウンローダ?を使っていたので、試しに自分も導入。
_cURL - Download の下の方にある 「Win32 - Generic」のどれかを選択。
今回は、 _cURL groks URLs - CURL 7.37.0 から、curl_737_0_ssl.zip をDLした。
解凍して、curl.exe を、パスの通ったところにコピー。
このままだと、https でアクセスできないらしいので、以下を参考に作業。
_Why can't cURL properly verify a certificate on Windows? - Super User
_cURL - Extract CA Certs from Mozilla
cacert.pem をDLして、curl.exe と同じ階層に、curl-ca-bundle.crt というファイル名でコピー。
あるいは、--insecure をつけることでも、https でアクセスできる?
_WindowsR環境にcURLコマンドを実行できるようにインストールする手順 | ええかげんブログ(本店)
_cURL - Download の下の方にある 「Win32 - Generic」のどれかを選択。
今回は、 _cURL groks URLs - CURL 7.37.0 から、curl_737_0_ssl.zip をDLした。
解凍して、curl.exe を、パスの通ったところにコピー。
このままだと、https でアクセスできないらしいので、以下を参考に作業。
_Why can't cURL properly verify a certificate on Windows? - Super User
_cURL - Extract CA Certs from Mozilla
cacert.pem をDLして、curl.exe と同じ階層に、curl-ca-bundle.crt というファイル名でコピー。
あるいは、--insecure をつけることでも、https でアクセスできる?
_WindowsR環境にcURLコマンドを実行できるようにインストールする手順 | ええかげんブログ(本店)
curl --insecure https://www.google.co.jp/といった指定でもイケるらしい。
[ ツッコむ ]
#3 [python] 画像の周辺の黒い部分をトリミングするPythonスクリプト
Python + PIL で、トリミング作業の実験。
getbbox() で、周辺の黒い部分を取り除いた領域の値を得られるらしい。
そんな便利なメソッドがあったとは知らなかったものだから、画像のドット数分、getpixel() を使って1ドットずつチマチマと全部チェックして、コレはさすがに処理が遅すぎるわ、高速化できんかなー、とか思ってました。getbbox() を使ったら一瞬で処理が終わってしまった…。
from optparse import OptionParser
import os.path
import glob
try:
import Image
except ImportError:
from PIL import Image
ver = "0.0.1"
def chk_args():
"""コマンドラインオプションを解析."""
parser = OptionParser(usage = "%prog [--trim] img\*.png",
version = "%prog " + ver)
parser.add_option("--trim", dest="trim",
action="store_true", default=False, help="Trim exec")
(opts, args) = parser.parse_args()
if len(args) == 0:
parser.print_help()
exit
return (opts, args)
def open_image(fn, enable_trim):
"""画像ファイルを開いて情報を調べる."""
if not os.path.isfile(fn):
print "Not Found %s" % fn
return
im = Image.open(fn).convert('RGB')
w, h = im.size
xmin, ymin, xmax, ymax = im.getbbox()
xw = xmax - xmin
yh = ymax - ymin
print "DrawArea (%d, %d) - (%d, %d) : %d x %d : Img Size %d x %d : %s" % \
(xmin, ymin, xmax, ymax, xw, yh, w, h, fn)
if enable_trim:
region = im.crop((xmin, ymin, xmax, ymax))
dn = os.path.dirname(fn)
bn = os.path.basename(fn)
new_fn = os.path.join(dn, "trim_%s" % bn)
region.save(new_fn)
def main():
(opts, args) = chk_args()
for fng in args:
for fn in glob.glob(fng):
open_image(fn, opts.trim)
if __name__ == '__main__':
main()
getbbox() で、周辺の黒い部分を取り除いた領域の値を得られるらしい。
そんな便利なメソッドがあったとは知らなかったものだから、画像のドット数分、getpixel() を使って1ドットずつチマチマと全部チェックして、コレはさすがに処理が遅すぎるわ、高速化できんかなー、とか思ってました。getbbox() を使ったら一瞬で処理が終わってしまった…。
◎ 2014/06/17追記。 :
[ ツッコむ ]
以上、1 日分です。