我正在创建一个棋盘图案如下:
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 1The 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)更多推荐
发布评论