Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Converting JPEG to TIFF

Subject: Converting JPEG to TIFF

From: Damien Gaillard

Date: 5 Mar, 2009 10:36:01

Message: 1 of 17

Hi!

I have created a code to open all images from a folder and save them in a TIFF format. It works for many formats (GIF, BMP, PNG,...), but not for JPEG.

My code is :

file = 'C:\Documents and Settings\...\Images';
extension = '*.jpg'; % needs to be changed for other formats
chemin = fullfile(file,extension);
list = dir(chemin);

for n = 1:numel(list)
    info = imfinfo(fullfile(file,list(n).name),extension(end-2:end));
    Nom = info.Filename;
    img = imread(fullfile(file,list(n).name),extension(end-2:end));
  hold on
    imshow(img);
    img2 = [Nom(1:end-4) '.tif'];
    imwrite(img,img2);
  hold off
end

When I run it I get a message saying "??? Error using ==> imjpginfo at 28
File C:\Documents and Settings\...\Images\image1.jpg is not a JPEG file". This leads to the function isequal, but I don't know what to change to make it work.

Any ideas are welcome. Thanks!

Subject: Converting JPEG to TIFF

From: us

Date: 5 Mar, 2009 10:47:42

Message: 2 of 17

"Damien Gaillard"
> I get a message saying "??? Error using ==> imjpginfo at 28
> File C:\Documents and Settings\...\Images\image1.jpg is not a JPEG file". This leads to the function isequal, but I don't know what to change to make it work...

did you look at your files using another program...
did they show up correctly...

us

Subject: Converting JPEG to TIFF

From: us

Date: 5 Mar, 2009 10:48:01

Message: 3 of 17

"Damien Gaillard"
> I get a message saying "??? Error using ==> imjpginfo at 28
> File C:\Documents and Settings\...\Images\image1.jpg is not a JPEG file". This leads to the function isequal, but I don't know what to change to make it work...

did you look at your files using another program...
did they show up correctly...

us

Subject: Converting JPEG to TIFF

From: us

Date: 5 Mar, 2009 10:48:02

Message: 4 of 17

"Damien Gaillard"
> I get a message saying "??? Error using ==> imjpginfo at 28
> File C:\Documents and Settings\...\Images\image1.jpg is not a JPEG file". This leads to the function isequal, but I don't know what to change to make it work...

did you look at your files using another program...
did they show up correctly...

us

Subject: Converting JPEG to TIFF

From: Damien Gaillard

Date: 5 Mar, 2009 12:16:02

Message: 5 of 17

"us "
> did you look at your files using another program...
> did they show up correctly...

The files aren't corrupted, if this is what you mean. They show up correctly in Matlab (which is the programm I need to use to do this) when I open them one by one with :

img = imread('image1.jpg');
imshow(img);

I don't see the difference with an automatic function that would open them all. Any ideas why it isn't considered as a JPEG file suddenly?

Thanks!

Subject: Converting JPEG to TIFF

From: us

Date: 5 Mar, 2009 12:31:01

Message: 6 of 17

"Damien Gaillard"
> I don't see the difference with an automatic function that would open them all. Any ideas why it isn't considered as a JPEG file suddenly...

that seems weird, indeed...
can you show the result of

     info=imfinfo(fullfile(file,list(n).name),extension(end-2:end));
     disp(info);

for the file that seems to be corrupt...

us

Subject: Converting JPEG to TIFF

From: Damien Gaillard

Date: 5 Mar, 2009 13:10:17

Message: 7 of 17

"us "
> can you show the result of
>
> info=imfinfo(fullfile(file,list(n).name),extension(end-2:end));
> disp(info);

The code doesn't work so far. It's blocked by the errors :
- imjpginfo at 28 --> File C:\Documents and Settings\Me\Bureau\Images\image1.jpg is not a JPEG file
- imfinfo at 174 --> info = feval(fmt_s.info, filename);
- conversion at 17 --> info=imfinfo(fullfile(file,list(n).name),extension(end-2:end));

But if I open the image in the Command Window with img = imfinfo('image1.jpg');, for disp(img) I get

         Filename : 'image1.jpg'
        FileModDate: '23-ao?t-2005 12:11:00'
           FileSize : 10916
             Format : 'jpg'
     FormatVersion : ''
              Width : 400
             Height : 300
           BitDepth : 24
          ColorType : 'truecolor'
  FormatSignature : ''
 NumberOfSamples: 3
      CodingMethod: 'Huffman'
      CodingProcess: 'Progressive'
            Comment: {[1x57 char]}

