国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Python > 正文

python 實現將小圖片放到另一個較大的白色或黑色背景圖片中

2024-09-09 19:03:14
字體:
來源:轉載
供稿:網友

將圖片放入到白色或黑色背景圖中,前提是圖片小于背景圖片的尺寸,原圖為如下,長為192,寬為64。

1.將圖片放入到白色背景圖(224,224)中

import cv2import numpy as npdef img_resize_to_target_white(image): target = np.ones((224,224),dtype=np.uint8)*255 ret = cv2.cvtColor(target, cv2.COLOR_GRAY2BGR) h = image.shape[0] w = image.shape[1] for i in range(224):  for j in range(224):   if(i < h) and (j < w):    ret[i, j, 0] = image[i, j, 0]    ret[i, j, 1] = image[i, j, 1]    ret[i, j, 2] = image[i, j, 2]   else:    ret[i, j, 0] = 255    ret[i, j, 1] = 255    ret[i, j, 2] = 255 return ret if __name__ == '__main__': image = cv2.imread('/home/yasin/桌面/test.png') img_new_white = img_resize_to_target_white(image) cv2.imshow("img_new_white", img_new_white) cv2.waitKey() 

運行結果為:

2.將圖片放入到黑色背景圖(224,224)中

import cv2def img_resize_to_target_black(image): target = np.zeros((224,224),dtype=np.uint8) bgr_img = cv2.cvtColor(target, cv2.COLOR_GRAY2BGR) h = image.shape[0] w = image.shape[1] for i in range(h):  for j in range(w):   bgr_img[i, j, 0] = image[i, j, 0]   bgr_img[i, j, 1] = image[i, j, 1]   bgr_img[i, j, 2] = image[i, j, 2] return bgr_imgif __name__ == '__main__': image = cv2.imread('/home/yasin/桌面/test.png') img_new_black = img_resize_to_target_black(image) cv2.imshow("img_new_black", img_new_black) cv2.waitKey()

運行結果為:

以上這篇python 實現將小圖片放到另一個較大的白色或黑色背景圖片中就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網之家。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 马尔康县| 南宫市| 鄂托克旗| 汨罗市| 安阳县| 澄迈县| 永安市| 类乌齐县| 安乡县| 佛冈县| 崇阳县| 荃湾区| 永吉县| 方城县| 德安县| 河源市| 孟州市| 永德县| 美姑县| 汉川市| 长汀县| 昌吉市| 濮阳县| 仁怀市| 崇礼县| 渝中区| 河间市| 松潘县| 普兰店市| 长兴县| 沙湾县| 白城市| 沁水县| 清水河县| 高淳县| 盐池县| 延寿县| 宜都市| 定结县| 融水| 沙河市|