Borrar filtros
Borrar filtros

Unpredictable outcome placing images in a single cell table.

1 visualización (últimos 30 días)
Matt O'Brien
Matt O'Brien el 19 de Nov. de 2022
Movida: Dyuman Joshi el 12 de En. de 2024
I am trying to create a Pdf, landscape size, size 9 inches wide by 5 inches high, for viewing on an ipad Pro.
On this page I wish to place an image within a cell size of say 8 inches by 4 inches.
I am leaving some space for adding Title Caption, Location information later. My problem is correct scaling of the image within the cell.
The image needs to fit inside the constraints of the cell size.... no cropping.
The images are jpgs, designed to fit the max no of pixels displayable, so they have appropriate number of pixels for my device.
I copied the example in the documentation (say a 2inch x 2 inch) cell. This works.
But ... when I try different images which come close to or exceed the size of the cell... the image is displaying full scale instead of staying (to scale) within the defined size of the cell.
I will post an example in 30 mins or so, when I get back to my workstation.
I am hoping there is an obvious answer
  2 comentarios
Matt O'Brien
Matt O'Brien el 21 de Nov. de 2022
I have made some progress, but wasted a full day. The Report Generator module is an exercise in frustration. I now have my app configured, so the image stays on the page, but the bottom limit of the table changes position, depending on the characteristics of the pixels. I suspect cell and table inner margins and whether an image is landscape or portrait is impacting behaviour.
I have also spent 5 hours trying to transfer a simple array into a table and continue to get an index error.
I am really disappointed I cannot easily place images and metadata where I want on a page. Instead I am at the mercy of sequentially placing objects on the page from top to bottom, which constantly appear to overstep the size parameters I have specified.
Also, I cannot specify a path for images. I must either copy the images to the working directory or place all my images in a folder and dynamically add this folder to the path. This is ok for testing, but not ok when I need to process images listed in a database, which may be spread across 100 different folders.
Finally, I can not understand why Fonts are not available, when they are clearly installed and working in every app except Matlab and are installed in the correct Windows (not user) folder.
I cannot spend any more time on this for a few more days. I will try and post a simplified example of what I am trying to achieve ( 1 image and 6 pieces of related metadata placed under the image) and see if anyone can post the code to achieve this basic layout.
Matt O'Brien
Matt O'Brien el 21 de Nov. de 2022
I have made progress.
Still difficult to be precise placing images .... need to work more on this myself (as I wish to build in logic for handling landscape and portrait orientated images).
Main Issues Remaing.
  • Using images with full file path is still (a major) issue.
  • Installed fonts not available (and have checked they are in the admin (not user) folder).
  • Text words are splitting across table cell line breaks.

Iniciar sesión para comentar.

Respuestas (1)

Ayush Modi
Ayush Modi el 12 de En. de 2024
Hi Matt,
I understand you are trying to put an image inside a single cell of custom size but if the image is larger than the cell, the image is displaying full scale instead of staying (to scale) within the defined size of the cell. You can achieve this by determining the Scaling Factor based on the cell size and the original image size while maintaining the aspect ratio.
Here is an example to demonstrate how you can accomplish this:
% Define the metadata
metadata = {
'Title: Example Image';...
'Resolution: 1920x1080 pixels';
'Comments: This is an example of image metadata.'
};
% Define page size and cell size in inches
pageWidth = 9;
pageHeight = 5;
cellWidth = 4;
cellHeight = 4;
img = imread('C:\Users\user\foldername\filename.jpg');
[imgHeight, imgWidth, ~] = size(img);
% Calculate scaling factor
scaleWidth = cellWidth / imgWidth;
scaleHeight = cellHeight / imgHeight;
scaleFactor = min(scaleWidth, scaleHeight); % Choose the smaller scale factor
% Calculate the new size of the image
newImgWidth = scaleFactor * imgWidth;
newImgHeight = scaleFactor * imgHeight;
% Create a figure with the specified page size in landscape orientation
fig = figure('Units', 'inches', 'Position', [0, 0, pageWidth, pageHeight]);
% Create axes for the image
% Adjust the axes position based on your requirements for title and caption
axes('Units', 'inches', 'Position', [(pageWidth-newImgWidth)/2, (pageHeight-newImgHeight)/2, newImgWidth, newImgHeight]);
subplot('Position', [0.1, 0.3, 0.8, 0.6]); % Adjust as needed
imshow(img);
axis off;
% Display metadata below the image
for i = 1:numel(metadata)
% Calculate normalized position for each piece of metadata
metadataPos = [0.1, 0.3 - 0.05*i, 0.8, 0.05];
% Ensure the position values are between 0 and 1
metadataPos(metadataPos < 0) = 0;
metadataPos(metadataPos > 1) = 1;
annotation(fig, 'textbox', metadataPos, 'String', metadata{i}, 'EdgeColor', 'none', 'FitBoxToText', 'on');
end
% Save the figure as a PDF
set(fig, 'PaperOrientation', 'landscape', 'PaperUnits', 'inches', 'PaperSize', [9, 5]);
saveas(fig, 'output_filename.pdf');
close(fig);
Please refer to the following MathWorks documentation for more information on the "gcf" :
Note - The above code uses full path directory to read the image and the Text words are intact and within a cell.
Issue - 2: Installed fonts not available (and have checked they are in the admin (not user) folder).
You can check the fonts recognized by the "Matlab" using the below code:
availableFonts = listfonts;
disp(availableFonts);
Note - If the required font is not listed, you may need to troubleshoot why MATLAB isn't recognizing it.
I hope this helps!
  1 comentario
Matt O'Brien
Matt O'Brien el 12 de En. de 2024
Movida: Dyuman Joshi el 12 de En. de 2024
I really appreciate your response. I will revisit that project and check how I left things.
I use MatLab to assist the management, analysis and presentation of large volumes of digital images and their related metadata.
While there is a lot to like, I have largely given up on MatLab for a few reasons, but the main reason is that I cannot trust MatLab to return to me valid status of files on folders when the images are stored on SD Cards. There are so many scenarios where it is impossible to determine the status of hidden or system files, which need to be reliably excluded from the results of the Dir command. I have put in a request to MatLab to resolve this but no indications this most fundimental gap will be addressed.
BTW, for anyone else with this image placement challenge I used theMatLab report Generator module to help me generate multipage pdf's with precise placement of image and related metadata on a page, which helps me automate generation of books which I send to the Apple Books app.
Again, I am deeply grateful for this response.

Iniciar sesión para comentar.

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by