Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying different images with actual size in matplotlib subplot

Tags:

I'm working on some image processing algorithms using python and matplotlib. I'd like to display the original image and the output image in a figure using a subplot (e.g. the original image next to the output image). The output image(s) are of different size than the original image. I'd like to have the subplot display the images in their actual size (or uniformly scaled) so that I can compare "apples to apples". I currently use:

plt.figure() plt.subplot(2,1,1) plt.imshow(originalImage) plt.subplot(2,1,2) plt.imshow(outputImage) plt.show() 

The result is that I get the subplot, but both images are scaled so that they are the same size (despite the fact that the axes on the output image are different than the axes of the input image). Just to be explicit: if the input image is 512x512 and the output image is 1024x1024 then both images are displayed as though they are the same size.

Is there a way to force matplotlib to either display the images at their respective actual sizes (preferable solution so that matplotlib's dynamic rescaling doesn't effect the displayed image) or to scale the images such that they are displayed with sizes proportional to their actual sizes?

like image 533
Doov Avatar asked Mar 02 '15 17:03

Doov


People also ask

How do I display multiple images in one figure correctly in Matplotlib?

The easiest way to display multiple images in one figure is use figure(), add_subplot(), and imshow() methods of Matplotlib. The approach which is used to follow is first initiating fig object by calling fig=plt. figure() and then add an axes object to the fig by calling add_subplot() method.

How do I display an image in a subplot?

subplot divides a figure into multiple display regions. Using the syntax subplot(m,n,p) , you define an m -by- n matrix of display regions and specify which region, p , is active. For example, you can use this syntax to display two images side by side.


1 Answers

This is the answer that you are looking for:

def display_image_in_actual_size(im_path):      dpi = 80     im_data = plt.imread(im_path)     height, width, depth = im_data.shape      # What size does the figure need to be in inches to fit the image?     figsize = width / float(dpi), height / float(dpi)      # Create a figure of the right size with one axes that takes up the full figure     fig = plt.figure(figsize=figsize)     ax = fig.add_axes([0, 0, 1, 1])      # Hide spines, ticks, etc.     ax.axis('off')      # Display the image.     ax.imshow(im_data, cmap='gray')      plt.show()  display_image_in_actual_size("./your_image.jpg") 

Adapted from here.

like image 149
Joseph Avatar answered Oct 09 '22 12:10

Joseph