How to read jpg in format CMYK?

23 views (last 30 days)
Shahar Ades
Shahar Ades on 28 Dec 2021
Edited: DGM on 28 Dec 2021
Hello,
I have many pictures on format jpg with cmyk.
I need to change the size of this picture and need to use imread() , but when I do this I get the error:
Error using imread (line 440)
JPEG images with CMYK colorspace are not currently supported.
I read in mathworks that the function imread() in matlab doesnt work with jpg in cmyk format and that you need to convert the picture to tiff before using imread(), but I cant do this once in a time because I have many pictures.
the code:
cd /Users/shahar/Desktop/ad
imagefiles = dir();
nfiles = length(imagefiles); % Number of files found
for ii=1:nfiles
%currentfilename = imagefiles(ii).name;
sprintf((imagefiles(ii).name));
currentfilename=fullfile('/Users/shahar/Desktop/ad',sprintf(imagefiles(ii).name));
imgarray = imread(currentfilename);
currentimage = imread(currentfilename);
B = imresize(currentimage,[900 900]);
images{ii} = B;
imwrite(B, sprintf(imagefiles(ii).name));
how can I get over this problem? thank you, Shahar

Answers (2)

Image Analyst
Image Analyst on 28 Dec 2021
Then you'll either
  1. have to do the conversion in some other program than MATLAB, or
  2. somehow avoid writing those images with that colorspace in the first place.
By the way, there is lots wrong with your code. I've made it better and more robust:
inputFolder = '/Users/shahar/Desktop/ad';
% Define output folder.
outputFolder = '/Users/shahar/Desktop/ad/Resized';
% Create output folder if necessary.
if ~isfolder(outputFolder)
mkdir(outputFolder);
end
% Get listing of all *.jpg or *.jpeg files in the input folder.
filePattern = fullfile(inputFolder, '*.jp*');
imageFiles = dir(filePattern);
numberOfFiles = length(imageFiles) % Number of files found
% Preallocate a cell array. THIS IS PROBABLY NOT NECESSARY.
images{k} = cell(numberOfFiles, 1); % Probably not necessary
% Loop over all files, resizing and saving them.
for k = 1 : numberOfFiles
% Read in the input image.
thisInputFileName = fullfile(imageFiles(k).folder, imageFiles(k).name);
fprintf('Processing %s.\n', thisInputFileName);
inputImage = imread(thisInputFileName);
% Resize the image.
resizedImage = imresize(inputImage, [900 900]);
% Optional display of images.
subplot(2, 1, 1);
imshow(inputImage)
axis('on', 'image');
subplot(2, 1, 2);
imshow(resizedImage)
axis('on', 'image');
drawnow;
% Store in a cell array. THIS IS PROBABLY NOT NECESSARY.
images{k} = resizedImage; % Probably not necessary
% Create output filename.
thisOutputFileName = fullfile(outputFolder, imageFiles(k).name);
% Write the resized image to the output folder.
fprintf(' Writing %s.\n', thisOutputFileName);
imwrite(resizedImage, thisOutputFileName);
end

DGM
DGM on 28 Dec 2021
Edited: DGM on 28 Dec 2021
If imread() can't read CMYK JPG files (as most things can't), then you'll obviously have to use something else to convert the images. The other option is to write your own decoder.
If you have imagemagick installed, you can externally batch process the files using mogrify, or you can do the process in the MATLAB script using system().
fname = 'cmykimage.jpg'; % this was my test file
system(sprintf('convert %s -colorspace "sRGB" convertedimage.png',fname));
A = imread('convertedimage.png');
imshow(A)
Obviously, you'll have to do whatever is needed to incorporate that into the batch reading routine.
Alternatively, if you want to stay in CMYK instead of converting to RGB, you can convert to a CMYK TIFF file instead. Imread() should be able to read that.
system(sprintf('convert %s -colorspace "CMYK" convertedimage.tif',fname));
Depending on whatever files you have, you may have to specify an appropriate profile option in the call to convert.

Community Treasure Hunt

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

Start Hunting!