Adding a grid on top of a tif image in python

Hi, I was wondering how to add a grid on top of my image and then render it in python. Here is a picture of what I want to do. NOTE. I also want to specify the type and color of the line for some image blocks, as shown in the figure below. Many thanks. this image was generated in matlab

+3
source share
2 answers

Function imshowto display the image. Displaying the grid on top of the axes is as simple asgrid(True).

0
source

The following example shows how to display a .tif file, create a grid, and how to place a grid below other elements of the chart so that you can draw rectangles and lines on top of the image and grid.

import matplotlib.pyplot as plt
from PIL import Image
import matplotlib.patches as mpatches

im = Image.open('stinkbug.tif')

# Flip the .tif file so it plots upright
im1 = im.transpose(Image.FLIP_TOP_BOTTOM)

# Plot the image
plt.imshow(im1)
ax = plt.gca()

# create a grid
ax.grid(True, color='r', linestyle='--', linewidth=2)
# put the grid below other plot elements
ax.set_axisbelow(True)

# Draw a box
xy = 200, 200,
width, height = 100, 100
ax.add_patch(mpatches.Rectangle(xy, width, height, facecolor="none",
    edgecolor="blue", linewidth=2))

plt.draw()

plt.show()

matplotlib.patches . , , matplotlib.lines.Line2D.

plt.axvline(x=0.069, ymin=0, ymax=40, linewidth=4, color='r')
0

All Articles