15 Replies Latest reply on Feb 27, 2010 3:03 PM by Kasper Hviid

    pasting graphic with transparence without centering

    Kasper Hviid

      I have an image which I copy-paste into another document. My problem is that the image has transparent areas at the edges. This causes the image to be centered when I paste it.

       

      I know I can fix it by filling the pixel in each four corners with color at 1 % opacity. However, I would really like a smarter way!

       

      - Kasper

        • 1. Re: pasting graphic with transparence without centering
          Paul Riggott Level 6

          You can make a selection and paste it into that.

          • 2. Re: pasting graphic with transparence without centering
            MarkWalsh Level 4

            This isn't a scripting issue, but if you are just trying to duplicate layer(s) from one document to another, if you drag them holding down the shift key, they will be placed in the same spot in the receiving document (as long as the documents are the same size)

            • 3. Re: pasting graphic with transparence without centering
              Kasper Hviid Level 1

              Thanks for the replies! But making a selection in the destination document will not keep the target content from being centered inside that selection. And shift-drag doesn't seem to work, either.

              • 4. Re: pasting graphic with transparence without centering
                Paul Riggott Level 6

                You make the selection where you want to put the content. Yes the content will be centered in the selection isn't that what you want?

                • 5. Re: pasting graphic with transparence without centering
                  Kasper Hviid Level 1

                  I'm already make a selection (128x128 px) where want my 128x128 content.

                   

                  The problem is that the source document has a lot of transparency at its edges, and the non-transparent pixels are placed in the lower-right part of the document.

                   

                  When I copy it, Photoshop forgets about the part that are 100 % transparent, and just copies the rest. As a result it gets centered inside the selection.

                   

                  I'll think I just do that thing with placing pixels at the corners. It's just such a bother for something that simple ...

                  • 6. Re: pasting graphic with transparence without centering
                    Paul Riggott Level 6

                    What you can do is get the layer bounds of the layer to copy, make a selection on those bounds, get your copy, make a selection based on the layer bounds in your target document and paste.

                    • 7. Re: pasting graphic with transparence without centering
                      MarkWalsh Level 4

                      Shift drag should only work if the pixel dimensions are exactly the same. Alternatively, you could select 'Duplicate layers' and specify the target document, and it should keep the same relative position, regardless of pixel size.

                      • 8. Re: pasting graphic with transparence without centering
                        Paul Riggott Level 6

                        Here is some sample code..

                        var startRulerUnits = app.preferences.rulerUnits;
                        app.preferences.rulerUnits = Units.PIXELS;
                        //select document and layer to copy
                        var LB = activeDocument.activeLayer.bounds;
                        activeDocument.selection.select([[LB[0],LB[1]], [LB[2],LB[1]], [LB[2],LB[3]], [LB[0], LB[3]]], SelectionType.REPLACE, 0, false);
                        activeDocument.selection.copy();
                        activeDocument.selection.deselect();
                        //select target document
                        var originalDoc = documents[1];
                        activeDocument = originalDoc;
                        //Top left X and Y of where to place the selection.
                        var X=10;
                        var Y = 20;
                        //Width of selection
                        var W = LB[2].value - LB[0].value;
                        //Height of selection
                        var H = LB[3].value - LB[1].value;
                        activeDocument.selection.select([[X,Y], [X+W,Y], [X+W,Y+H], [X, Y+H]], SelectionType.REPLACE, 0, false);
                        app.activeDocument.paste(true);
                        activeDocument.selection.deselect();
                        app.preferences.rulerUnits = startRulerUnits;
                        
                        1 person found this helpful
                        • 9. Re: pasting graphic with transparence without centering
                          Michael L Hale Level 5

                          I would not copy and paste. I would use layer.duplicate.

                           

                          If both docs are the same size and res this will dupe the layer in the exact positon it was in. If not you can make a temp copy of the source doc and match the size and res before the dupe.

                           

                          I think that using the clipboard is bad for both the image and the system resources.

                          • 10. Re: pasting graphic with transparence without centering
                            Kasper Hviid Level 1

                            @Michael L Hale:

                             

                            layer.dublicate sounds like a good method .... but sadly, the target document is much larger than the source.

                             

                            @Paul Riggott:

                            Thanks for the code! Sadly, it refused to work. It just places the graphic in the upper left, except for the part with var X=10; var Y = 20;

                             

                             

                             

                             

                            • 11. Re: pasting graphic with transparence without centering
                              Michael L Hale Level 5

                              I think one of the problems is we are not sure where you want the layer placed in the target doc. Paul's code does work. You just need to edit var X=10; var Y = 20; so that it places the image where you want it.

                              • 12. Re: pasting graphic with transparence without centering
                                Michael L Hale Level 5

                                Or try this. Note you will still need to edit to set where you want the layer placed.

                                 

                                var startRulerUnits = app.preferences.rulerUnits;
                                app.preferences.rulerUnits = Units.PIXELS;
                                var targetDoc = app.documents[1];
                                app.activeDocument.activeLayer.duplicate( targetDoc );
                                //select target document
                                activeDocument = targetDoc;
                                //Top left X and Y of where to place the selection.
                                var X=0;// edit these two lines to place the image where you want.
                                var Y = 0;
                                positionLayer( app.activeDocument.activeLayer, X, Y );
                                app.preferences.rulerUnits = startRulerUnits;
                                function positionLayer( lyr, x, y ){// layerObject, Number, Number
                                     // if can not move layer return
                                     if(lyr.iisBackgroundLayer||lyr.positionLocked) return
                                     // get the layer bounds
                                     var layerBounds = lyr.bounds;
                                     // get top left position
                                     var layerX = layerBounds[0].value;
                                     var layerY = layerBounds[1].value;
                                     // the difference between where layer needs to be and is now
                                     var deltaX = x-layerX;
                                     var deltaY = y-layerY;
                                     // move the layer into position
                                     lyr.translate (deltaX, deltaY);
                                }
                                
                                • 13. Re: pasting graphic with transparence without centering
                                  Kasper Hviid Level 1

                                  Here I have set Paul Riggott's script to X=0; Y=0;

                                   

                                  The image ignores the transparent area in the upper left and place the edge of the visible pixels at 0,0

                                   

                                  Michaels script seems to do the same thing:

                                   

                                  http://img715.imageshack.us/img715/9885/photoshopwoes.jpg

                                   

                                  HOWEVER ... If I goes back one step in history after running Michaels script, it looks alright. (The step that is called "Free transform")

                                   

                                  I think I'm almost quite very pretty close! Thank you guys! :-D

                                  • 14. Re: pasting graphic with transparence without centering
                                    Michael L Hale Level 5

                                    My script without the translate/transform step is just a layer duplicate script, which is the first thing I suggested.

                                    • 15. Re: pasting graphic with transparence without centering
                                      Kasper Hviid Level 1

                                      I only needed one small adjustment for the code to work - here is the final one:

                                       

                                      Thanks for the help. This one was driving me nuts!

                                       

                                      var startRulerUnits = app.preferences.rulerUnits;
                                      app.preferences.rulerUnits = Units.PIXELS;
                                      
                                      var targetDoc = app.documents[1];
                                      app.activeDocument.activeLayer.duplicate( targetDoc );
                                      
                                      //select target document
                                      activeDocument = targetDoc;
                                      
                                      //Top left X and Y of where to place the selection.
                                      var X= 50;// edit these two lines to place the image where you want.
                                      var Y = 50;
                                      positionLayer( app.activeDocument.activeLayer, X, Y );
                                      
                                      app.preferences.rulerUnits = startRulerUnits;
                                      
                                      
                                      
                                      function positionLayer( lyr, x, y ){// layerObject, Number, Number
                                           
                                           // if can not move layer return
                                           if(lyr.iisBackgroundLayer||lyr.positionLocked) return
                                           
                                           // get the layer bounds
                                           var layerBounds = lyr.bounds;
                                           
                                           // get top left position
                                           var layerX = layerBounds[0].value;
                                           var layerY = layerBounds[1].value;
                                           
                                           // move the layer into position
                                           lyr.translate (x, x);
                                      
                                      }