Clear Filters
Clear Filters

I have a folder of .tif images that need to be converted to a different format for downstream analysis

12 views (last 30 days)
I have a folder of ~2400 .tif files, where these files were clipped from a georeferenced raster in QGIS. If I use the imread() they show up as an all white image, but if I use mat2gray() I can visualize them. So simply running this code below (found in a previous question answered by image analyst) doesn't help my issue that they stay in matrix style. I believe that is my issue, but not sure. I attached a .zip folder with 5 .tif images for reference. Any suggestions? Thanks!
% Specify the folder where the files live.
myFolder = '/Users/masonlien/Desktop/PhD/Drone/2020/test';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease select another folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir();
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.tif'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
numFiles = length(theFiles);
if numFiles == 0
warningMessage = sprintf('Warning: no TIFF images found in the folder:\n%s', myFolder);
uiwait(warnuser(warningMessage));
return;
end
for k = 1 : numFiles
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf('Now reading %s (#%d of %d).\n', fullFileName, k, numFiles);
% such as reading it in as an image array with imread()
imageArray = imread(mat2gray(fullFileName));
imshow(imageArray); % Display image.
caption = sprintf('%s (#%d of %d)', fullFileName, k, numFiles);
title(caption, 'FontSize', 14, 'Interpreter', 'none');
drawnow; % Force display to update immediately.
% Write it out as a PNG file.
baseFileName = strrep(lower(baseFileName), '.tif', '.jpg');
outputFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now writing %s\n', outputFileName);
imwrite(imageArray, outputFileName);
end
  1 Comment
Mason Lien
Mason Lien on 14 Nov 2020
maybe I'm overthinking this as I found some more information in this post - https://www.mathworks.com/matlabcentral/answers/30784-how-to-convert-a-matrix-to-a-gray-scale-image
ALSO - there is an error I made in the code when I was fiddeling with original.
imageArray = imread(mat2gray(fullFileName)); %wrong
imageArray = imread(fullFileName)); %correct
As you can tell, I'm inexperienced with this type of work.

Sign in to comment.

Accepted Answer

ICR
ICR on 14 Nov 2020
Hi,
I think your input tif files are in single format whereas your output file format which is jpeg doesn't support single format while using imwrite (you can find using help imwrite in the 'fmt' of the file output section)
You can do this instead. either save them in a different file format let us say 'bmp', 'jp2' or 'png'. Else you can do the following thing by converting your input image from single format to unsigned integer 8 (uint8).
% Specify the folder where the files live.
myFolder = '/Users/masonlien/Desktop/PhD/Drone/2020/test';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease select another folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir();
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.tif'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
numFiles = length(theFiles);
if numFiles == 0
warningMessage = sprintf('Warning: no TIFF images found in the folder:\n%s', myFolder);
uiwait(warnuser(warningMessage));
return;
end
for k = 1 : numFiles
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf('Now reading %s (#%d of %d).\n', fullFileName, k, numFiles);
% such as reading it in as an image array with imread()
% Change made here
imageArray = uint8(imread((fullFileName)));
imshow(imageArray); % Display image.
caption = sprintf('%s (#%d of %d)', fullFileName, k, numFiles);
title(caption, 'FontSize', 14, 'Interpreter', 'none');
drawnow; % Force display to update immediately.
% Write it out as a PNG file.
baseFileName = strrep(lower(baseFileName), '.tif', '.jpg');
outputFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now writing %s\n', outputFileName);
imwrite(imageArray, outputFileName);
end
  1 Comment
Mason Lien
Mason Lien on 14 Nov 2020
Thank you, Ragupathy! You're edit did the trick. I removed some of the code that wasn't relevant to what I needed shown below.
% Specify the folder where the files live.
myFolder = '/Users/masonlien/Desktop/PhD/Drone/2020/20200425_Havelock_1033_RGB';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease select another folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir();
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.tif'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
numFiles = length(theFiles);
if numFiles == 0
warningMessage = sprintf('Warning: no TIFF images found in the folder:\n%s', myFolder);
uiwait(warnuser(warningMessage));
return;
end
for k = 1 : numFiles
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf('Now reading %s (#%d of %d).\n', fullFileName, k, numFiles);
imageArray = uint8(imread((fullFileName)));
% Choose what format you want to change it to .tif to .png, .jpg, etc.
baseFileName = strrep(lower(baseFileName), '.tif', '.jpg');
outputFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now writing %s\n', outputFileName);
imwrite(imageArray, outputFileName);
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!