Trying to convert some grayscale images to RGB (1,1,1).. I have a folder of about 1500 images that I need batch converted using the code below (which works well with individual images)
Interestingly enough,
imwrite(repmat(imread(files(1).name), [1 1 3]),files(1).name)
imwrite(repmat(imread(files(2).name), [1 1 3]),files(2).name)
imwrite(repmat(imread(files(3).name), [1 1 3]),files(3).name)
...(and so forth)
works just fine
files = dir('*.jpeg')
for I=1:length(files)
imwrite(repmat(imread(files(i).name), [1 1 3]),files(i).name)
display(i)
end
Error using writejpg (line 46) Data with 9 components not supported for JPEG files.
Error in imwrite (line 485) feval(fmt_s.write, data, map, filename, paramPairs{:});
You need to do two things:
Use the correct variable name for looping, i.e. i
or I
but not a mix! Note that i
has a built in definition as the imaginary constant, so you're better of using I
, or something different entirely.
You show a warning for JPEGs with 9 elements not being supported when trying to write the file. This suggests you've blindly used repmat
to triplicate an image which is already RBG.
We can address both of these like so:
files = dir('*.jpeg')
for k = 1:length(files)
img = imread( files(k).name ); % Load the image first
% Convert greyscale to RBG if not already RGB
% If it's already RBG, we don't even need to overwrite the image
if size(img,3) == 1
imwrite(repmat(img, [1 1 3]), files(k).name);
end
% Display progress
display(k)
end