Reputation: 863
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?
Upvotes: 29
Views: 41950
Reputation: 1
Here is my solution, the basic idea comes from @MrE. However, in my case the relative size among figures across height and width may be different, thus whatever the display order we choose, either width or height of the first figure will become incomplete. (In another word, fig1's height is greater than fig2, while the fig1's width is smaller than fig2.)
The situation becomes more tricky when we need to display multiple images, and we couldn't simply reverse the display order of the images since the relative relationship of size is agnostic within a batch loop.
In this case, we could declare a blank image with the maximum height and width among all of the iamges, and don't neccessarily display it by set_visible to False.
def plot_image(ax, rgb_array, title, subplot_index):
image = Image.fromarray(rgb_array)
ax[subplot_index].imshow(image)
ax[subplot_index].set_title(title + f" {rgb_array.shape}", fontsize=22)
ax[subplot_index].axis("off")
return None
# Assuming resize_array is already defined
max_height = max(rgb_array.shape[0], resize_array.shape[0])
max_width = max(rgb_array.shape[1], resize_array.shape[1])
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(max_height//10, max_width//10), dpi=80, sharex=True, sharey=True)
plot_image(ax, rgb_array, "original", 0)
plot_image(ax, resize_array, "resized", 1)
# larger one should be plotted on the later
blank_array = np.full((max_height, max_width, 3), 255, dtype=np.uint8)
plot_image(ax, blank_array, "blank", 2)
ax[2].set_visible(False)
btw, the figure size is irrelevant to the problem due to the autoscale mechanism, the only trick here is to display the largest one last contributed by @MrE based on my tests.
Upvotes: 0
Reputation: 3477
One must consult the image dimensions and use the larger ones in order to create an apprropriate figure size. So, here we go:
dpi = 100
(h1,w1),(h2,w2) = originalImage.shape[:2],outputImage.shape[:2]
w,h = max(w1,w2),max(h1,h2) # Get largest width and largest height
figsize = (2*(w/dpi),h/dpi) # Set the figure size acc/ly
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=figsize, dpi=dpi, sharex=True, sharey=True)
ax[0].imshow(originalImage, cmap='gray')
ax[1].imshow(outputImage, cmap='gray')
plt.show()
Upvotes: 0
Reputation: 531
Adapting Joseph's answer here: Apparently the default dpi changed to 100, so to be safe in the future you can directly access the dpi from the rcParams as
import matplotlib as mpl
import matplotlib.pyplot as plt
def display_image_in_actual_size(im_path):
dpi = mpl.rcParams['figure.dpi']
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")
Upvotes: 13
Reputation: 20838
If you are looking to show images at their actual size, so the actual pixel size is the same for both images in subplots, you probably just want to use the options sharex
and sharey
in the subplot definition
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(15, 7), dpi=80, sharex=True, sharey=True)
ax[1].imshow(image1, cmap='gray')
ax[0].imshow(image2, cmap='gray')
results in:
Where the second image is 1/2 size of the first one.
Upvotes: 14
Reputation: 2577
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.
Upvotes: 23