Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You will need the python imaging library (PIL) installed. (See here <a href="https://pypi.python.org/pypi/PIL" rel="nofollow noreferrer">https://pypi.python.org/pypi/PIL</a>). See these answers for examples of ways to install PIL: <a href="https://stackoverflow.com/questions/9070074/how-to-install-pil-on-mac-os-x-10-7-2-lion">answer 1</a>, <a href="https://stackoverflow.com/questions/8863917/importerror-no-module-named-pil">answer 2</a></p> <p>Right, with that installed, the following code should do what you ask for:</p> <pre><code>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) </code></pre> <p>Which, if used on the <code>grace_hopper.png</code> example file, produces the following output:</p> <p><img src="https://i.stack.imgur.com/9jY9C.png" alt="Image with numbered grid"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload