How to save an image without axis or white space?

I am currently trying to save an image adn then reopen that image in MATLAB. I am running into an issue that when I save that image it is saving with the axis. This then causes me to reopen the image and have the image be a different diemnsion due to the axis saving. I have then turned off the axis as peopel have recommended from readings other comments on similar problems. However, when I turn off the axis, then the space where the axis used to be saves as white space on my image. I want to open the image and save the image as the same size. I have attached a few pictures of what the image saving looks like. I have attahced the orgional image called square, the working image which is the image without after saved and reopened in MATLAB, and then the same thing hapening wiht the axis which is the screen shot.
Attached below is also my current code for this section of my code.
set(gca, 'Visible', 'off');
saveas(gcf,'working_image.jpg');
close all
img = imread('working_image.jpg'); % Replace with your image file

6 Comments

Hi Alexandra,
The issue of changing image dimensions when saving with or without axes displayed involves adjusting the image saving process to maintain consistent dimensions.
% Disable axes and save the image without white space
set(gca, 'Visible', 'off');
set(gcf, 'Units', 'pixels');
set(gcf, 'Position', get(gcf, 'OuterPosition'));
saveas(gcf, 'working_image.jpg');
close all;
% Read the saved image without altering dimensions
img = imread('working_image.jpg');
By setting the figure's units to pixels and adjusting its position to match the outer position, the saved image will maintain its original dimensions without white space where the axes were displayed. Hope following these steps will help resolve your problem.
Thank you so much for helping. Unfortunately, I am still getting the same issue when I reopen the image that it is smaller than the orgional.
Hi Alexandra,
Could you please help clarify about issues with code in details like what is exactly you are trying to accomplish with your code. This will help me navigate you in the right direction.
My main goal is to save an image that I have opened in and have drawn a rectangle on. After saving that image, I then want to reopen that same image with it being the same size as the orgional image to draw more rectangles on.
My issue currently is that when I open the image the image opens with an axis. This then causes the image to be saved with the axis and thus the actual image part is smaller in the newly saved image. Even when I turn off the axis, the images being saved with extra white space around the axis still making the image smaller than the orgional image. I want to keep the image size the same because I have points stored that I want to apply to the new image.
Hi Alexandra,
Thanks for clarifying your issues in more details. Now, I have a complete picture of your objective, let me help you by resolving your issue by sharing my code as an example which will help resolve your problem and then you can reply if this is what you were looking for.
% Read the original image
originalImage = imread('original_image.jpg');
% Display the original image with a rectangle drawn on it
imshow(originalImage);
hold on;
rectangle('Position', [x, y, width, height], 'EdgeColor', 'r', 'LineWidth', 2);
hold off;
% Save the image without the axis
set(gca, 'Visible', 'off');
set(gcf, 'Units', 'pixels', 'Position', get(gcf, 'Position'));
saveas(gcf, 'image_with_rectangle.png');
close;
% Reopen the saved image without the axis
savedImage = imread('image_with_rectangle.png');
% Display the saved image with the same size as the original image
figure('Units', 'pixels', 'Position', get(0, 'Screensize'));
imshow(savedImage);
hold on;
% Draw more rectangles or apply stored points on the new image
rectangle('Position', [new_x, new_y, new_width, new_height], 'EdgeColor', 'b', 'LineWidth', 2);
hold off;
So, let me explain the above code, it reads the original image and displays it with a red rectangle drawn on it.It then saves the image without the axis by turning off the axis visibility and saving the figure without extra white space. The saved image is reopened and displayed with the same size as the original image by setting the figure's position to the screen size.Additional rectangles or stored points can be applied to the new image as needed.
Now, I will wait for your response.
Hi! I tried your code and greatly appreciate you heling me. However, I am still having the same issues that I have been having. You can see that then the image opens a seocnd time there is now white space that appears around the black and inside the axis. This is what I am trying to git rid of. I just want to save the image that is the orgional black box without saving the extra white space that appears around the image after I save it once.

