python下的繪圖需要import pychartdir模組,可以去http://www.advsofteng.com/index.html下載,根據自己系統的版本進行相應選擇。
下載下來後,並不是標準的安裝,需要進行相關的設定,首先用tar命令解壓了
tar -zxvf chartdir_python_version.tar.gz
找到Python 模組尋找路徑目錄下(即site_packages),此路徑可以通過python -c "import sys;print sys.path" 來擷取,建立一個chartdirector目錄
進入Chartdirector/ lib,將其下所有內容copy到上邊建立的目錄,同時在site_packages下touch一個chartdirector.pth,vim編輯加入chartdirector
進入python,即可:
ystan@debian: pythonPython 2.6.7 (r267:88850, Nov 28 2011, 09:24:44) [GCC 4.3.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import pychartdir>>>
try: from PIL import Imageexcept ImportError: import Imagetry: from cStringIO import StringIOexcept ImportError: from StringIO import StringIO from django.core.files.base import ContentFiledef resize_image(image=None,size=50): if image.mode != 'RGB': image = image.convert('RGB') (w, h ) = image.size if w != size or h != size: if w > h: diff = (w - h) / 2 image = image.crop((diff, 0, w - diff, h)) else: diff = (w - h) / 2 image = image.crop((0, diff, w, h-diff)) image = image.resize((size, size), Image.ANTIALIAS) thumb = StringIO() image.save(thumb, 'JPEG') thumb_file = ContentFile(thumb.getvalue()) return thumb_file