Filter löschen
Filter löschen

How to convert GIF image into JPG without making it gray.

9 Ansichten (letzte 30 Tage)
Fateme Jalali
Fateme Jalali am 29 Okt. 2016
Bearbeitet: DGM am 23 Jun. 2023
hello, I have a GIF image and want to divide it into frames. then I convert each frame into jpg format. GIF image is colored but I obtain gray images from bellow codes after saving each frame while I need colored frames. can any one help me plz?
for k=1:25
[img,map] = imread('D:/myimage.gif','frames',k);
thisImage{k}=img
save(sprintf('E:/myvibe/img%d.mat',k),'img');
save('map.mat','map');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
baseFileNamee = sprintf('imgg%d.png', k); % e.g. "1.png"
fullFileNamee = fullfile(Resultados, baseFileNamee);
imwrite(thisImage{k}, fullFileNamee);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%save(sprintf('E:/vibefolder/img%d.jpg',k),'img');
%save('map.mat','jpg');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure;
imshow(img,map);
end

Antworten (2)

Image Analyst
Image Analyst am 29 Okt. 2016
I think you're going to have to use ind2rgb() since GIF is an indexed format.

DGM
DGM am 23 Jun. 2023
Bearbeitet: DGM am 23 Jun. 2023
All GIF files are indexed color. JPG images are not indexed color; most are RGB. While ind2rgb() could be used to convert to RGB explicitly, it's not normally necessary unless you want certain assurances.
The core problem is the fact that the map is being discarded and a gray colormap is being substituted when writing the image. This will happen regardless of whether the output file format supports indexed color images.
[inpict map] = imread('canoe.tif');
imwrite(inpict,'bad.jpg') % a default gray colormap is used
bad = imread('bad.jpg'); % JPG does not support indexed color, but PNG does
imshow(bad,'border','tight')
To make sure the correct map is used, simply make sure to pass the map to imwrite().
imwrite(inpict,map,'good.jpg') % the correct colormap is used
good = imread('good.jpg'); % JPG does not support indexed color, but PNG does
imshow(good,'border','tight')
If you're writing to a format that supports indexed color, imwrite() will write an indexed color image. If you're writing to a format like JPG that does not support indexed color, imwrite() will handle the conversion internally.
On the other hand, if you're writing to a format like GIF which only supports indexed color, imwrite() will convert RGB inputs to indexed color. If in that scenario you were to pre-convert the image with ind2rgb(), you're unnecessarily converting to RGB and then back to an indexed color image, so you would accomplish nothing other than wasting CPU time and slightly degrading the image quality.
The scenario in which you would need to explicitly convert the image prior to writing is if you're writing to a format which supports both RGB and indexed color (e.g. PNG), and you want to ensure that the image does not stay in indexed color. This may be relevant, since despite the question asking about converting to JPG, the code is actually converting to PNG instead.

Kategorien

Mehr zu Convert Image Type finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by