日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python知识:如何从图片的四周扩大一些尺寸

發(fā)布時(shí)間:2025/3/21 python 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。