Sign in to comment.

Answers (2)

First things first, unless you need to include other graphical objects (lines, markers, annotations) in the image, save images uing imwrite(), not by taking screenshots of the figure.
If you do need to include other things in the output, then you can try to get the output geometry to match the input geometry using truesize(). There are limitations, and it's not always exact, but it's a lot simpler than trying to just use saveas(). If you try using saveas() without a ton of extra brittle work, you can expect the output size to essentially be random. Trying to use something like truesize() to force figure/axes geometry won't work with saveas().
% an image (256x256)
inpict = imread('cameraman.tif');
% an image object with other objects that need to be captured
figure % use a new figure
imshow(inpict); hold on
plot([100 200 150 100],[50 130 200 50],'linewidth',2)
% this simply doesn't work easily
saveas(gca,'alwayswrongsize.png') % NO JPG
% verify the output geometry
S = imfinfo('alwayswrongsize.png');
[S.Height S.Width]
ans = 1x2
522 678
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% try to use truesize() with saveas()!
drawnow
truesize(gcf,size(inpict,1:2))
saveas(gca,'stillswrongsize.png') % NO JPG
% verify the output geometry
S = imfinfo('stillswrongsize.png');
[S.Height S.Width]
ans = 1x2
471 495
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Instead, you can try to use truesize() with getframe()/imwrite() to save the image, though depending on the version or circumstance, I wouldn't guarantee it to always be exact.
% an image (256x256)
inpict = imread('cameraman.tif');
% an image object with other objects that need to be captured
figure % use a new figure
imshow(inpict); hold on
plot([100 200 150 100],[50 130 200 50],'linewidth',2)
% _try_ to force figure geometry to match the image
drawnow
truesize(gcf,size(inpict,1:2))
% save the image using getframe() and imwrite()
screenshot = frame2im(getframe(gca));
imwrite(screenshot,'screenshot.png') % NO JPG
% verify the output geometry
S = imfinfo('screenshot.png');
[S.Height S.Width]
ans = 1x2
256 256
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Truesize() does require there to be an image object in the figure. The attached function captureaxes() does not, and seems to be at least as consistent.
% an image (256x256)
inpict = imread('cameraman.tif');
% an image object with other objects that need to be captured
figure % use a new figure
imshow(inpict); hold on
plot([100 200 150 100],[50 130 200 50],'linewidth',2)
% save the image using captureaxes() and imwrite()
screenshot = captureaxes(gca,size(inpict,1:2));
imwrite(screenshot,'screenshotca.png') % NO JPG
% verify the output geometry
S = imfinfo('screenshotca.png');
[S.Height S.Width]
ans = 1x2
256 256
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
There are other tools like exportgraphics(), but I don't have that, so I can't trust that it consistently produces output that's actually on-size.
You can use imwrite() to save any matrix as an image. It won't save any axis labels or graphics in the overlay.
You can use getimage() to extract the original image from an axes control, like if one function put an image into the axes but a second function wanted to save that image but was not passed the original image from the other function.
You can use getframe() to get, basically, a screenshot of the displayed image. It will give you the image at whatever resolution it is displayed at, which may not be the original size - it depends on how you sized the figure, like from dragging a corner of the figure window to resize it. It will give you the graphics in the overlay as "burned into" the RGB image that getframe gives you. You can then use imwrite to save that screenshot image (+ overlay) to disk. It does not save axis tick labels. Example:
img = imread('moon.tif');
imshow(img);
impixelinfo;
hold on
% Put rectangle into overlay.
rectangle('Position', [10, 10, 200 200], 'EdgeColor', 'r', 'LineWidth', 2);
strScreenshot = getframe(gca); % Get screenshot
rgbImage = strScreenshot.cdata;
figure
imshow(rgbImage)
axis off;
impixelinfo;
imwrite(rgbImage, 'Screenshot.png');

Asked:

on 11 Jul 2024

Answered:

on 12 Jul 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!