Hope it helps...

Subject: Converting JPEG to TIFF

From: Damien Gaillard

Date: 5 Mar, 2009 13:12:01

Message: 8 of 17

"us "
> can you show the result of
>
> info=imfinfo(fullfile(file,list(n).name),extension(end-2:end));
> disp(info);

The code doesn't work so far. It's blocked by the errors :
- imjpginfo at 28 --> File C:\Documents and Settings\Me\Bureau\Images\image1.jpg is not a JPEG file
- imfinfo at 174 --> info = feval(fmt_s.info, filename);
- conversion at 17 --> info=imfinfo(fullfile(file,list(n).name),extension(end-2:end));

But if I open the image in the Command Window with img = imfinfo('image1.jpg');, for disp(img) I get

         Filename : 'image1.jpg'
        FileModDate: '23-ao?t-2005 12:11:00'
           FileSize : 10916
             Format : 'jpg'
     FormatVersion : ''
              Width : 400
             Height : 300
           BitDepth : 24
          ColorType : 'truecolor'
  FormatSignature : ''
 NumberOfSamples: 3
      CodingMethod: 'Huffman'
      CodingProcess: 'Progressive'
            Comment: {[1x57 char]}

Hope it helps...

Subject: Converting JPEG to TIFF

From: us

Date: 5 Mar, 2009 13:34:01

Message: 9 of 17

"Damien Gaillard"
> > info=imfinfo(fullfile(file,list(n).name),extension(end-2:end));
> > disp(info);

> But if I open the image in the Command Window with img = imfinfo('image1.jpg');, for disp(img) I get
> Filename : 'image1.jpg'
> FileModDate: '23-ao?t-2005 12:11:00'

ok, the file seems to exist and seems to be in good jpg-shape, to boot...
now - show CSSM the result of these...

     disp(n);
     disp(fullfile(file,list(n).name));
     disp(extension(end-2:end));

us

Subject: Converting JPEG to TIFF

From: Damien Gaillard

Date: 5 Mar, 2009 13:57:01

Message: 10 of 17

"us "
> ok, the file seems to exist and seems to be in good jpg-shape, to boot...
> now - show CSSM the result of these...
>
> disp(n);
> disp(fullfile(file,list(n).name));
> disp(extension(end-2:end));

It gives me
- 1
- C:\Documents and Settings\Me\Bureau\Images\image1.jpg
- jpg

Then if I use imfinfo(fullfile(file,list(n).name),extension(end-2:end)); I get the error from imjpginfo at 28 --> "File ... is not a JPEG file."

And if I use imread(fullfile(file,list(n).name),extension(end-2:end)); I get the error from readjpg at 16 --> "The file may not be a valid JPEG file."

Seems strange, because the extension is '.jpg' ?!? Well, I hope you still have ideas about what's wrong...

Subject: Converting JPEG to TIFF

From: us

Date: 5 Mar, 2009 14:27:01

Message: 11 of 17

"Damien Gaillard"
> > disp(fullfile(file,list(n).name));
> > disp(extension(end-2:end));

> - C:\Documents and Settings\Me\Bureau\Images\image1.jpg
> - jpg

ok, brute force

% are you sure that you're in
% C:\Documents and Settings\Me\Bureau\Images
% if you type
     r=imfinfo('imag1.jpg'); % <- apparently no error
% to confirm, do a
     disp(cd);
% then, what does this yield...
     od=cd('..');
     r=imfinfo('Images\imag1.jpg');
     cd(od);
% last: which ML version do you have...
     disp(version);

let's not give up :-)
us

Subject: Converting JPEG to TIFF

From: Damien Gaillard

Date: 5 Mar, 2009 15:20:17

Message: 12 of 17

"us "
> % are you sure that you're in C:\Documents and Settings\Me\Bureau\Images
>% if you type r=imfinfo('image1.jpg');
            Filename : 'image1.jpg'
        FileModDate : '23-ao?t-2005 12:11:00'
...and all the parameters as before.

> % to confirm, do a disp(cd);
C:\Documents and Settings\Me\Bureau\Images

> % then, what does this yield... od=cd('..'); r=imfinfo('Images\imag1.jpg'); cd(od);
              Filename : 'Images\image1.jpg'
        FileModDate : '23-ao?t-2005 12:11:00'

