from PIL import Image
def clipping(img, width):
'''
通過(guò)獲取像素點(diǎn),然后將像素點(diǎn)等于255(透明),從而完成修剪圖片的操作
:param img: Image對(duì)象
:param width: 要修剪的寬度
:return:
'''
w, h = img.size
pixdata = img.load()
# 修剪左邊
for x in range(width):
for y in range(0, h):
pixdata[x, y] = 255
# 修剪右邊
for x in range(w - width, w):
for y in range(0, h):
pixdata[x, y] = 255
# 修剪上邊
for x in range(0, w):
for y in range(0, width):
pixdata[x, y] = 255
# 修剪下邊
for x in range(0, w):
for y in range(h - width, h):
pixdata[x, y] = 255
return img
img = Image.open(filepath)
img = clipping(img, 2)
網(wǎng)站欄目:Python:裁剪圖片
網(wǎng)頁(yè)地址:
http://weahome.cn/article/joicsc.html