-
大小: 988B文件類型: .zip金幣: 2下載: 0 次發(fā)布日期: 2021-05-18
- 語(yǔ)言: Python
- 標(biāo)簽: 圖像裁剪??深度學(xué)習(xí)??數(shù)據(jù)擴(kuò)充??Python??
資源簡(jiǎn)介
利用python進(jìn)行圖片裁剪,并設(shè)置有部分重疊區(qū)域,消除裁剪時(shí)的黑邊。

代碼片段和文件信息
import?numpy?as?np
from?PIL?import?Image
import?os
import?cv2
import?matplotlib.pyplot?as?plt
import?math
Image.MAX_IMAGE_PIXELS?=?400000000
imagedir?=?‘E:/sample/‘
savedir?=?‘E:/cutresult/‘
imagelist?=?os.listdir(imagedir)
for?i?in?range(0len(imagelist)):
path?=?os.path.join(imagedirimagelist[i])
filename?=?os.path.splitext(imagelist[i])[0]
filetype?=?os.path.splitext(imagelist[i])[1]
image?=?cv2.imread(path)
imagesize?=?image.shape
width?=?imagesize[1]
height?=?imagesize[0]
# image?=?Image.open(path)
# plt.imshow(image)
# plt.show()
# width?=?image.size[0]
# height?=?image.size[1]
numwidth?=?(math.ceil((width-500)/400))?+?1
numheight?=?(math.ceil((height-500)/400))?+?1
print(numwidth)
print(numheight)
for?numH?in?range?(0numheight):
up?=?400?*?numH
down?=?up?+?500
for?numW?in?range?(0numwidth):
left?=?400?*?numW?
right?=?left?+?500
if?numH?==?0?and?numW?==?0:
box?=?[00500500]
elif?numH?==?0?and?numW?>?0:
if?right?>?width:
box?=?[(width?-500)0(width)500]
else:
box?=?[left0right500]#
else:
if?right? box?=?[leftuprightdown]
elif?right?>?width?and?down? box?=?[(width?-?500)upwidthdown]
elif?right??height:
box?=?[left(height?-?500)rightheight]
else:
box?=?[(width?-?500)(height?-?500)widthheight]
#print?(box)#
numH?=?‘{:0>2d}‘.format(int(numH))
i?=?‘{:0>2d}‘.format(int(i))
numW?=?‘{:0>2d}‘.format(int(numW))
cutresult?=?os.path.join(savedirstr(i)+str(numH)+str(numW)+‘.png‘)
#newimage?=?image[box[0]:box[1]box[2]:box[3]]
newimage?=?image[box[1]:box[3]box[0]:box[2]]
#print?(newimage)
cv2.imwrite(cutresultnewimage)
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-09-13?14:00??imagecunt\
?????文件????????1816??2018-09-13?13:59??imagecunt\imagecut500.py
- 上一篇:RasaCore官方文檔中文版
- 下一篇:Python調(diào)用QQ微信截圖
評(píng)論
共有 條評(píng)論