Monthly Archives: February 2014

PDFs and PythonMagic

At work I was tasked with converting python code from Mac to Linux that changes a PDF into a series of images. I found that PIL doesn’t do exactly what I want it do do, so I ended up trying ImageMagic’s wrapper PythonMagic. I’ve found, as everyone else on the internet, that there is no documentation. So here is the documentation I’ve generated for myself:

  • PythonMagic:

    • Image(string path) – creates an Image object. In the instance of a PDf, you can use the [int pagenum] suffix to create an Image of a specific page of the PDF or specific frame of a multi-frame image.
      image = PythonMagic.Image("/home/niels/example.pdf[2]")
    • Image(string size, Color bgColour) – creates an Image of the specified size and a specific color. The size parameter is a Geometry string of (int)x(int).
      size = "%dx%d"%(width, height)
      bgColor = PythonMagick.Color("#FFFFFF")
      image = PythonMagick.Image(size, bgColor)
    • Color(string color) – create a color object. Note I have found no implementation of any other Color subclass such as RGBColor. The color parameter can take a string (“white”, “blue”, “red”, etc) or the hex of the color (“#FFFFFF”, “#FF0000”, “#00FF00”, “#0000FF”, etc).
      colorWhite = PythonMagick.Color("#FFFFFF")
      print colorWhite // returns PythonMagick.Color object
    • Geometry(string size) – creates a Geometry object of size dementions in the format “(int)x(int)”.
      size = Geometry("500x500")
      print size // returns PythonMagick._PythonMagic.Geometry object
  • PythonMagic.Image:

    • magick(string type) – changes the type of the image.
      image = PythonMagick.Image("/home/niels/example.pdf[1]")
      image.magick('JPEG')
    • size() – gets the Geometry object of the Image object
      image = PythonMagick.Image("/home/niels/example.pdf[1]")
      size = image.size()
      print size // returns PythonMagick._PythonMagic.Geometry object
    • resize(string size(geometry)) – changes the size of the image to the specified geometry size.
      image = PythonMagick.Image("/home/niels/example.pdf[1]")
      image.resize("640x840")
    • density(string density) – reads in the image at set quality.
      image = PythonMagick.Image()
      image.density('600')
      image.read('/home/niels/example.pdf[1]')
    • quality(integer quality) – sets the compression for the image.
      image = PythonMagick.Image("/home/niels/example.pdf[1]")
      image.qulity(30)
    • read(string path) – reads in the image at the given path
      image = PythonMagick.Image()
      image.read("/home/niels/example.pdf[1]")
    • write(string path) – writes the image out to a given path
      image = PythonMagick.Image()
      image.read("/home/niels/example.pdf[1]")
      image.magick('JPEG')
      image.write("/home/niels/example_page1.jpg")
  • PythonMagic.Geometry:

    • width() – gets the width of the image.
      size = Geometry("500x500")
      width = size.width()
      print width //returns 500
    • height() – gets the height of the image.
      size = Geometry("500x500")
      height = size.height()
      print height //returns 500
  • PythonMagic.Color:

    • Nothing of importance yet.