1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#! /usr/bin/env python
#coding=utf-8
# SOURCE : http://www.developpez.net/forums/d857722/autres-langages/python-zope/general-python/rogner-automatiquement-image/
import Image
im = Image.open("test.png")
pix = im.load()
width, height = im.size
left,top,right, bottom =-1, -1, -1, -1
for left in xrange(width):
if any( pix[left,y][:3] != (255,255,255) for y in xrange(height)):
break
for right in xrange(width-1,left,-1):
if any( pix[right,y][:3] != (255,255,255) for y in xrange(height)):
break
for top in xrange(height):
if any( pix[x,top][:3] != (255,255,255) for x in xrange(width)):
break
for bottom in xrange(height-1,top,-1):
if any( pix[x,bottom][:3] != (255,255,255) for x in xrange(width)):
break
im = im.crop((left,top,right+1,bottom+1))
# We add a marg around the picture.
width, height = im.size
L = 20
color = "blue"
imL = Image.new("RGB", (width+2*L, height+2*L), color)
imL.paste(im, (L, L))
imL.save("test-margin.png") |
Partager