[[Vorlage(Archiviert)]] Ein kleines Modul, das die Verwendung von ImageMagick in Python ermöglicht. {{{#!code python #!/usr/bin/env python # -*- coding: utf-8 -*- # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Library General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. from popen2 import popen2 class image: def __init__(self, image): self.img = image def output(self): return self.img def execute(self, cmd): r,w = popen2(cmd) w.write(self.img) w.close() self.img = r.read() def resize(self, width, height="auto"): if height != "auto": scale = str(width)+"x"+str(height)+"\!" else: scale = str(width) self.execute("convert - -resize "+scale+" -") def rotate(self, angle): self.execute("convert - -rotate "+str(angle)+" -") def contrast(self, value): self.execute("convert - -contrast "+str(value)+" -") def mirror(self, vertical): if vertical: op = "-flip" else: op = "-flop" self.execute("convert - "+op+" -") def raisedborder(self, width): scale = str(width)+"x"+str(width) self.execute("convert - -raise "+scale+" -") def loweredborder(self, width): scale = str(width)+"x"+str(width) self.execute("convert - +raise "+scale+" -") def colorborder(self, width, color): scale = str(width)+"x"+str(width) self.execute("convert - -bordercolor \#"+color+" -border "+scale+" -") def roll(self, x, y): self.execute("convert - -roll "+str(x)+"x"+str(y)+" -") def gaussianblur(self, radius=2): self.execute("convert - -gaussian "+str(radius)+" -") def radialblur(self, angle): self.execute("convert - -radial-blur "+str(angle)+" -") def charcoal(self, factor): self.execute("convert - -charcoal "+str(factor)+" -") def colorize(self, value): self.execute("convert - -colorize "+str(value)+" -") def implode(self, factor): self.execute("convert - -implode "+str(factor)+" -") def solarize(self, factor): self.execute("convert - -solarize "+str(factor)+" -") def spread(self, size): self.execute("convert - -spread "+str(size)+" -") def emboss(self, radius): self.execute("convert - -emboss "+str(radius)+" -") def edge(self, radius): self.execute("convert - -edge "+str(radius)+" -") def noise(self, factor): self.execute("convert - -noise "+str(factor)+" -") def painting(self, factor): self.execute("convert - -paint "+str(factor)+" -") def swirl(self, deg): self.execute("convert - -swirl "+str(deg)+" -") def monochrome(self): self.execute("convert - -monochrome -") def negate(self): self.execute("convert - -negate -") def normalize(self): self.execute("convert - -normalize -") def overlay(self, color): self.execute("convert - -opaque \#"+color+" -") def maketrans(self, color): self.execute("convert - -transparent \#"+color+" -") def shade(self, azimuth, elevation): args = str(azimuth)+"x"+str(elevation) self.execute("convert - -shade "+args+" -") ################################################################################ #Kleines Beispiel, das die Datei test.jpg bearbeitet ################################################################################ if __name__ == "__main__": bild = image(file("test.jpg").read()) bild.resize(600) bild.spread(2) bild.gaussianblur(4) bild.swirl(-50) bild.spread(2) bild.swirl(50) bild.spread(2) bild.radialblur(4) bild.gaussianblur(4) file("test2.jpg","w").write(bild.output()) }}} ---- # tag: Programmierung