1 Reply Latest reply on Feb 28, 2012 12:09 PM by JJMack

    Script for saving jpegs successively at varying quality levels

    john_keeg

      Hi.


      I am trying to show degeneration of a jpeg through successive saves.

      I have tried this "by hand" meaning that I opened saved opened saved

      at various qualities to look at different patterns of degeneration.
      When I did this, the jpegs would degenarate at a fast rate to
      begin with, and then "flatten out" at between 100- 200 saves,
      and there was no detectable difference between 200 and 500.

      I not change anything in the files other than the quality level

      when I did this.

       

      Then I tried the scripts from http://forums.adobe.com/thread/777148?tstart=0

      ("Script to show JPEG degenration" thread) as a new starting point.
      But I could not get them to degenerate at all. Probably something I did
      wrong as no one else seemed to have this problem.

      Im quite new to scripting and thought that by applying a for each loop

      with the jepg save options (and a rounded random number as
      that quality level), each file would save at a different, random
      quality level.

       

      This is that script:

       

       

       

       

      // saves jpg multiple times;
      // be advised: this closes the open file without saving and overwrites existing jpgs of the same name without prompting. 
      // 2011, use it at your own risk;
      #target photoshop;
      // set the number of saves;
      var theNumber = 500;
      if (app.documents.length > 0) {
      var thedoc = app.activeDocument;
      // getting the name and location;
      var docName = thedoc.name;
      if (docName.indexOf(".") != -1) {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}
      else {var basename = docName};
      // getting the location, if unsaved save to desktop;
      try {var docPath = thedoc.path
           var thePath = docPath+"/"+basename+".jpg";
           }
      catch (e) {
           var docPath = "~/Desktop";
           var thePath = docPath+"/"+basename+".jpg";     
           };
      // save as jpg if not a jpg yet;
      if (docName.indexOf(".jpg") == -1) {
      // jpg options;
           var randomNumber = Math.round(Math.random()*12)
      for (var r = 0; r < theNumber; r++) {
           var jpegOptions = new JPEGSaveOptions();
           jpegOptions.quality = randomNumber
           jpegOptions.embedColorProfile = true;
           jpegOptions.matte = MatteType.NONE;
           };
      //save jpg as a copy:
           thedoc.saveAs((new File(thePath)),jpegOptions,true);
           };
      // close doc;
      thedoc.close(SaveOptions.DONOTSAVECHANGES);
      //save jpg as a copy:
      for (var m = 0; m < theNumber; m++) {
           File(thePath).copy(File(docPath+"/"+basename+"_"+bufferNumberWithZeros(m + 1, 3)+".jpg"));
           var thePath = docPath+"/"+basename+"_"+bufferNumberWithZeros(m + 1, 3)+".jpg";
           var thedoc = app.open(File(thePath));
      // make change;          
           thedoc.selection.select([[0,0], [0,1], [1,1], [1,0]]);
           thedoc.selection.invert();
      // save;
           thedoc.save();
           thedoc.close(SaveOptions.DONOTSAVECHANGES);
           };
      //that’s it; thanks to xbytor;
      };
      ////// buffer number with zeros //////
      function bufferNumberWithZeros (number, places) {
           var theNumberString = String(number);
           for (var o = 0; o < (places - String(number).length); o++) {
                theNumberString = String("0" + theNumberString)
                };
           return theNumberString
           };
      

       

      But I could not see any difference with this script either, and all of the files looked the same.

      What may be the reason that none of the scripts from the last thread seemed to re encode the
      images?

      I am ultimately hoping to degenerate the image until it becomes completely unrecogniseable.

       

       

      Hope that I am making any sense.

      All help is greatly apprieciated.

       

      Thanks

        • 1. Re: Script for saving jpegs successively at varying quality levels
          JJMack Most Valuable Participant

          If you open a image file lets say a RAW  file with ACR convert to an RGB Image Document 8bit color sRGB color space and save 13 jpeg file using jpeg quality 0 through 12 you will have 13 jpeg files with the same number of pixels with 13 different files sizes with different image quality.  If you save an additional 1000 jpeg image files at jpeg quality 10 the 1000 files will be identical and the same as the other jpeg file save at quality 10.

           

          Jpeg generations degrade you need to open a jpeg file this will decode the encoded image into Photoshop as a document it will not be identical to the document that it was encoded from..  You must do something to that document so Photoshop marks it as changed you must then save a new generation so the changed document is encoded into a jpeg image file and close the the document.  If you do not change something Photoshop will not re-encode the image it will just write out the old encoded image no new generation will be written. You need to keep repeating the process decode change encode close to generate new generations and see the image degrade you can use the same Jpeg quality like 10 the image will degrade. Every time you decode a jpeg the image in the Photoshop will be a little different then the image it was encoded from.  If you decode a jpeg file 1000 time the resulting documents will alwaye be the same.   Its the sequense Encode, Close, Decode (not quite the same image encoded), Encode, Close, Decode (not quite the same a the image encoded) Encode, Close... ... ...