12 Replies Latest reply on Jan 18, 2018 10:59 AM by Kukurykus

    reading an image

    amykwa

      I'm trying to read an image (with the end goal of posting it on a web page), but my script hangs when it's trying to read the image.

       

      var f = File("C:/image.jpg");
      f.encoding = "BINARY";
      f.open("r");
      var buffer = f.read();
      f.close();
      

       

      The image is 200x200 and 3KB. I tried an image that was even smaller, but still no luck. Has anyone else had this problem? Thanks!

        • 1. Re: reading an image
          Tom Ruark Adobe Employee

          Add some more logic and tell us where it fails.

           

          if (! f.exists) {alert("please create a file at " + f); }

          if ( ! f.open("r") ) { alert("could not open file"); }

          if ( buffer.length <= 0 ) { alert("file is empty"); }

          • 2. Re: reading an image
            Paul Riggott Level 6

            This is a common problem when running the code from ExtendScript Toolkit and working with binary files. I find that CS3 ESTK works better than CS4/5 or 6. The code should run okay from Bridge or Photoshop.

            When I am doing any work with binary I have a copy in Notepad and Task Manager open so that I can close ESTK if and when it freezes.

            I know that Mark has the same problems using a Mac so it is something that is common to both OSs

            • 3. Re: reading an image
              Muppet Mark Level 5

              Paul, yep my mac had big issues with reading BINARY files ( locked up the ESTK every time ) eventually the solution I came up with that worked for me ( don't know why this should make a difference? ) was just to wrap it in a function and call it… NO LOCKUP

               

              #target photoshop
              
              readBinaryFile();
              
              function readBinaryFile() {
                  var f, b;
                  f = File( Folder.desktop + '/Test_Image.jpg' );
                  if ( f.exists ) {
                      f.encoding = 'BINARY';
                      f.open( 'r' );
                      b = f.read();
                      f.close(); 
                  };
                  return b;
              };
              
              • 4. Re: reading an image
                Michael L Hale Level 5

                I agree with Tom, it would have been helpful if you said what error message( if any ) you were getting with your script.

                 

                I have also had ESTK hang when working with binary files. Mark's suggestion of wrapping the read/write in a function helps. As long as you remember not to step into that function when stepping through a script. At least on my system when stepping through a script stepping over the read/write function call is ok but ESTK will still freeze at the read line if you step into that function.

                 

                I have also had some 'out of memory' errors when working with binary files. But that may be just my system.

                • 5. Re: reading an image
                  Mikaeru69 Level 3

                  According to the "JavaScript Tools Guide" (CS4):

                   

                  open()

                   

                  The method attempts to detect the encoding of the open file. It reads a few bytes at the current

                  location and tries to detect the Byte Order Mark character 0xFFFE. If found, the current position is

                  advanced behind the detected character and the encoding property is set to one of the strings

                  UCS-2BE, UCS-2LE, UCS4-BE, UCS-4LE, or UTF-8. If the marker character is not found, it checks for

                  zero bytes at the current location and makes an assumption about one of the above formats (except

                  UTF-8). If everything fails, the encoding property is set to the system encoding.

                   

                  This suggests that the encoding may be modified by the open(), depending on the file contents. So, I think it is more correct to set the encoding after the open(), but before the read(). Actually, this is how it is done in all my scripts opening binary files and I don't remember having any hanging problems...

                   

                  Please try this instead:


                  var f = File("C:/image.jpg");
                  f.open("r");
                  f.encoding = "BINARY";
                  var buffer = f.read();
                  f.close();
                  
                  

                   

                  HTH,

                   

                       --Mikaeru

                  • 6. Re: reading an image
                    Michael L Hale Level 5

                    Setting the encoding to binary after opening the file still will hang ESTK if you step through the code. At least for me, the running the code one line at a time that triggers whatever causes ESTK to freeze.

                    • 7. Re: reading an image
                      Muppet Mark Level 5

                      I wasted sooo much time with this issue… I tried almost everything with encoding… before, after, not at all… The ESTK freezes… No crash or log just unresponsive… Can't save or C&P text to other app… Click run and it just stopped… I trashed pref's cleaned out system the lot… The function wrapper solved it for me…

                      • 8. Re: reading an image
                        amykwa Level 1

                        Sorry for the delayed response...I used to get an email when I got responses to my posts, but didn't see any this time.

                         

                        Anyway, the code that I posted above doesn't give me any errors. As I mentioned, the line that the code is hanging on is the line where it's trying to read the file and I have to restart ESTK. The busy icon at the bottom doesn't rotate anymore, I can't copy/paste or do anything else.

                        var buffer = f.read();

                         

                        I tried stepping through the code, not stepping through the code, setting the encoding after opening the file but before the read, wrapping the code in a function, but still no luck.

                        • 9. Re: reading an image
                          Michael L Hale Level 5

                          Try

                           

                          alert(buffer.length);

                           

                          to the end of your script, save the file then run it in Photoshop using the File-Scripts menu. If you get an alert the scirpt can read the file and you will know it's an ESKT issue. If you don't get an alert then you might try Tom's suggestions.

                          • 10. Re: reading an image
                            amykwa Level 1

                            Thanks, Michael. I tried your suggestion and ran the script from Photoshop and I do get an alert, so it does seem like an ESTK issue.

                            • 11. Re: reading an image
                              DBarranca Level 4

                              Hi,

                              I add my experience with ESTK CS6 (3.8.0.12) doing strange things when dealing with binary files. I've run the following script:

                               

                              var infile = File ("/d/test_jpg/icon.png"); 
                              var outfile = File ("/d/test_jpg/icon.txt"); 
                              infile.open ("r"); 
                              infile.encoding = "binary"; 
                              var temp = infile.read(); 
                              infile.close(); 
                              outfile.open ("w");
                              outfile.write (temp.toSource ()); 
                              outfile.close ();
                              

                               

                              which goal is to convert the content of a binary file into a string.

                              The .txt file is created, but just once. If you try to run the script a second time (changing filenames for instance) nothing happens - actually, after the first run, EXTK gets useless (doesn't run, debug, etc) and I've to reboot the app.

                               

                              Davide

                              • 12. Re: reading an image
                                Kukurykus Adobe Community Professional

                                The only thing you need is to put your code into function, so 'freezing' effect of ESTK won't occur:

                                 

                                (function(){
                                     (fle1 = File(str = '/d/test_jpg/icon.png')).open('r');
                                     (fle2 = File(str.slice(0, -3) + 'txt')).open('w')
                                     fle2.write(fle1.read().toSource())
                                     fle1.close(), fle2.close()
                                })()