How to convert five JPG images into PNG using FOR LOOP?

10 Ansichten (letzte 30 Tage)
edusadiq
edusadiq am 3 Nov. 2019
Can someone help me? On converting five jpg images into png images. Please share the complete code with pictures, if you can.
I found the code but it isn't working. Debug this code and tell me where is the problem
f= dir('quetta/*.jpg');
fil = {f.name};
for k =1 :numel(fil)
file=fil{k}
new_file=strrep(file,'.jpg','.png')
im=imread(file)
imwrite(im,new_file)
end
  4 Kommentare
Guillaume
Guillaume am 24 Nov. 2019
Well, in that case, "Debug this code and tell me where is the problem" why haven't you tried to do that yourself?
There is no problem with the code (other than the lack of comments) and it will work as advertised as long as the conditions are right. Unfortunately, since you haven't told us what problem you have with it we can't really help you. "it isn't working" is completely useless if you don't tell us what's happening. In particular, if you get an error give us the full text of the error message.
Image Analyst
Image Analyst am 24 Nov. 2019
I agree. I still don't know what's not working? Maybe displaying the image, if that's what you want. I tried to make a guess at it, and that's why I put my answer below in the Answers sections but you haven't said anything about my answer below. At least if my code below also didn't work, say what it's not doing that you want it to do.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 3 Nov. 2019
Try this, adapted from THE FAQ:
% Specify the folder where the files live.
myFolder = 'C:\Users\yourUserName\Documents\My Pictures';
% 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, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
numFiles = length(theFiles);
if numFiles == 0
warningMessage = sprintf('Warning: no JPG 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);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = 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), '.jpg', '.png');
outputFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now writing %s\n', outputFileName);
imwrite(imageArray, outputFileName);
end

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by