DAY 10
0
Modern Web

### 慢慢帶你了解Flask系列 第 10 篇

((插入剛剛的對話──

``````import numpy as np
import cv2

out_path='new.jpg'
h,w=img.shape[0],img.shape[1]
side=max(h,w)
new = np.zeros((side,side,3), np.uint8)
new.fill(255)
if w>h:
center=((side-h)/2.0)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
new[int(i+center),j]=img[i,j]
else:
center=((side-w)/2.0)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
new[i,j+center]=img[i,j]

cv2.imwrite(out_path, new)
``````

`cv2.imread (你要讀取的圖片)`這行……我好像已經打出來他要用來幹麻了。
`h,w=img.shape[0],img.shape[1]`這就是讀取img的長寬。
`new = np.zeros((side,side,3), np.uint8)`這是創建一個新的img，長框設為side
`new.fill(255)`但是他的預設是黑色，所以我將它改為白色。(255是白色)

``````def fill_photo(img,out_path):

h,w=img.shape[0],img.shape[1]
side=max(h,w)
new = np.zeros((side,side,3), np.uint8)
new.fill(255)
if w>h:
center=((side-h)/2.0)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
new[int(i+center),j]=img[i,j]
else:
center=((side-w)/2.0)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
new[i,int(j+center)]=img[i,j]
cv2.imwrite(out_path, new)
``````

``````@app.route('/upload/',methods=['GET','POST'])

basepath = os.path.join(os.path.dirname(__file__), 'static','uploads')
dirs.insert(0,'New Folder')
dirs.insert(0,'Not Choose')

if request.method == 'POST':
flist = request.files.getlist("file[]")

for f in flist:
try:
basepath = os.path.join(os.path.dirname(__file__), 'static','uploads')
format=f.filename[f.filename.index('.'):]
fileName=time.time()
if format in ('.jpg','.png','.jpeg','.HEIC','.jfif'):
format='.jpg'
else:
format='.mp4'

if request.values['folder']=='0':

elif request.values['folder']=='1':

if format == '.mp4':

else:

else:
if format == '.mp4':
else:

if format =='.mp4':
else:
fill_photo(image,album_path)

except:

``````

### 1 則留言

0

iT邦新手 4 級 ‧ 2019-09-12 13:33:58