手機裡有很多G+裡的截屏,都有一些黑邊。像這樣:
因為原圖片的長寬不一定,導致黑邊的大小不一定。
雖然圖片都是居中的,但是有些圖片是寬比高大,是橫著截圖的,所以這樣的情況是上下左右都可能有黑邊。
於是想到了用python的 PIL庫來進行裁切。
研究了一下,原代碼如下
IL import Image
import os
src_folder = "."
tar_folder = "tar"
backup_folder = "backup"
def isCrust(pix):
return sum(pix) < 25
def hCheck(img, y, step = 50):
count = 0
width = img.size[0]
for x in xrange(0, width, step):
if isCrust(img.getpixel((x, y))):
count += 1
if count > width / step / 2:
return True
return False
def vCheck(img, x, step = 50):
count = 0
height = img.size[1]
for y in xrange(0, height, step):
if isCrust(img.getpixel((x, y))):
count += 1
if count > height / step / 2:
return True
return False
def boundaryFinder(img,crust_side,core_side,checker):
if not checker(img,crust_side):
return crust_side
if checker(img,core_side):
return core_side
mid = (crust_side + core_side) / 2
while mid != core_side and mid != crust_side:
if checker(img,mid):
crust_side = mid
else:
core_side = mid
mid = (crust_side + core_side) / 2
return core_side
pass
def handleImage(filename,tar):
img = Image.open(os.path.join(src_folder,filename))
if img.mode != "RGB":
img = img.convert("RGB")
width, height = img.size
left = boundaryFinder(img, 0, width/2, vCheck)
right = boundaryFinder(img, width-1, width/2, vCheck)
top = boundaryFinder(img, 0, height/2, hCheck)
bottom = boundaryFinder(img, height-1, width/2, hCheck)
rect = (left,top,right,bottom)
print rect
region = img.crop(rect)
region.save(os.path.join(tar,filename),'PNG')
pass
def folderCheck(foldername):
if foldername:
if not os.path.exists(foldername):
os.mkdir(foldername)
print "Info: Folder \"%s\" created" % foldername
elif not os.path.isdir(foldername):
print "Error: Folder \"%s\" conflict" % foldername
return False
return True
pass
def main():
if folderCheck(tar_folder) and folderCheck(src_folder) and folderCheck(backup_folder):
for filename in os.listdir(src_folder):
if filename.split('.')[-1].upper() in ("JPG","JPEG","PNG","BMP","GIF"):
handleImage(filename,tar_folder)
os.rename(os.path.join(src_folder,filename),os.path.join(backup_folder,filename))
pass
if __name__ == '__main__':
main()
#handleImage('Screenshot_2013-10-13-21-55-14.png','')
代碼作用是 遍歷當前文件夾下的所有JPG PNG BMP GIF文件,找到內容上下左右的邊距(實際上是內容矩形左上右下���點的坐標),將切出來的圖片保存到目標文件夾中,並備份源文件到備份文件夾中。
boundaryFinder() 函數使用時間復雜度為O(log n)的算法來找到內容的邊緣所在的線(可能是橫線也可能是豎線,取決於checker使用的是哪一個函數)
hCheck() 和 vCheck() 函數分別水平和豎直地對圖片上的某條線,相等間隔(默認50)的取點。如果這些點有一半被認為屬於要被切掉的邊,就會返回true
isCrust()函數判斷點是否屬於要切掉的邊 本例中 RGB三色加起來小於25 就被認為是要黑邊的內容
效果如下:
這個代碼適用於有純色邊的圖片剪切。通過修改isCrust() 函數的算法 可以適用於不同純色邊的裁切
《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm
《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm
Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm
在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm
Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