Error using imwrite to convert .tif files to .jpeg

9 ビュー (過去 30 日間)
Teshan Rezel
Teshan Rezel 2021 年 1 月 21 日
コメント済み: Teshan Rezel 2021 年 1 月 22 日
Hi,
I'm using the following code to convert .tif images to .jpeg
folder = 'D:\CAF\TR coke run Jan 21\';
imageFiles = dir([folder '\Images\*.tif']);
numImages = numel(imageFiles);
Settings = xml2struct([folder '\settings.xml']); %Converting XML to structure then accessing the number of the samples from the Settings file
NumOfSamples = str2double(Settings.Children(20).Children.Data);
destinationPath = mkdir([folder '\JPEG Images']);
for i = 1 : 5
currentImage = fullfile(imageFiles(i).folder, imageFiles(i).name);
img = imread(currentImage);
imageName = erase(convertCharsToStrings(imageFiles(i).name),'.tif');
newName = strcat(folder, 'JPEG Images\', imageName, '.jpg');
imwrite(img, newName);
end
but I get the following error message:
Error using writejpg (line 46)
Data with 4 components not supported for JPEG files.
Error in imwrite (line 546)
feval(fmt_s.write, data, map, filename, paramPairs{:});
I know that img is a "1024x1280x4 uint8" variable. Hence, I've tried writing imwrite(img(:,:,i), newName) to get the programme to write them. This works for the first 4 images but stops at the 5th, expectedly.
Is there any way around this?
Thanks in advance
  1 件のコメント
Walter Roberson
Walter Roberson 2021 年 1 月 22 日
The files are either RGBA or CMYK - or possibly RGB+NIR. Or possibly even geotiff.
jpeg does not handle transparency (I would need to check jpeg 2000)
CMYK could be converted to RGB

サインインしてコメントする。

採用された回答

Matt Gaidica
Matt Gaidica 2021 年 1 月 22 日
JPEG files are only going to be NxMx3. Try:
imwrite(img(:,:,1:3), newName);
If that doesn't work, please supply your TIFF's.
  1 件のコメント
Teshan Rezel
Teshan Rezel 2021 年 1 月 22 日
this works, thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by