Conversion of .BMP to .jpg images

조회 수: 12 (최근 30일)
Sean
Sean 2015년 2월 16일
댓글: Eric 2015년 2월 17일
Im using the code below to convert .bmp images to .jpeg, Once typed into the command window no errors appear. However there are no new images written into the 'JPEG Data' folder, Can anyone help?
%load images from file
filePathIn = '.\Project\Matlab Folder\BMP Data';
filePathOut = '.\Project\Matlab Folder\JPEG Data';
%Load names of .bmp files in folder filePathIn
d = dir([filePathIn,'*.bmp']);
%for each .bmp file in the directory, convert to jpg
for i = 1:length(d)
%read .bmp file
fname = d(i).name;
%BMP = imread([filePathIn,d(i).name]);
BMP = imread([filePathIn,fname]);
%convert to jpg and rename with .jpg extension
fname = [fname(1:end-4),'.jpg'];
imwrite(BMP,[filePathIn,fname],'jpg');
%reload this file in .jpg format
A = imread([filePathIn,fname]);
rgbImage = repmat(A,[1 1 3]);
%write jpg image to new folder
imwrite(rgbImage,[filePathOut,fname],'JPEG','Quality',100);
end

채택된 답변

Image Analyst
Image Analyst 2015년 2월 16일
Sean, try this code:
% Demo to create JPG copies of BMP files in a different folder.
% By Image Analyst
% inputFolder = fileparts(which('cameraman.tif')) % Determine where demo folder is (works with all versions).
inputFolder = fullfile(pwd, 'Project\Matlab Folder\BMP Data');
filePattern = fullfile(inputFolder, '*.bmp')
% Get list of all BMP files in input folder
bmpFiles = dir(filePattern)
% Create the output folder:
outputFolder = fullfile(pwd, 'Project\Matlab Folder\JPEG Data')
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
figure;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Loop over all bmp files, making a jpg version
% of them in the output folder.
for k = 1 : length(bmpFiles)
% Read in .bmp file
baseFileName = bmpFiles(k).name;
fullFileNameInput = fullfile(inputFolder, baseFileName)
rgbImage = imread(fullFileNameInput);
subplot(1, 2, 1);
imshow(rgbImage);
title('Original image', 'FontSize', 30);
drawnow;
% Prepare output file name
fullFileNameOutput = fullfile(outputFolder, baseFileName);
% Converts to JPEG and gives it the .jpg extension
fullFileNameOutput = strrep(lower(fullFileNameOutput), '.bmp', 'jpg')
imwrite(rgbImage,fullFileNameOutput);
% Reloads it in a JPEG format to see how bad the compression artifacts are.
rgbImage = imread(fullFileNameOutput);
subplot(1, 2, 2);
imshow(rgbImage);
title('Recalled JPG image', 'FontSize', 30);
drawnow;
pause(1);
end
% Open Windows Explorer to the output folder:
winopen(outputFolder);
  댓글 수: 1
Sean
Sean 2015년 2월 16일
great got it working, really good notation aswell, very good to follow. thanks for the help, really appreciate it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

matt dash
matt dash 2015년 2월 16일
Looks like you're missing a path separator between your folder name and file name. You should use the fullfile fucntion to combine paths with filenames instead of trying to do it with [].
  댓글 수: 6
Sean
Sean 2015년 2월 16일
편집: Sean 2015년 2월 16일
I got it working with the code below, although the new images are re written in the same directory. Therefore the problem lay with writing images in a new folder.
%Creates structure of bitmap images inside working directory
d=dir('*.bmp');
for i=1:length(d)
%Reads in .bmp file
fname=d(i).name;
BMP=imread(fname);
%Converts to JPEG and gives it the .jpg extension
fname = [fname(1:end-4),'.jpg'];
imwrite(BMP,fname,'jpg');
%Reloads it in a JPEG format
A = imread(fname);
rgbImage = repmat(A,[1 1 3]);
%Rewrites in into working Directory
imwrite(rgbImage,fname,'JPEG','Quality',100);
end
Eric
Eric 2015년 2월 17일
That's what I was getting at in my comment above. I didn't mean copy the BMP file and simply rename it to JPG, but to copy the first jpeg (the one that wrote correctly) to the location where the write failed.
You could check two things:
1. Does the output directory exist when you try to write to it?
2. Do you have write privileges to the directory?
-Eric

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by