在矩阵中添加数字作为图像的简便方法?(Easy way to add a number as an image in a matrix?)

编程入门 行业动态 更新时间:2024-10-23 06:22:34
矩阵中添加数字作为图像的简便方法?(Easy way to add a number as an image in a matrix?)

我正在创建一个棋盘图案如下:

def CheckeredBoard( x=10 , y=10 , sq=2 , xmax = None , ymax = None ): coords = np.ogrid[0:x , 0:y] idx = (coords[0] // sq + coords[1] // sq) % 2 if xmax != None: idx[xmax:] = 0. if ymax != None: idx[:, ymax:] = 0. return idx ch = CheckeredBoard( 100, 110 , 10 ) plt.imshow2( ch )

我想要的是在一些方框中添加一个数字来编号,这样当我运行plt.imshow2( ch )我得到的数字是图像的一部分。

我能想到这样做的唯一方法是使用某种注释,然后保存图像并加载带注释的图像,但这看起来非常混乱。

例如,一个成功的矩阵看起来像:

1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 1

上面的矩阵在两个角上有1和8。 感谢任何帮助,如果您需要其他信息,请告诉我们。

谢谢

编辑

这是我最终想要的结果。

红色圆圈增加了强调。

I'm creating a checkerboard pattern as follows:

def CheckeredBoard( x=10 , y=10 , sq=2 , xmax = None , ymax = None ): coords = np.ogrid[0:x , 0:y] idx = (coords[0] // sq + coords[1] // sq) % 2 if xmax != None: idx[xmax:] = 0. if ymax != None: idx[:, ymax:] = 0. return idx ch = CheckeredBoard( 100, 110 , 10 ) plt.imshow2( ch )

What I would like is to add a number in some of the boxes to number them so that when I run plt.imshow2( ch ) I get the numbers be part of the image.

The only way I can think of doing this is by using some sort of annotation and then saving the image and loading the annotated image but this seems really messy.

For example a succesfull matrix would look like:

1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 1

The matrix above has a 1 and an 8 in the two corners. Appreciate any help, let me know if you want additional information.

Thanks

EDIT

Here is something closer to what I'd actually like to end up with.

Red circles added for emphasis.

最满意答案

那么使用PIL / Pillow呢?

import numpy as np import pylab from PIL import Image, ImageDraw, ImageFont #-- your data array xs = np.zeros((20,20)) #-- prepare the text drawing img = Image.fromarray(xs) d = ImageDraw.Draw(img) d.text( (2,2), "4", fill=255) #-- back to array ys = np.asarray(img) #-- just show pylab.imshow(ys)

What about using PIL / Pillow?

import numpy as np import pylab from PIL import Image, ImageDraw, ImageFont #-- your data array xs = np.zeros((20,20)) #-- prepare the text drawing img = Image.fromarray(xs) d = ImageDraw.Draw(img) d.text( (2,2), "4", fill=255) #-- back to array ys = np.asarray(img) #-- just show pylab.imshow(ys)

更多推荐

本文发布于:2023-04-27 14:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327176.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   简便   图像   数字   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!