python - 通过使用 np.newaxis 进行 RGB 成像,从 (:,:,1) 扩展到 (:,:,3)
问题描述
我正在尝试将最初的列表“ThetaVect1”转换为形状为 (16,) 的 np.ndarray,将其更改为 (4,4) 数组,然后使用 np.newaxis 获取我试图将其设置为 3 的第三个维度,但无法弄清楚如何设置。
想法是,一旦我这样做了,我就可以根据每个“像素”不同的随机数 np.random.randint(0,255) 为我的灰度图像添加颜色。所以虽然我可以得到 print(Greyscale_Theta1_RGB.shape) = (4,4,1) 我不能把它变成 (4,4,3) 格式。我相信这是需要做的。
我正在尝试通过以下想法工作
Greyscale_ThetaVect1 = np.array(ThetaVect1,dtype=np.uint8)
print(Greyscale_ThetaVect1.shape)
Greyscale_Theta1 = np.reshape(Greyscale_ThetaVect1, (-1, 4))
Greyscale_Theta1_RGB = Greyscale_Theta1[:,:,None]
# Greyscale_Theta1_RGB [:,:,0] = np.random.randint(0,255)
# Greyscale_Theta1_RGB [:,:,1] = np.random.randint(0,255)
# Greyscale_Theta1_RGB [:,:,2] = np.random.randint(0,255)
print(Greyscale_Theta1_RGB.shape)
save_file = "CM.jpg"
i = Image.fromarray(Greyscale_Theta1).save(save_file)
i = Image.open("CM.jpg")
i.show()
编辑
使用 Mark Setchell 的出色答案以及此处接受的答案,我试图将随机颜色放入 2-D 图像阵列中。我用这段代码得到了一些东西:
for k,l in enumerate(rgb):
print(l)
rgb[k] = l * [random.randint(0, 255),random.randint(0, 255),random.randint(0, 255)]
这不太正确,因为有明显的黑线以及开头的黑条。图像被放大以显示直线的黑线。
我还通过将 f 更改为 : 来删除渐变,f = lambda i, j: int((128))
并且能够获得这个有趣的图像,但请注意不是像素而是线条。
解决方案
只是对@hpaulj 的评论发表评论......
只需复制并附加下面的代码片段,无需散布图像即可获得单个可运行的代码块。
我认为您有一个灰度图像,您想用颜色进行注释,但无法弄清楚如何将其制成 RGB 图像,并且可能还保留了您已有的灰度值。
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Define width and height big enough to see
w,h = 256,100
# Make left-right gradient of greyscale values - without going to pure white so you can see the extent on StackOverflow's white background
f = lambda i, j: int((j*192)/w)
gg = np.fromfunction(np.vectorize(f), (h,w)).astype(np.uint8)
这给了我们这个单通道灰度图像:
# Replicate greyscale and stack to make RGB image where R=G=B
rgb = gg[:,:,np.newaxis].repeat(3,2)
# If you find the above syntax difficult, here is an alternative
# ... stack the grey image 3 times in the "depth" dimension
# rgb = np.dstack((gg,gg,gg))
# DEBUG: Save image
Image.fromarray(rgb).save('result1.png')
这给了我们这个 RGB 图像:
# DRAWING PART
# Make top edge red 10px wide
rgb[:10,:,:]=[255,0,0]
# Make left border green 20px wide
rgb[:,:20,:]=[0,255,0]
# Make right border blue 30px wide
rgb[:,:-30:-1,:]=[0,0,255]
# DEBUG: Save image
Image.fromarray(rgb).save('result2.png')
如果您想使用 PIL 而不是使用 Numpy 绘制或着色图像,请删除上面“DRAWING PART”之后的代码并替换为以下代码:
from PIL import ImageDraw
# Make PIL Image from numpy array
rgb = Image.fromarray(rgb)
# Get drawing handle and draw magenta circle and save
draw = ImageDraw.Draw(rgb)
draw.ellipse([10,10,90,90],fill=(255,0,255))
rgb.save('result.png')
如果您只想要一个 700x300 的随机图像:
import numpy as np
from PIL import Image
# Generate a random image 700x300
im = np.random.randint(0,256,(300,700,3), dtype=np.uint8)
# Make into PIL Image, display and save
p = Image.fromarray(im)
p.display()
p.save('result.png')
如果您想在渐变顶部制作随机图像,您可以这样做:
import numpy as np
from PIL import Image
# Generate a random image 700x300
im = np.random.randint(0,256,(300,700,3), dtype=np.uint8)
gradient = np.linspace(0,1,700,dtype=np.float32) + np.zeros(300)[:, None]
im = im*np.dstack((gradient,gradient,gradient))
推荐阅读
- amazon-web-services - SSL 问题:具有负载均衡器和外部域的 AWS Beanstalk
- reactjs - Material UI 在 value 属性中使用一个对象选择多个组件,添加重复条目
- azure-iot-hub - IoT 中心设备孪生中的 etag 有什么用?
- netsuite - 使用 record.load() 加载子记录
- html - 为什么我的页面没有显示任何引导布局?
- apache-kafka - 来自 Kafka 的 Flink SQL CLI 客户端 CREATE TABLE
- shader - 片段着色器的 Spark AR 脚本中的点积错误
- javascript - 使用 jQuery 添加 onClick 方法
- .net - 实体框架 ThenInclude 不包括孙子的属性
- java - Java 8 流对列表进行分组