16 Replies Latest reply on Aug 1, 2013 7:36 AM by Peter Kahrel

    [CC/JS] Check for Images Containing White Borders

    tobias.wantzen Level 1

      Hi,

       

      I’m writing a script, that places images (CMYK-JPGs) in frames. So far so good, but when an image has a white background at its top, right, bottom OR left border, then the image frame should get a border in InDesign (30% black).

      Is there an easy way to check this?

       

      I’d be very glad to get some ideas, starting points, code snippets whatever ...

       

      Cheers

      Tobias

        • 1. Re: [CC/JS] Check for Images Containing White Borders
          Peter Kahrel Adobe Community Professional & MVP

          Tobias,

           

          InDesign can't look at pixels. You'd have to use PhotoShop to check the images and if any have a white background, add something to their name (e.g. xyz.jpg > xyz_bg.jpg). Then in InDesign you can recognise the images that need a border by _bg. in their name.

           

          Peter

          • 2. Re: [CC/JS] Check for Images Containing White Borders
            Jump_Over Level 5

            Hi,

             

            Agree with Peter, but...

            You could switch image clippingPath type to get a path restricted to frame and use it to create a shape with geometricBounds to be compared with your image gBounds.

            Just an idea...

             

            to start it:

            mImage  = app.selection[0].graphics[0];
            with (mImage.clippingPath) {
              currClip = clippingType;
                      clippingType = ClippingPathType.DETECT_EDGES;
                      useHighResolutionImage = false;
                      restrictToFrame = true;
                      threshold = 1;
                      mPath = paths[0];     // hm... here can be more paths...
                      }
            mShape = app.activeDocument.graphicLines.add({strokeWidth:0.1});
            mShape.paths[0].entirePath = mPath.entirePath;
            
            mImage.clippingPath.clippingType = currClip;
            

             

            assumes: frame (image.parent) is selected

            result: mShape is an object with geoBounds and set of points. How to compare it - that's mathematic effort and your tolerance assumings...

            mShape is to remove() at the end

             

            Jarek

            • 3. Re: [CC/JS] Check for Images Containing White Borders
              Peter Kahrel Adobe Community Professional & MVP

              Hi Jarek,

               

              Nice one. But I assumed that that white background is simply a bunch of white-coloured pixels. Would a clipping path detect those?

               

              Peter

              • 4. Re: [CC/JS] Check for Images Containing White Borders
                Jump_Over Level 5

                Hi,

                threshold property limits this detection. If == 0 detect edges of white pixels but less number produces more "independent" paths.

                 

                Jarek

                • 5. Re: [CC/JS] Check for Images Containing White Borders
                  tobias.wantzen Level 1

                  @Peter:

                  Thanks for your answer. That’s what I expected.

                   

                  @Jarek:

                  Very nice idea! I’ll give it a try. And to your comment »// hm... here can be more paths...«: When there are more than one path, I definitely have white borders. So the first, simple check would be something like:

                  if ( paths.length !=1 ) // white borders found

                   

                  Regards

                  Tobias

                  • 6. Re: [CC/JS] Check for Images Containing White Borders
                    tobias.wantzen Level 1

                    Maybe something like this:

                     

                     

                    #target InDesign
                    
                    
                    mImage  = app.selection[0].graphics[0];
                    with (mImage.clippingPath) {
                      currClip = clippingType;
                              clippingType = ClippingPathType.DETECT_EDGES;
                              useHighResolutionImage = true;
                              restrictToFrame = true;
                              threshold = 3;
                              tolerance = 0;
                              mPath = paths;
                              }
                          
                    if (mPath.length != 1) {
                        alert( mPath.length );
                    } else {
                        alert( mPath[0].pathPoints.count() );
                    }
                    
                    

                     

                    I’ll test a bit ...

                     

                    Regards

                    Tobias

                    • 7. Re: [CC/JS] Check for Images Containing White Borders
                      Jump_Over Level 5

                      tobias.wantzen wrote:

                       

                      if ( paths.length !=1 ) // white borders found

                       

                      Not true, I am afraid. This could find a paths inside your image, far away from borders as well. It depends on threshold and tolerance and... your image pixels of course.

                       

                      Jarek

                      • 8. Re: [CC/JS] Check for Images Containing White Borders
                        tobias.wantzen Level 1

                        But a path inside my image will only be found, when I set

                        includeInsideEdges = true

                        So I have to add »includeInsideEdges = false« and that’d be it.

                         

                        Or am I missing something?

                         

                        Tobias

                        • 9. Re: [CC/JS] Check for Images Containing White Borders
                          Jump_Over Level 5

                          Hi Thobias,

                           

                          includeInsideEdges = false could decrease a number of paths found but it still can be 10th of them.

                           

                          More I test this idea more I am scared to results

                          • 10. Re: [CC/JS] Check for Images Containing White Borders
                            tobias.wantzen Level 1

                            Jarek,

                            includeInsideEdges = false could decrease a number of paths found but it still can be 10th of them.

                             

                            Hoping not to annoy you, but I don’t understand your remark. If I use the clipping path in InDesign (»in the UI«), and do not check »Include Inside Edges« or »Invert Path«, InDesign only tries to find a clipping path from outside to inside (from the object borders as far as it goes) ignoring any white areas _inside_ the image. So I get one path with x points on it (but 1 path). Why should the behaviour of scripting be different to that? Or – the other way around – can you give me an example which illustrates your thoughts?

                             

                            Tobias

                            • 11. Re: [CC/JS] Check for Images Containing White Borders
                              [Jongware] Most Valuable Participant

                              Jarek, this is a fantastic idea! It even works on vector illustrations Tobias, this is what Jarek means:

                               

                              clippath.PNG

                               

                              It is a single 'pathitem' but with lots of compound sub-paths.

                               

                              By the way: I found that setting threshold and tolerance to 0 is No Good. It then fails with the simple plain white background above, and all I get is the original rectangle. But if you find the paths are a bit too tight, you can always set the inset value to a small value.

                               

                              There is a caveat: if you cropped your image, the cropped-off part with "invisible" parts will also be considered, and be included in the final path.

                               

                              Other than that: great suggestion! I'm going to take a look at how efficient this is. No more manually cropping images to the nearest pixel!

                              1 person found this helpful
                              • 12. Re: [CC/JS] Check for Images Containing White Borders
                                tobias.wantzen Level 1

                                Jongware,

                                 

                                thanks for your reply. When I recreate your example image, for »mPath = paths.length« I get »6«. I’m no native english speaker, maybe I don’t get the point by misunderstanding something.

                                 

                                Your caveat is right: with cropped image the »invisible« part is also considered. (I thought this would not be the case when choosing ».restrictToFrame = true;« ...)

                                 

                                But the code I tried - based on Jarek’s idea - solves my problem! Great work - thanks!

                                 

                                When someone gets some new insights on this subject, I’d be glad to know!

                                 

                                Tobias

                                • 13. Re: [CC/JS] Check for Images Containing White Borders
                                  [Jongware] Most Valuable Participant

                                  Tobias, you mentioned you got only a single path for your test image. My example shows it is possible to get more than one.

                                   

                                  As to my caveat: you can simply set 'restrictToFrame' to true to solve this. Here is an experimental bit of code; it uses convertToFrame so you can see what happens, but in a final script this won't be necessary.

                                   

                                  if (app.selection.length == 1)
                                  {
                                  img = app.selection[0];
                                  if (img.hasOwnProperty("images") && img.images.length == 1)
                                  {
                                    img.images[0].clippingPath.properties = {clippingType: ClippingPathType.DETECT_EDGES, restrictToFrame:true, includeInsideEdges:false, threshold:3,tolerance:2, useHighResolutionImage:true};
                                  // paths = img.images[0].clippingPath.paths;
                                    paths = img.images[0].clippingPath.convertToFrame();
                                    paths.strokeWeight = 2;
                                    paths.strokeColor = app.activeDocument.swatches.item("Black");
                                  }
                                  }
                                  
                                  1 person found this helpful
                                  • 14. Re: [CC/JS] Check for Images Containing White Borders
                                    Jump_Over Level 5

                                    Uff,

                                     

                                    three heads are better than one

                                     

                                    Jarek

                                    • 15. Re: [CC/JS] Check for Images Containing White Borders
                                      tobias.wantzen Level 1

                                      Thanks Jongware, now I understand!

                                       

                                      And thanks again Jarek for the genious point in the right direction!

                                       

                                      Tobias

                                      • 16. Re: [CC/JS] Check for Images Containing White Borders
                                        Peter Kahrel Adobe Community Professional & MVP

                                        Great stuff, Jarek. Thanks.

                                         

                                        Peter