2011-02-17 101 views
13

我想定義一個函數來調用imagemagick來轉換圖像。在Python中使用ImageMagick。 (在Linux系統上)

def convert(filein,fileout): 
#imagemagick>convert filein fileout 

如何在Python中調用和使用imagemagick?

我在Linux系統上運行,imagemagick已安裝,並且我沒有使用PIL.module,因爲它不處理PPM [p3]。

+0

給魔杖一個嘗試,http://docs.wand-py.org/en/0.4.1/ – 2015-09-02 17:25:05

回答

7

可以使用Python(os.system,subprocess.Popen)的某個shell接口調用imagemagick二進制文件,或者嘗試PythonMagick

+1

謝謝,os.system工作perferctly。 – Alpagut 2011-02-17 11:57:02

3

我還沒有使用的圖像魔法,但你可以使用使用os.system調用shell命令:

import os 
os.system('imagemagick-converting-command filein fileout') 

我建議你去PythonMagic作爲Creshal說。它由ImageMagic提供,因此必須是可用於python的最佳端口之一。

6

我建議u使用它是安全

import subprocess 
params = ['convert', 'src_file', 'result_file'] 
subprocess.check_call(params) 
+0

好的答案,如果它已經在磁盤上,您也不需要將其加載到內存中的好處 – 2015-09-16 06:57:48

15

免責聲明:我魔杖的作者。

您可以使用Wand輕鬆做到這一點,ImageMagick for Python的一個簡單綁定。例如,下面的代碼PNG圖像轉換成JPEG圖像:

from wand.image import Image 

with Image(filename='in.png') as img: 
    img.format = 'jpeg' 
    img.save(filename='out.jpg') 

this tutorial爲好。

+0

您可以將其調整爲300%,如何添加平坦,以及如何更改背景? – Sekai 2014-12-19 13:14:17

相關問題