"Optimize to file size" script

Hi everyone,
I am currently writing an applescript to circumvent Photoshop's lack of scripting support for the "Optimize to File Size" option in the Save for Web command, but I'm having trouble with targeting the file sizes correctly.
Here is the code I'm using (forgive me, I know its not so graceful... I'm still very new). Basically it exports the image at 100% quality, checks the resulting file size against the desired file size, and if it's not equal to or less than that value, it deletes, reduces the quality level, and re-exports until it meets that criteria:
on optimizeFile(currentDoc, optimizeSize, exportPath, fileExtension, trimmedFilename, qualityIncrement)
set qualityLevel to 100
set optimizedFile to ((exportPath & trimmedFilename & fileExtension) as string)
tell application "Adobe Photoshop CS3"
tell currentDoc
export in file optimizedFile as save for web with options {class:save for web export options, quality:qualityLevel, web format:JPEG}
end tell
end tell
tell application "Finder"
set truncatedFile to files of alias exportPath whose name contains "#0"
repeat with thisFile in truncatedFile
set name of thisFile to (trimmedFilename & fileExtension)
end repeat
set fileSize to physical size of optimizedFile
end tell
repeat until fileSize is less than or equal to optimizeSize
tell application "Finder"
delete file optimizedFile
set qualityLevel to (qualityLevel - qualityIncrement)
end tell
tell application "Adobe Photoshop CS3"
tell currentDoc
export in file optimizedFile as save for web with options {class:save for web export options, quality:qualityLevel, web format:JPEG}
end tell
end tell
tell application "Finder"
set truncatedFile to files of alias exportPath whose name contains "#0"
repeat with thisFile in truncatedFile
set name of thisFile to (trimmedFilename & fileExtension)
end repeat
set fileSize to physical size of optimizedFile
end tell
end repeat
end optimizeFile
The problem that I am having is that when this script exports bigger images at bigger file sizes (even in the 30 to 40k range) it works perfectly. However, whenever I get to thumbnail sized images that i need to export at say 3k or 4k, it runs into an infinite cycle of deleting and re-exporting. I have tried using the "data size" property instead of "physical size" but that seems to ignore the loops altogether and export files at much larger sizes.
Can anyone out there help? I'd love to nail down a script that can do this function well, as I think there is a huge need for this functionality amongst people who regularly batch process images, i'm just afraid that my version may not be the most efficient.

You're right - I really could do with this functionality!
Sorry I can't help on the scripting side, but maybe someone who can will notice this post now.