> % last: which ML version do you have... disp(version);
7.7.0.471 (R2008b)

OK, you won't believe it, but it works! I think the code is still the same (cf below). All I did was to move the images to another folder and then copy them back in the folder "Images". These are the same pictures, still in JPEG, but they are accepted as JPEG files. I don't really know what changed...

Anyway, THANKS a lot for your time and your help!!!

Damien

My Code :

file = 'C:\Documents and Settings\Me\Bureau\Images';
extension = '*.jpg';
chemin = fullfile(file,extension);
list = dir(chemin);

for n = 1:numel(list)
     
    info = imfinfo(fullfile(file,list(n).name),extension(end-2:end));
    Nom = info.Filename;
    img = imread(fullfile(file,list(n).name),extension(end-2:end));
     
hold on
    figure;
    imshow(img);
    img2 = [Nom(1:end-4) '.tif'];
    imwrite(img,img2);
hold off

end

Subject: Converting JPEG to TIFF

From: us

Date: 5 Mar, 2009 16:53:01

Message: 13 of 17

"Damien Gaillard"
> OK, you won't believe it, but it works! I think the code is still the same (cf below). All I did was to move the images to another folder and then copy them back in the folder "Images". These are the same pictures, still in JPEG, but they are accepted as JPEG files. I don't really know what changed...

hmm... now THAT is REALLY weird...

us

Subject: Converting JPEG to TIFF

From: Walter Roberson

Date: 5 Mar, 2009 18:59:28

Message: 14 of 17

us wrote:
> "Damien Gaillard"
>> OK, you won't believe it, but it works! I think the code is still the same (cf below). All
>> I did was to move the images to another folder and then copy them back in the folder "Images".
>> These are the same pictures, still in JPEG, but they are accepted as JPEG files.

Windows XP by chance? And had you done a bunch of file copying just before, or had you
run a disk defragmenter? If so, then you might have run out of Volume Shadow Copy buffers,
in which case Windows might have re-used buffers that were still in use.

Just grasping at straws here, based upon a technical note I read when I updated O&O Defrag
a couple of days ago.

Subject: Converting JPEG to TIFF

From: ImageAnalyst

Date: 5 Mar, 2009 20:24:14

Message: 15 of 17

Damien:
I've encountered several times where the extension is wrong and you
can't tell by opening the file in another program. For example I
think that I was seeing images in thumbnail form in Windows Explorer
and in the Windows Picture and Fax viewer but couldn't open them in
Photoshop. Apparently the Picture and Fax Viewer program looks
internal to the file to determine the true file type whereas other
programs rely on the extension. When I get conflicting information
like that (and this happened just yesterday) is to open the file in
wordpad. You'll see a bunch of gibberish but look at the first 5
characters or so. A BMP file will start with BM, a JPEG file will
start with JFIF, a PNG file will start with PNG. In my case
yesterday, I found 4 files with extension jpg but only two could be
opened (tried 4 different programs). When I opened with wordpad, it
turns out that those 2 bad images were actually PNG images (as I could
see from the first few characters in the file) but they had an
incorrect extension.

Damien, please open up your images in wordpad and tell me the first 5
or 10 characters that you see.
Regards,
ImageAnalyst

Subject: Converting JPEG to TIFF

From: Walter Roberson

Date: 5 Mar, 2009 20:44:17

Message: 16 of 17

ImageAnalyst wrote:
> Damien:
> I've encountered several times where the extension is wrong and you
> can't tell by opening the file in another program.

The windows program irfanview is good about detecting the real
file format and offering to rename the file extension to match
the content. (Unless, that is, you have deliberately turned off
the appropriate preference.)

Subject: Converting JPEG to TIFF

From: Damien Gaillard

Date: 6 Mar, 2009 09:04:02

Message: 17 of 17

Walter,

Yes, I ran a disk fragmenter three days ago. The code wasn't working before that and it still wasn't working yesterday morning.


ImageAnalyst,

I could open the JPEG images with Photoshop as well as with PictureViewer without problems. For the two images used to test the code, I have :
- image 1 : ...JFIF...gd-jpeg v1.0 (using IJG JPEG v62), quality = 70
- image 2 : ...JFIF...gd-jpeg v1.0 (using IJG JPEG v62), quality = 85
It seems like these images were real JPEG files after all...

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us