How to export images to a single pdf file

45 ビュー (過去 30 日間)
Lora
Lora 2014 年 8 月 26 日
編集済み: DGM 2024 年 8 月 11 日
Hi I have a cell array and in it i have say 200 images of size(250,250). Now i want to generate a visualization i-e i want to generate a pdf file that will have all those 200 images in a single pdf file. Can someone please give a hint or guide me because i cant seem to find a possible solution by using print function.
Thanks anyways

回答 (3 件)

Joseph Cheng
Joseph Cheng 2014 年 8 月 26 日
There are some functions in the File Exchange but i find saving to ppt and then printing that to PDF works well. I haven't used the saveppt2() as much as i used the original one that it was based on saveppt() which can also be found on the File exchange. but if you want to go directly to pdf perhaps the one listed here http://www.mathworks.com/matlabcentral/answers/16000-multiple-figures-to-pdf-s would work for you.

Umar
Umar 2024 年 8 月 10 日
編集済み: DGM 2024 年 8 月 10 日
Hi @Lora ,
If I comprehend your issue correctly, you have a cell array containing 200 images, each sized 250x250 pixels and your goal is to compile these images into a single PDF file.
In order to tackle this problem, first I would make sure that images are stored in a cell array and each of the element of the cell array should contain an image matrix. Then, I will use MATLAB's figure functions to create a new figure for displaying images through each image in the cell array and display it in the figure. Then, use `exportgraphics` to save the figure as a PDF file. For more information on this function, please refer to
Here is complete code in MATLAB to help you out.
% Assume 'imagesCellArray' is your cell array containing 200 images
% Each image should be of size (250, 250)
% Initialize the number of images and set up the figure
numImages = length(imagesCellArray);
imagesPerRow = 10; % Number of images per row
imagesPerCol = ceil(numImages / imagesPerRow); % Calculate required rows
% Create a new figure for visualization
figure;
% Loop through each image and display it
for idx = 1:numImages
% Create subplot for each image
subplot(imagesPerCol, imagesPerRow, idx);
% Display the image
imshow(imagesCellArray{idx}); % Display the image from cell array
title(['Image ', num2str(idx)]); % Add title for clarity
end
% Adjust layout to prevent overlap
sgtitle('Collection of Images'); % Add a super title for context
% Save the figure as a PDF file
pdfFileName = 'images_collection.pdf';
exportgraphics(gcf, pdfFileName); % Save the current figure as PDF
disp(['PDF saved as ', pdfFileName]);
Make sure to adjust `imagesPerRow` based on how you want your images arranged in the PDF. For instance, setting it to 5 would result in more rows but fewer images per row. Now, you should be able to successfully create a PDF document containing all your images neatly organized and ready for presentation or sharing. Please let me know if you have any further questions.

DGM
DGM 2024 年 8 月 10 日
編集済み: DGM 2024 年 8 月 11 日
Saving images by taking screenshots will not ensure that the original resolution is preserved. I can only imagine that preserving resolution through the PPT-PDF routine would be at least as difficult. Whether that matters is up to you.
I don't know of any canonical ways to directly save images as PDF, but this is how I've done it for years.
% i don't have your data in a cell array
% so i'm going to load some placeholder data
S = load('stack.mat');
stack = S.stack;
% parameters
tiling = [4 3]; % image arrangement per page [y x]
bgcolor = [1 1 1]; % page color
outname = 'imagecatalog.pdf';
% write each page as a separate image
npicts = numel(stack);
npp = prod(tiling);
npages = ceil(npicts/npp);
tempnames = '';
for k = 1:npages
if npp == 1
thispage = stack{k};
else
stackidx = ((k-1)*npp + 1):min((k*npp),npicts);
thispage = imtile(stack,'frames',stackidx,'gridsize',tiling, ...
'bordersize',5,'background',bgcolor);
end
% write as a temporary PNG file
temppng = fullfile(tempdir(),sprintf('tempfile_%03d.png',k));
imwrite(thispage,temppng);
% convert the PNG to a single-page PDF
temppdf = strrep(temppng,'.png','.pdf');
system(sprintf('convert %s %s',temppng,temppdf));
% cleanup
tempnames = [tempnames ' ' temppdf]; % build list for the final command str
delete(temppng) % get rid of the temp PNG
end
% concatenate the PDFs
cmdstr = sprintf('pdfunite%s %s',tempnames,outname);
[status,~] = system(cmdstr);
% if that failed try one other thing
% i don't know if this will break on other systems
if status ~= 0 && isunix
cmdprefix = 'unset LD_LIBRARY_PATH OSG_LD_LIBRARY_PATH; ';
[status,res] = system([cmdprefix cmdstr]);
end
% if nothing worked, then nothing worked
if status ~= 0
error('I tried, but I got an error: %s',res)
end
This example relies on external tools in a Linux environment. Specifically, I'm using ImageMagick to do the PNG-PDF conversion, and I'm using pdfunite from the Poppler package to do PDF concatenation.
Other than the use of imtile(), this approach should work in practically any MATLAB version. I use it in R2009b, but the usage of imtile() in this example limits us to R2018b or newer.
Obviously, I can't run this on the forum, but the input and output files are attached. The images are numbered and use a single-pixel checkerboard background. While this should make it easy to see that the image resolution has not been adulterated, many PDF viewers will not display a PDF at 100% without some applied interpolation, so that can be misleading. If you want to be sure no information has been lost, you can use IM convert to convert a single-page PDF back to PNG and verify that the round-trip conversion is lossless.

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by