Page 1 of 2

reading free text in jpg-file

Posted: Wed Nov 30, 2011 11:54 am
by wimapon
I can not find a simple pb prog to read the free text in the header of a jpg-file.
i am not good enough in pb do make it myself.

does somebody have a working source?

thanks

Wim

Re: reading free text in jpg-file

Posted: Wed Nov 30, 2011 12:16 pm
by em_uk

Re: reading free text in jpg-file

Posted: Wed Nov 30, 2011 1:18 pm
by wimapon
thanks em_uk,
but this is not enough for me... i do not understand it.....
with this info i can not write a short program myself.

Wim

Re: reading free text in jpg-file

Posted: Wed Nov 30, 2011 4:28 pm
by em_uk
Download the example program from that site, it will allow you to see the information.

You need to at least have a go.

Re: reading free text in jpg-file

Posted: Wed Nov 30, 2011 5:59 pm
by c4s
What do you mean? Exif or the JPG Comment?

You can find a code for the JPG Comment here: http://www.purebasic.fr/english/viewtopic.php?p=44684 and Exif here: Search

Re: reading free text in jpg-file

Posted: Wed Nov 30, 2011 7:16 pm
by infratec

Re: reading free text in jpg-file

Posted: Wed Nov 30, 2011 8:37 pm
by infratec
Hi Wim,

if you mean only the 'real' comment, try this:

Code: Select all

NewList JPG_CommentList.s()

Procedure JPG_Comment(Filename$, List Comment.s())
  
  Dim Buffer.a(1)
  
  Result = #False
  
  File = ReadFile(#PB_Any, Filename$)
  If File
    FileSize = Lof(File)
    ;Debug "Filesize: " + Str(FileSize)
    EOI = #False
    Length.l = 0
    FilePos.q = 0
    Repeat
      If ReadData(File, @Buffer(0), 2) = 2
        FilePos + 2
        If Buffer(0) = $FF
          ;Debug "Filepos: " + Hex(FilePos)
          ;Debug Hex(Buffer(1))
          Select Buffer(1)
            Case $D8  ; SOI
            Case $D9  ; EOI
              EOI = #True
            Case $DA  ; SOS
              EOI = #True
            Case $FE  ; COM
              If ReadData(File, @Buffer(0), 2) = 2
                Length = Buffer(0) << 8 | Buffer(1)
                Comment$ = ReadString(File)
                AddElement(Comment())
                Comment() = Comment$
                FilePos + Len(Comment$)
                Result = #True
              EndIf
            Default
              If ReadData(File, @Buffer(0), 2) = 2
                Length = Buffer(0) << 8 | Buffer(1)
                ;Debug "Length: " + Hex(Length)
                FilePos + Length
                ;Debug "Filepos BKK: " + Hex(FilePos)
                FileSeek(File, FilePos)
              EndIf
          EndSelect
        EndIf
      EndIf
      If Eof(File)
        ;Debug "EOF"
        EOI = #True
      EndIf
    Until EOI
    CloseFile(File)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure


Filename$ = OpenFileRequester("Choose a JPG file", "", "JPG (*.jpg)|*.jpg", 0)
If Len(Filename$) > 0
  If JPG_Comment(Filename$, JPG_CommentList())
    Text$ = "Comments:" + #LF$
    ForEach JPG_CommentList()
      Text$ + JPG_CommentList() + #LF$
    Next
  Else
    Text$ = "No comment found"
  EndIf
  MessageRequester("Comments", Text$)
EndIf
It works with a comment which I added with IrfanView.

In theorie it is possible to add more than one comment.
So I used a list.
If you know that you have only one comment, you can return the comment as result
and set EOI to #True. That speeds up the procedure.

Bernd

Re: reading free text in jpg-file

Posted: Sat Dec 03, 2011 6:46 pm
by wimapon
Hi folks, i am very greatfull.. this is exact what i needed.
Now i can write a copyright in my photograps and read it back.

For infratec: all my pure basic programs for my radio-telescope are working very nice.
the guys of LOFAR saw my results and asked me to give a presentation
at there office in Dwingeloo.... So my star is rising.... ( forget that please)
With pure basic the 2D-FFT give nice maps of the radio-sky!
See http://home.kpn.nl/apon001/huis.htm item: phasing interferometry results

SO THIS FORUM IS FANTASTIC ! thanks

Re: reading free text in jpg-file

Posted: Sat Dec 03, 2011 6:53 pm
by infratec
Hi Wim,

you are welcome :!:

I know your site already and I visit it from time to time.
(To see your progress :mrgreen: )

It is also easy to put this type of comment inside a JPG file in PB.
So you can write a program which runs over a complete directory and do it by it self.

Bernd

Re: reading free text in jpg-file

Posted: Sat Dec 03, 2011 7:05 pm
by wimapon
Yes Bernd, i think i can manage that!
Wim

Re: reading free text in jpg-file

Posted: Sat Dec 03, 2011 8:04 pm
by infratec
Hi Wim,

work is already done:
http://www.purebasic.fr/english/viewtop ... 12&t=48426

:mrgreen:

Bernd

Re: reading free text in jpg-file

Posted: Sat Dec 03, 2011 11:41 pm
by wimapon
Hi Bernd,
but it is not exactly what i like to have.
your program from above does not write a comment in files when there is a comment allready.
If i change the program i can write a comment after the old comment.
I prefer to overwrite the old comment with the new comment, and i can not find
how to do that....
Your first program does overwrite the old comment.
I can not merge that program in this new one...

Wim

Re: reading free text in jpg-file

Posted: Sun Dec 04, 2011 1:00 pm
by infratec
Hi Wim,

yes it worked, but... only accidently.
Than there was more than one comment in the file.

I extended all the comment functions and rewrote the example.
Please look here again:
http://www.purebasic.fr/english/viewtop ... 12&t=48426

Bernd

Re: reading free text in jpg-file

Posted: Sun Dec 04, 2011 9:24 pm
by wimapon
Okee Bernd,
this is great. It works.
I can use this very nicely!!
thank you very much!!

Wim

Re: reading free text in jpg-file

Posted: Sun Dec 04, 2011 10:14 pm
by infratec
Hi,

I extended the JPGCommentReader.
Now it can export the result to a csv file.
So you can archive the stuff in a spreadsheet.

Bernd