用Python进行图像赋值,在1RGB基础上,加入光流两个通道,代码如下所示: import numpy as npimport cv2import matplotlib.pyplot as plt path = 'frame_00003_rgb.png'img = cv2.imread(path)img1 = np.zeros([480, 640, 5])im
用Python进行图像赋值,在1RGB基础上,加入光流两个通道,代码如下所示:
import numpy as np import cv2 import matplotlib.pyplot as plt path = 'frame_00003_rgb.png' img = cv2.imread(path) img1 = np.zeros([480, 640, 5]) img1[:, :, 0:3] = np.array(img) cv2.imshow('test1', np.array(img)[:,:,2]) cv2.imshow('test2', img1[:, :, 0]) cv2.waitKey(10000)
打印出来的时候,图像显示空白:
后来找到了问题,是numpy数组11声明的问题,改成uint8就行了:如下所示:
import numpy as np import cv2 import matplotlib.pyplot as plt path = 'frame_00003_rgb.png' img = cv2.imread(path) img1 = np.zeros([480, 640, 5], dtype=np.uint8) img1[:, :, 0:3] = np.array(img) cv2.imshow('test1', np.array(img)[:,:,2]) cv2.imshow('test2', img1[:, :, 0]) cv2.waitKey(10000)
补充知识:python3改图片白色该为透明
我就废话不多说了,大家还是直接看代码吧!
# pip install Pillow import PIL.Image as Image # 以第一个像素为准,相同色改为透明 def transparent_back(img): img = img.convert('RGBA') L, H = img.size color_0 = (255,255,255,255)#要替换的颜色 for h in range(H): for l in range(L): dot = (l,h) color_1 = img.getpixel(dot) if color_1 == color_0: color_1 = color_1[:-1] + (0,) img.putpixel(dot,color_1) return img if __name__ == '__main__': img=Image.open('img.png') img=transparent_back(img) img.save('img2.png')
以上这篇解决python图像处理图像赋值后变为白色的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。