Similar Messages

  • Script to "Optimize to file size" (automatically) in "Save for Web"?

    Hello, all,
    I'm a novice when it comes to scripts in Photoshop, but I'm really coming to the conclusion that this is the only way to automate the actions I need to do, whithout having to do it one-by-one.
    Is it possible to write a Script to automatically choose the option (and settings) to "Optimize to file size" in the "Save for Web" dialog?
    Is it also possible to save the file in the document's original folder, instead of a single specific folder, using "Save for Web"?
    I have a really big number of different files (some .JPEG, some .PNG, some .TIFF) and I need to generate a single "reference.jpg" in each one of those original folders. It's quite easy to use whatever method to separate all those files in some lists and later open them in Photoshop in small subgroups of files.
    I only have a restriction of size (60Kb) for those "reference.jpg" files, and I would like to save all of them, in each document original folder, with the best possible quality within that size constraint.
    Here's my problem:
    I can easily do that with a very simple action, but, that specific way, I have no control whatsoever over the resulting filesize of those "reference.jpg" files generated. Moreover, I also find the "save for web" command create a more "slim" file, allowing for better quality within my size constraint.

    c.pfaffenbichler wrote:
    "I can’t make that out in the ScriptingListener code, but maybe someone else knows better or has a Script for the task already.
    I seem to remember a thread where it was discussed to save a copy, check its size and overwrite it with different settings if it was too big … that should be possible, but may run longer."
    Many, many thanks for your reply, c.pfaffenbichler!    I spend the whole day (and night) yesterday learning (or rather trying to learn) about javascript, OOP, and how it should be applied in Photoshop's Scripts.
    I was able to modify some snippets of code I found on the web to suit my needs (temporarily):
    //SaveforWebSP.jsx
    //Save a fixed width image with a size constraint in its original folder
    var docRef = activeDocument;
    var outputFolder = docRef.path;
    NamesaveRef = new File( outputFolder + "/reference.jpg" );
    var NewfileRef = new File( NamesaveRef )
    // quality/size constraints
    var w = 300; // new file width
    var MaxSz =  61440; // max. 60Kb
    var Qlt = 100; // initial quality 100
    var x = 1; // decreasing step
    // resize the image to the right width
    docRef.resizeImage(UnitValue(w,"px"),null,100,ResampleMethod.BICUBICSHARPER);
    // Perform the first SaveForWeb Operation
    ExpWeb(NewfileRef, Qlt);
    // Keep trying to save the file with max. Qlt, but under MaxSz   
    while (NewfileRef.length > MaxSz)
          Qlt = Qlt - x;
          NewfileRef = new File( NewfileRef );  
          NewfileRef.remove();
          ExpWeb(NewfileRef, Qlt);  // Perform a new SaveForWeb Operation, with slightly lower Qlt
             if (Qlt <= 50) {
               alert("The file can't be saved with the desired size AND quality.");
               break  // break the loop whenever the quality is as low as 50 (this shouldn't need to happen)
    var FileSz = NewfileRef.length/1024;
    FileSz = Math.round(FileSz);
    // close the original file without saving
    activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    // SaveForWeb Export, with the desired constraints and parameters
    function ExpWeb(FileNm, Qlt)
          var options = new ExportOptionsSaveForWeb();
          options.quality = Qlt;   // Start with highest quality (biggest file).
          options.format = SaveDocumentType.JPEG;   // Save Format for the file
          docRef.exportDocument(File(FileNm), ExportType.SAVEFORWEB, options);
    This works, but it takes a long time and keep the HD disk "going crazy" during the time it is processing the images.
    This is too bad, because the "Optimize to file size" dialog in the "Save for Web" would have been so much faster, efficient and much less "disk-intensive"...

  • Action to "Optimize to File Size"?

    I often need to resize multiple sizes of JPEGs to be exactly 40k for websites. My current process is to:
    Open a file in Photoshop
    "Save for Web"
    Click on the menu drop down on the Optimize window and select "Optimize to File Size"
    Enter 40k and click OK
    Save.
    This is a tedious process when I have to do it for dozens of files a week. I've tried creating an Action to speed up the process, but it doesn't recognize the "Optimize to File Size" setting.
    Is there a script or other solution to automate this process?
    (I'm on a Mac with Photoshop CS6.)

    Perhaps someone has create a script to do something like that.  I'm quite sure one could be programmed,  However a script like you want is not supplied by Adobe with Photoshop.
    I also do not think it is possible to save all image as jpegs that are exactly 40K in size.  Less than or near 40K yes exactly 40K no.

  • [Photoshop CS5] Optimize to File Size working not correctly.

    ******BUG******
    Concise problem statement: Optimize to File Size working not correctly.
    Steps to reproduce bug:
    1. Open any photo.
    2. Draw a slice
    3. Save For Web, choose Optimize to File Size, type 10KB (Current Slice).
    4. Save "selected slices" only
    Results: File result larger than expect (over 10KB)
    Expected results: <=10KB filesize.
    I uninstalled whole Adobe CS5 Master Collection (Trial) & tried to reproduce the bug in Photoshop CS4, but it was not happened, it's ok on CS4.
    Finally, Windows 7 is not reason, it's a bug of Photoshop CS5. Please fix, I don't want to buy a bugged software.
    I really need the feature "Optimize to File Size" in my works.
    P.S: I tested on 4 PCs, and the results is the same.

    In the SFW dialog change the Metadata dropdown to None

  • Automating "Optimize to File Size"

    I have been using PS7. An ongoing frustration has been that if I record an action using Save for Web and do the Optimize to File Size dialog, the action does not include the file size optimization. So for each image I have to have the action stop at the Save for Web step and do the file size optimization manually. I've searched the web and apparently there is no workaround for this.
    Skip four versions ahead - I just downloaded a trial version of CS4 Extended. One of the first things I tried was to record an action that included the Optimize to File Size dialog. When I ran the action, lo and behold it still does not appear to remember the Optimize to File Size step. I checked that the images were capable of being compressed to the desired file size by manually doing Optimize to File Size.
    Please tell me that I am just doing something wrong and that it should be possible for the action to remember the Optimize to File Size step.
    Thanks.

    Okay, I've done some searching into the "image processor" and found it fairly quickly. Unfortunately, that only lets you specify the maximum pixel dimensions of the image, not the file size. Many web sites have file size limitations for posted photos, e.g., 100 kB. The Optimize to File Size lets you meet the file size criterion as closely as possible without exceeding it.

  • Optimize/Reduce File Size

    I'm working with a PDF that I did not create but have been asked to try to reduce the size of. It was created in InDesign CS4 (don't know if it was Mac or Windows). It is a large book (600 pages) but entirely text, other than one graphic on the cover. A similar book that I had worked on produced a PDF of about 2.8 MB. The one in question is over 6MB. Using the Reduce File Size command and using the Optimizer both increase the file size.
    Auditing the space usage shows that 86% of the space is "Structure Info." I believe the InDesign file was done by someone not very versed in the ways of InDesign, and that there were many places where text was threaded in strange ways, with lots of overlapping text boxes. I don't know if that would have anything to do with the "structure info" in the PDF. "Structure Info" isn't even listed in the audit results of the normal PDFs I'm comparing this one to. The InDesign file was actually several InDesign files, and I don't know how the PDFs of the individual files were combined, but that's another piece of information.
    Given the above facts and the little I know about how the PDF was really exported and assembled, does anyone have any ideas about actually making the file smaller? Based on the other PDFs we've done of simlilar books, it should be somewhere around 2.5 to 3MB in size, not the 6 to 8 it currently is.

    Auditing the space usage shows that 86% of the space is "Structure Info." 
    It would appear that the PDF is a Tagged output PDF.
    As discussed in ISO 32000-1, Tagged PDF facilitates content use/consumption by:
    --| users of AT for Accessibility
    --| users of mobile devices (reflow)
    --| content export with format/layout information
    --| simple copy/paste
    If the PDF is, in fact, a well-formed Tagged PDF then the author actually has commendable competency with InDesign.
    Oh yes, using Acrobat Pro you can use either Optimizer or a Preflight to remove all structure. This would help reduce the PDF's size.
    Be well...
    Message was edited by: CtDave

  • JPEG exports - "optimize to file size"

    just saw this tucked away in somebody's lengthy post, but i think it merits its own thread.
    it would be very useful, and isnt very difficult, to implement a jpeg export option for auto-setting the quality slider based on "desired file size".
    for instance, my web products need jpegs w/ a max of 800k. in PS, i have to dork around w/ the preview sliders to get this. in LR, there isnt a way. preferred for both would be UI for inputting the desired size and let the cpu do its job.
    this would rock.
    thanks
    matt

    Yes. Generally speaking, would be nice to know the exported file size in terms of MB / KB, in addition to pixel dimensions which are already there.
    Gilles.

  • Save for Web with Optimize File Size for Batch Process

    I have a client who needs to be able to run a batch process on jpg images ranging in size from 600KB to 2.5MB optimizing their file size as close to 200KB as possible. The feature is included in Photoshop's save for web optimize dialog but it doesn't work for batch processes. If you record an action with the optimize to file size option set it processes all the images in the batch based on the quality settings required to resize the image you used during recording, meaning if the image you recorded was 1.5MB the quality setting might be 70 percent to hit the 200KB mark but that wouldn't be the correct quality setting for a 600KB file nor a 2.5MB file those would either be too small or too large.
    I've done quite a bit of searching both online and in the photoshop scripting forums but I haven't been able to find anything that would point me in the right direction.
    My client's paying for this work so if there's someone who knows how to script this who'd rather be paid than just do some pointing we can arrange for that quite easily.
    Thanks in advance for your assistance and suggestions.
    In case it's of importance we're using Photoshop CS4.

    If you don't mind spending a few bucks on a shareware prog, Jpegsizer has the option to save to a filesize.
    You can find it here.
    http://www.tangotools.com/jpegsizer/register.htm
    There is a free trial version, but you can't save files.

  • Delete Pages, file size, PDF Optimizer Clean Up

    Using Acrobat 9 Pro (for Windows, if that makes any difference).
    I have a 148 page PDF of a magazine, about 58 MB in size.
    I Deleted 40 pages of ads, leaving just the useful content.
    The file size is still about 58 MB.
    When I run Advanced -> PDF Optimizer,
    the file size goes down to about 47 MB.
    The Optimizer settings don't seem to matter.
    Even with just "Clean Up" checked, all its options UNchecked,
    and Compression set to "Leave Compression Unchanged"
    the file size is still reduced.
    I have tried several other PDF editors and tools,
    and none of them reduce the file size after deleting the 40 pages,
    even using their "optimize" or "compress" functions.
    Can someone explain why deleting pages does not reduce the file size,
    and what the Optimizer is doing (that the other tools don't do)
    to reduce the file size ?
    Thanks,
    -- Glenn

    this may be helpful
    http://forums.adobe.com/message/4568414#4568414
    in Adobe 9 Pro Windows, I found the best was:
    Document -> Optimize Scanned PDF (defaults)... tho you could play w the slider bar for even smaller.

  • We are looking for a software which converts SWF to MP4, we found many software which does the job but issue is file size is not optimize.  We found Adobe captivate 6 which gives optimum quality with good output but thing is we do not have batch process i

    Hi
    We are looking for a software which converts SWF to MP4, we found many software which does the job but issue is file size is not optimize.
      We found Adobe captivate 6 which gives optimum quality with good output but thing is we do not have batch process in captivate 6. so we request you to create a plugin or give a best option which have batch render quality and optimize in file size,

    Captivate is e-learning development software not a dedicated video convertor, you are better off trying something like Adobe Media Encoder or Super.

  • OCR and Reducing file size

    I have a large document (a book) that I am trying to scan. I will be scanning it chapter by chapter. The book was printed in grayscale, so I don't have a pure BLACK AND WHITE document. I would like to optimize the file size, but I have a few questions about that.
    Currently running:
    Windows 7
    Acrobat Pro X
    Epson GT-S80 High-speed scanner
    1. What is a good typical workflow? I have tried scanning the documents to PDF using the scanner's software then opening them up in Acrobat to OCR them. I have tried using Acrobat's Scan feature with OCR being one of the steps in the scanning process. I have tried letting both softwares do their own color mode detection, where they will mix black and white and grayscale to reduce the file size, but have typically told it to stick with grayscale because that gives me the cleanest and clearest document. Does anyone have any recommendations on getting a good quality image and using a mix of black and white, as well as grayscale, or should I keep using just grayscale?
    2. I am having some trouble, I think, with the file size. I have a 12 page document I believe was either scanned at 300 dpi or was scanned at full resolution because I used CLEARSCAN, and downsampled everything to 300 dpi. I don't remember exactly, but that file is about 2.20 MB in size, and I think that runs about 185K per page. I would think there could be a way to get a smaller file.
    3. For text recognition purposes, this document is not ideal because it is a collection of powerpoint slide sheets (2 - 3 slides per page), and in some cases there is text on top of image in the slides, and it seems very hard to discern.
    4. Once a document has been scanned, and OCR has been run on it, I was under the impression that the OCR is in a separate layer, and that (if Searchable Text is chosen), you basically have a scanned image with another layer of searchable text. Because the OCR'd text is "there somewhere", is it possible to remove the scanned image text, and have just the raw recognized text, similar to if I created the document in Word, and created a PDF?
    5. Sort of back to number 1, suppose I am stuck with leaving the scanned image behind, and just running OCR, what is the optimal way to reduce the file size of the PDF? I had read that running your scan at 600 dpi may help with the text recognition. The same article suggested doing the higher resolution scan and using the ClearScan because it would  a) recognize the text better and  b) convert the text image to actual text and reduce the file size. From there, should I then just run the PDF optimizer to downsample the images to a certain DPI to further reduce the size?
    Hopefully you all can understand what I am saying and help fill in some gaps.
    Thanks,
    Ian

    Let us know if this tutorial helps you with your workflow Acrobat X: Taking the guesswork out of scanning to PDF.

  • Can file size validation be done in client for af:inputfile

    if you upload a big file with size bigger than the 'org.apache.myfaces.trinidad.UPLOAD_MAX_DISK_SPACE' in web.xml, then it will take a long time to process. finally, a error message is shown in the page, saying "the file is too large". the whole page seems hang during the uploading process.
    can this size validation be done in client site before uploading?

    Actually our QA had fire a bug for this issue,:
    they set proxy and not check the bypass option , then when they upload a 100M size file ,the browser will hung there during the uploading process
    and they do believe this is ADF's bug ,no matter how we explain
    so we had do some research try to fixed it by ourselves , then here is a workaround may be work ,we have not implemented just have this thinking :
    In IE browser we can use the ActiveX to implementation
    In FF and Google Chrome we can use the W3C's new API to get the file size:
    <input type='file' id='fileinput'>
    <script>
    var input = document.getElementById('fileinput');
    var file = input.files[0];
    var fireName = file.name
    var fireSize = file.size;
    </script>
    But I also found in some documents pointed that the Struts and Sprint framework can check the upload file size before the file uploaded , but I do not know how can they do that.
    If other framework can do that why ADF can not, then we need to resolve this issue by ourselves?
    Edited by: user13152002 on Sep 1, 2011 11:29 PM

  • I want to reduce pdf size up to 5mb for mailing perpose, anybody have any option to reduce it since I have used optimiser & reduce file size option but its not helpfull.

    I want to reduce pdf size up to 5mb for mailing perpose, anybody have any option to reduce it since I have used optimizer & reduce file size option but its not helpful.

    The optimizer can reduce space, but some things can't get smaller. Text for example. Play with the settings, examine the results of Audit Space Usage.
    Or give up. Even 5 MB is too large for a bulk mailing, by far. Instead put it on your web site and mail a link - done!

  • Specify exact pixel and file size

    Hello,
    I have recently purchased Photoshop Elements 4.0 for Windows for a company project. I need to compile a number of outdoor photographs to be uploaded to a specific website. The photo specifications required by the website are that all photos must in JPG format, be 350 pixels across and no more than 40kbs in file size. I've been experimenting with photoshop to try and achieve this effect, but am wondering if there is a simple way to accomplish this. I have been able to adjust horizontal pixel number, which automatically adjusts vertical pixel number and photo size. When I save photos, I am able to adjust what I assume is the resolution of the photo. If there is not too much color contrast in the photo, I can sometimes reduce the file size to 40kbs by taking it as low as it will go, but not always. I was just wondering if there is a simple way to make sure that a photo will be saved with a specific pixel number and no more than a certain file size (40kbs). I would greatly appreciate any suggestions.
    Brandon

    You can also set the pixel dimensions in S4W and save a step. Just enter the size you want in the box about halfway down the right panel, and then click Apply. Then you can optimize the file size by clicking the right-facing arrow at the upper right of the panel and choosing Optimize to file size. You can specify the exact size you want. Just watch the preview to be sure the quality is good enough at the target size.

  • JPEG File Size Help!

    Any tips (or software) on how to reduce the file size of
    JPEGs without compromising the quality. I have a flash ad that has
    3 screens with photos, but clients needs the .swf file to be under
    20K. I can only get it down to 28K.
    I've done the following:
    * cropped photo down to actual size
    * used "Save for Web" function in Photoshop
    Any thing else I can do???
    TIA!

    There is a flyout option in the PS Save for Web dialogue (top right)
    it allows you to Optimize to File Size
    I'd be looking at that 1st
    hope this helps

Maybe you are looking for