Applying image decoration (borders) in Python (programmatically)

I am looking for a way to create a border in python. Is there a library in Python that we can import to create a border.

Please note that I do not want to use image masks to create this effect (for example, I do not want to use any image editing package, such as GIMP, to create a border image mask).

Here is what I am looking for:

import fooImageBorders import Image foo = Image.open("someImage.jpg") foo2 = fooImageBorders.bevel(foo, color = black) 

... I can write my own methods for adding borders .. but if there is already something like this with a full set of border parameters, I would like to use it.

I looked at the PIL documentation and could not find a way to do this. I have windows xp and there seems to be no way to install PythonMagick for Python 2.6 unless you have cygwin.

thanks!

+4
source share
3 answers

Look at the ImageOps module in PIL.

 import Image import ImageOps x = Image.open('test.png') y = ImageOps.expand(x,border=5,fill='red') y.save('test2.png') 
+9
source

You can use the PythonMagick module . the documentation for this module is here (MagiC ++ documentation)

Example. To add a red frame with 2 pixels to the image, you will need the following code.

 from PythonMagick import Image i = Image('example.jpg') # reades image and creates an image instance i.borderColor("#ff0000") # sets border paint color to red i.border("2x2") # paints a 2 pixel border i.write("out.jpg") # writes the image to a file 
+2
source
 foo2 = foo.copy() draw = ImageDraw.Draw(foo2) for i in range(width): draw.rectangle([i, i, foo2.size[0]-i-1, foo2.size[1]-i-1], outline = color) 

foo2 will have a width pixel border color .

If you need different colored borders on each side, you can replace .rectangle with repeated calls to .line .

If you want the frame to not cover any part of the existing image, use foo.copy() instead.

 foo2 = Image.new(foo.mode, (foo.size[0] + 2*width, foo.size[1] + 2*width)) foo2.paste(foo, (width, width)) 
+1
source

All Articles