显示标签为“Python”的博文。显示所有博文
显示标签为“Python”的博文。显示所有博文

2009年9月17日星期四

python寫的改變圖片大小的腳本

用python寫了一個處理圖片的腳本,當然用到了另一個opensource的軟件ImageMagick-6.5.5-5.tar.gz,

安裝是三板斧 configure, make , make install

這裡用到的是python的一個系統調用subprocess.call

#! /usr/bin/env python
import sys, os, subprocess
fileSrc = open('./testClass2.txt','r')
aDict = dict()                  #創建一個dictionary
for line in fileSrc:
        head, tail = line.split(',')
        aDict[head]= tail    #生成一個dictionary
fileSrc.close()

for atom in aDict:

        if int(aDict[atom]) == 1:    
                srcStr=str('/home/demotest/source/yes/p9_%08d.jpg' % int(atom))
                destStr=str('/home/demotest/source/yes/1/1_p9_%08d.jpg' % int(atom))
               subprocess.call(['convert', srcStr, '-resize', '180x180', '-bordercolor', 'yellow', '-border', '10x10', destStr])
                print '1----True'
        elif int(aDict[atom]) == 9:
                srcStr=str('/home/demotest/source/yes/p9_%08d.jpg' % int(atom))
                destStr=str('/home/demotest/source/yes/9/9_p9_%08d.jpg' % int(atom))
                subprocess.call(['convert', srcStr, '-resize', '580x580', '-bordercolor', 'yellow', '-border', '10x10', destStr])
                print '9----True'
        elif int(aDict[atom]) == 4:
                srcStr=str('/home/demotest/source/yes/p9_%08d.jpg' % int(atom))
                destStr=str('/home/demotest/source/yes/4/4_p9_%08d.jpg' % int(atom))
                subprocess.call(['convert', srcStr, '-resize', '380x380', '-bordercolor', 'yellow', '-border', '10x10', destStr])
                print '4----True'
        else:
                print 'no----True'

我這裡本來嘗試使用cmd.split()方式來,但是測試了半天沒有通過,放棄了,所以就用比較笨的辦法拼出來.

記錄一下! 繼續研究python

2009年8月31日星期一

成功安裝了Pydev,運行了”Hello World”

在我的Mac上面安裝了 Eclipse插件Pydev, 運行了”Hello World”.

以後python的開發準備用這個東東了.

參考:http://ez2learn.com/index.php/python-tutorials/eclipse-pydev-tutorials/181-eclipse--pydev

Zen of python--應該是必讀的吧

The Zen of Python
    Beautiful is better than ugly. 漂亮別醜陋強
Explicit is better than implicit. 顯性比隱性的強
Simple is better than complex. 簡單比複雜強
Complex is better than complicated. 組合複雜比結構複雜強
Flat is better than nested. 直接比嵌套強
Sparse is better than dense. 稀疏比稠密強
Readability counts. 可讀性
Special cases aren't special enough to break the rules. 別壞了規矩
Although practicality beats purity. 與簡潔合拍
Errors should never pass silently. 有問題不能不出聲
Unless explicitly silenced. 
In the face of ambiguity, refuse the temptation to guess. 別叫人家猜
There should be one-- and preferably only one --obvious way to do it. 只有一個合適的方法
Although that way may not be obvious at first unless you're Dutch.
Now is better than never. 馬上行動
Although never is often better than *right* now. 
If the implementation is hard to explain, it's a bad idea. 如果實現的沒辦法解釋,它一定是個壞想法
If the implementation is easy to explain, it may be a good idea. 如果實現解釋起來很簡單,它也許是一個好想法
Namespaces are one honking great idea -- let's do more of those! 

ref: http://www.python.org/dev/peps/pep-0020/

準備用Python寫點小東西,遇到的第一個問題:python 調用系統命令

python
Python 2.6.2 (r262:71600, Aug 19 2009, 12:13:04)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> os.system('ls')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
###貌似沒有找到os這個東東(module)

>>> import os
>>> os.system('ls')
Desktop             rubyStudy
>>>

出來了.

2009年8月27日星期四

Python 安裝三板斧

見笑,最近學習python,用到numpy這個包,因為裏面有array, python building-in沒有這個容器,所以要從外面安裝,Mac OS的直接從

http://sourceforge.net/projects/numpy/files/NumPy/ 下載最新的安裝即可.

但是開發環境是CentOS,咋整呢?只能用源代碼編譯的方式,摸索了半天,裝上了,記錄一下,估計Python的第三方包都是這樣安裝的吧:

 

下載最新的tar包

wget –c http://sourceforge.net/projects/numpy/files/NumPy/1.3.0/numpy-1.3.0.tar.gz/download

tar xvfz numpy-1.3.0.tar.gz

[root@CRRACTEST2 software]# cd numpy-1.3.0
[root@CRRACTEST2 numpy-1.3.0]# ls
build           doc          MANIFEST.in  README.txt   setupscons.py
COMPATIBILITY   INSTALL.txt  numpy        setupegg.py  site.cfg.example
DEV_README.txt  LICENSE.txt  PKG-INFO     setup.py     THANKS.txt
[root@CRRACTEST2 numpy-1.3.0]#

安裝:

python ./setup.py build ##第一步是build

python ./setup.py install  ##第二步是install

測試:

[root@CRRACTEST2 ~]# python
Python 2.6.2 (r262:71600, Aug 19 2009, 12:13:04)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from numpy import *
>>> vec = array([1,2])
>>> vec +2
array([3, 4])
>>> vec *3
array([3, 6])
>>>

可用了,繼續研究!