Question

I have a folder full of .png files and I want to convert them to .jpg. I wrote this code to process all of them and to convert them to jpg

imgPath = 'C:\Users\SET1\writer_2\';
imgType = '*.png'; % change based on image type
images  = dir([imgPath imgType]);

newPath = 'C:\Users\Desktop\SET1\';

for idx = 1:length(images)
    Seq{idx} = imread([imgPath images(idx).name]);

    imwrite(Seq{idx}, strcat(newPath,images(idx).name, '.jpg'));
end

But I noticed that when it reads the image in it somehow changes it. For example here is the original image

enter image description here

Here is the image read in, and this is what is saved as the output of imwrite

enter image description here

You can basically see the change. First of it inverted the image, then it converted the strokes of the pen to a collection of dots, as in what happens when one performs im2bw

I do not understand as to why this is happening and how to fix it.

Was it helpful?

Solution

The problem is that the image is color mapped. You need to read in the files like so:

[I,map]=imread(yourPngFile);
imshow(I,map);
imwrite(I,map,outfilename);

If you want you can convert it and save it in your cell array as an RGB image:

Seq{idx} = ind2rgb(I,map);

Source:

How do I load and show a PNG image in MATLAB?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top