python 在图像上画线_在matplotlib中的图像上绘制网格线

您将需要安装

python成像库(PIL). (见

https://pypi.python.org/pypi/PIL).有关安装PIL:

answer 1,

answer 2的方法的示例,请参阅这些答案

对,安装完毕后,以下代码应该做你所要求的:

import matplotlib.pyplot as plt

import matplotlib.ticker as plticker

try:

from PIL import Image

except ImportError:

import Image

# Open image file

image = Image.open('myImage.tiff')

my_dpi=300.

# Set up figure

fig=plt.figure(figsize=(float(image.size[0])/my_dpi,float(image.size[1])/my_dpi),dpi=my_dpi)

ax=fig.add_subplot(111)

# Remove whitespace from around the image

fig.subplots_adjust(left=0,right=1,bottom=0,top=1)

# Set the gridding interval: here we use the major tick interval

myInterval=100.

loc = plticker.MultipleLocator(base=myInterval)

ax.xaxis.set_major_locator(loc)

ax.yaxis.set_major_locator(loc)

# Add the grid

ax.grid(which='major', axis='both', linestyle='-')

# Add the image

ax.imshow(image)

# Find number of gridsquares in x and y direction

nx=abs(int(float(ax.get_xlim()[1]-ax.get_xlim()[0])/float(myInterval)))

ny=abs(int(float(ax.get_ylim()[1]-ax.get_ylim()[0])/float(myInterval)))

# Add some labels to the gridsquares

for j in range(ny):

y=myInterval/2+j*myInterval

for i in range(nx):

x=myInterval/2.+float(i)*myInterval

ax.text(x,y,'{:d}'.format(i+j*nx),color='w',ha='center',va='center')

# Save the figure

fig.savefig('myImageGrid.tiff',dpi=my_dpi)

如果在grace_hopper.png示例文件中使用,则会生成以下输出:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值