python知识:如何从图片的四周扩大一些尺寸
生活随笔
收集整理的這篇文章主要介紹了
python知识:如何从图片的四周扩大一些尺寸
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 問題描述:
?
如何將圖片原來尺寸適度外擴(kuò),或增加一個(gè)邊框尺寸,使得原圖產(chǎn)生邊框區(qū)域?如下列效果?
前提條件是務(wù)必操作簡(jiǎn)單易實(shí)現(xiàn)。
增加邊框后圖片:
2 應(yīng)用numpy的insert函數(shù)
采用numpy的insert函數(shù),下面顯示該函數(shù)用法:
example:
1)不指定張量維度
a = np.array([[1, 1], [2, 2], [3, 3]]) >>> a array([[1, 1],[2, 2],[3, 3]]) >>> np.insert(a, 1, 5) array([1, 5, 1, ..., 2, 3, 3]) >>> np.insert(a, 1, 5, axis=1) array([[1, 5, 1],[2, 5, 2],[3, 5, 3]])此時(shí)在第1個(gè)元素位置插入5;總體元素排列順序?yàn)?,1,2,2,3,3.
2)指定坐標(biāo)軸
a = np.array([[1, 1], [2, 2], [3, 3]]) np.insert(a, [1], [[1],[2],[3]], axis=1) array([[1, 1, 1],[2, 2, 2],[3, 3, 3]]) >>> np.array_equal(np.insert(a, 1, [1, 2, 3], axis=1), ... np.insert(a, [1], [[1],[2],[3]], axis=1)) True?3)平攤后
b = a.flatten() >>> b array([1, 1, 2, 2, 3, 3]) >>> np.insert(b, [2, 2], [5, 6]) array([1, 1, 5, ..., 2, 3, 3])4)切片?
>>> np.insert(b, slice(2, 4), [5, 6]) array([1, 1, 5, ..., 2, 3, 3])5)轉(zhuǎn)成浮點(diǎn)數(shù)?
>>> np.insert(b, [2, 2], [7.13, False]) # type casting array([1, 1, 7, ..., 2, 3, 3])6)指定行或列
>>> x = np.arange(8).reshape(2, 4) >>> idx = (1, 3) >>> np.insert(x, idx, 999, axis=1) array([[ 0, 999, 1, 2, 999, 3],[ 4, 999, 5, 6, 999, 7]])3? 程序代碼
import cv2 import numpy as np # 讀取圖片并轉(zhuǎn)至灰度模式 imagepath = 'huflv.jpg' img = cv2.imread(imagepath, 1) height,width,c = img.shape # print( img.shape) cv2.imshow("before",img) fiximg = np.insert(img, 50*[0,width] , (0,0,130), axis=1) newimg = np.insert(fiximg, 50*[0,height], (0,0,130), axis=0) cv2.imshow("before long",newimg) cv2.waitKey(0)總結(jié)
以上是生活随笔為你收集整理的python知识:如何从图片的四周扩大一些尺寸的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#教程01:关于C#
- 下一篇: python知识:NetworkX初步