7 Replies Latest reply on Feb 5, 2018 2:29 AM by Feast

    Resizing document without losing colored pixels

    Feast Level 1

      I've been using Photoshop's Trim and Resize image and I realized that it always deletes unwanted pixels of my .psd document.

       

      What can I do to have a more accurate Trim / Resize on my documents? I believe the cause of this is due to Resize.

       

      The script is very simple:

          doc = app.activeDocument;
          
          // trim
          doc.trim(TrimType.TRANSPARENT);
         
          // ========== set units to pixels ==========//
          var oldPrefs = app.preferences.rulerUnits;
          app.preferences.rulerUnits = Units.PIXELS;
          DocTrimmedWidth = doc.width;
          DocTrimmedHeight = doc.height;
         
          // resize algorithm
          if (DocTrimmedHeight > DocTrimmedWidth) {
              doc.resizeImage(undefined, UnitValue(1024, "px"),72, ResampleMethod.BICUBIC, 100);
          }
          else {
              doc.resizeImage(UnitValue(1024, "px"), undefined, 72, ResampleMethod.BICUBIC, 100);
          }
      
      
          doc.activeLayer.applyOffset(0,512, OffsetUndefinedAreas.WRAPAROUND);
      
      

       

       

      I tried different kinds of ResampleMethods to no avail. Here's the .psd I'm using it on:
      ImproperResize.psd - Google Drive

       

      There are always transparents pixels appearing on the .psd. What can I do about this problem?
      https://i.imgur.com/TZc36yG.png

        • 1. Re: Resizing document without losing colored pixels
          r-bin Level 5

          Try using this plugin for the resize

          C3C Image Size – Telegraph

          1 person found this helpful
          • 2. Re: Resizing document without losing colored pixels
            r-bin Level 5

            You can call this plugin in the script using the following function

            )

             

            function c3c_resize(w, h, sharp, use_contrast)
                {   
                try {
                    if (sharp == undefined) sharp = 0;
                    if (use_contrast == undefined) use_contrast = false;
            
                    var old_units = app.preferences.rulerUnits;
                    app.preferences.rulerUnits = Units.PIXELS;
            
                    var doc = app.activeDocument;
            
                    var w0 = Number(doc.width.value);
                    var h0 = Number(doc.height.value);
            
                    while (w > w0 || h > h0) 
                        {
                        doc.resizeImage(UnitValue(4*w0,"px"), UnitValue(4*h0,"px"), doc.resolution, ResampleMethod.NEARESTNEIGHBOR);
                        w0 = Number(doc.width.value);
                        h0 = Number(doc.height.value);
                        }
            
                    app.preferences.rulerUnits = old_units;
            
                    var d = new ActionDescriptor();
                
                    d.putInteger( charIDToTypeID( "Wdth" ), w );
                    d.putInteger( charIDToTypeID( "Hght" ), h );
                
                    d.putBoolean( charIDToTypeID( "cC3P" ), true );
                    d.putBoolean( charIDToTypeID( "cC3F" ), false );
                
                    d.putEnumerated( charIDToTypeID( "uc3S" ), charIDToTypeID( "tc3U" ), charIDToTypeID( "uc30" ) );
            
                    if (use_contrast == true)
                        d.putEnumerated( charIDToTypeID( "mc3S" ), charIDToTypeID( "tc3M" ), charIDToTypeID( "rc31" ) );
                    else
                        d.putEnumerated( charIDToTypeID( "mc3S" ), charIDToTypeID( "tc3M" ), charIDToTypeID( "rc30" ) );
            
                    d.putInteger( charIDToTypeID( "pC3S" ), sharp );
                
                    d.putInteger( charIDToTypeID( "pC3W" ), w );
                    d.putInteger( charIDToTypeID( "pC3H" ), h );
            
                    executeAction( stringIDToTypeID( "5f3d6843-d895-44c2-bbb7-b71ca0cb8c01" ), d, DialogModes.NO );
            
                    return true;
                    }
                catch (e) { alert(e); return false; }
                }
            
            1 person found this helpful
            • 3. Re: Resizing document without losing colored pixels
              Feast Level 1

              Great, thanks for the help r-bin. I'll try this out in an hour when I'm back home.

              • 4. Re: Resizing document without losing colored pixels
                geppettol66959005 Level 2

                I tried to use it on mac

                with photoshop  cs6 cc cc2014 does not work?

                Schermata 2018-02-05 alle 10.48.48.png

                • 5. Re: Resizing document without losing colored pixels
                  r-bin Level 5

                  Please change the line in the function

                   

                  d.putBoolean( charIDToTypeID( "cC3P" ), true );
                  

                  to

                   

                  d.putBoolean( charIDToTypeID( "cC3P" ), false );
                  

                   

                  This guarantees the exact final dimensions W and H that you set.

                  This parameter tries to keep the proportions fitting on only the width

                  1 person found this helpful
                  • 6. Re: Resizing document without losing colored pixels
                    r-bin Level 5

                    It's strange. On a mac I can not say anything. Write to the author.

                    • 7. Re: Resizing document without losing colored pixels
                      Feast Level 1

                      It works great, thanks Much appreciated!