'Python script simplification

I created this python script to replace images in a psd file, but I feel that I am repeating the same code and was looking to simplify it, since I have more instances of "groups", I just pasted 2 here to make it shorter. Here is the ps file screenshot.

photoshop file screenshot

Thank you for your time.

import win32com.client

#Open Adobe Photoshop App programmatically and locate PSD file
ps = win32com.client.Dispatch("Photoshop.Application")

mainPsd = ps.Open("C:/Users/Admin/Documents/images/edit_tiling_wall.psd")


#group 5 copy
mainGrp = mainPsd.layerSets[0]

#all grps in main group
allLayerSets = mainPsd.layerSets[0].layerSets


#function to resize

def resize(layer,w,h):
    w_max = w
    h_max = h
    bounds = layer.bounds
    w = bounds[2] - bounds[0]
    h = bounds[3] - bounds[1]
    #print(w,h)
    diffw = w_max - w
    diffh = h_max - h
    #print(diffw,diffh)
    aumw = diffw + w
    aumh = diffh + h
    #print(aumw,aumh)
    moltw = 100*aumw/w
    molth = 100*aumh/h
    #print(moltw,molth)

    layer.Resize(moltw,molth)

    bounds = layer.bounds
    w = bounds[2] - bounds[0]
    h = bounds[3] - bounds[1]
    return w,h


def translate(doc,layer,goalX,goalY):

    bounds = layer.bounds
    w = bounds[2] - bounds[0]
    h = bounds[3] - bounds[1]
    posX = doc.width - round(doc.width - (bounds[0] + w / 2))
    posY = doc.height - round(doc.height - (bounds[1] + h / 2))

    layer.Translate(goalX-posX,goalY-posY)

    bounds = layer.bounds
    w = bounds[2] - bounds[0]
    h = bounds[3] - bounds[1]
    posX = doc.width - round(doc.width - (bounds[0] + w / 2))
    posY = doc.height - round(doc.height - (bounds[1] + h / 2))
    return posX, posY



for grp in allLayerSets:

    # filter first type of texture
    if grp.name.startswith('Group 1'):

        print ( grp.name )
        print ( grp.artLayers[-1].name ) #base layer name

        # base layer
        baseLayer = grp.artLayers[-1]

        #get width and height of base layer
        boundsLayer = baseLayer.bounds
        w = boundsLayer[2] - boundsLayer[0]
        h = boundsLayer[3] - boundsLayer[1]

        print(w,h)


        #get position(center of layer img) of base layer
        posX = mainPsd.width - round(mainPsd.width - (boundsLayer[0] + w / 2))
        posY = mainPsd.height - round(mainPsd.height - (boundsLayer[1] + h / 2))

        print( posX, posY )

        # new image to add
        newTexture = "C:/CG_CONTENT/TEXTURES/Wall_v2_height.exr"

        newHeightMap = ps.Open(newTexture)

        newHeightMap_layer = newHeightMap.ArtLayers.Item(1)
        newHeightMap_layer.Copy() 
        newHeightMap.Close(2)

        # Set as active image in Photoshop
        ps.ActiveDocument = mainPsd          

        # Paste 'newHeightMap' image from clipboard 
        pasted = mainPsd.Paste()

        currentLayer = mainPsd.activeLayer

        # Move the pasted layer to the correct grp
        currentLayer.Move(grp, 1)

        # resize in pixels the new texture
        resize(currentLayer, w, h)

        # IS IT FLIPPED HORIZONTALLY OR VERTICALLY? MAYBE ADD A TAG TO FLIPPED LAYERS?
        if baseLayer.name.endswith('_x'):

            # flip horizontal
            currentLayer.Resize(-100,100)

        elif baseLayer.name.endswith('_y'): 

            # flip vertical
            currentLayer.Resize(100,-100)


        elif baseLayer.name.endswith('_x_n_y'): 

            # flip horizontal and vertical
            currentLayer.Resize(-100,-100)

        #translate the texture to position
        translate(mainPsd,currentLayer,posX,posY)

        baseLayer.visible = False



    # filter first type of texture
    elif grp.name.startswith('Group 2'):

        print ( grp.name )
        print ( grp.artLayers[-1].name ) #base layer name

        # base layer
        baseLayer = grp.artLayers[-1]


        #get width and height of base layer
        boundsLayer = baseLayer.bounds
        w = boundsLayer[2] - boundsLayer[0]
        h = boundsLayer[3] - boundsLayer[1]

        print(w,h)


        #get position(center of layer img) of base layer
        posX = mainPsd.width - round(mainPsd.width - (boundsLayer[0] + w / 2))
        posY = mainPsd.height - round(mainPsd.height - (boundsLayer[1] + h / 2))

        print( posX, posY )

        # new image to add
        newTexture = "C:/CG_CONTENT/TEXTURES/Wall_v3_height.exr"

        newHeightMap = ps.Open(newTexture)

        newHeightMap_layer = newHeightMap.ArtLayers.Item(1)
        newHeightMap_layer.Copy() 
        newHeightMap.Close(2)

        # Set as active image in Photoshop
        ps.ActiveDocument = mainPsd          

        # Paste 'newHeightMap' image from clipboard 
        pasted = mainPsd.Paste()

        currentLayer = mainPsd.activeLayer

        # Move the pasted layer to the correct grp
        currentLayer.Move(grp, 1)

        # resize in pixels the new texture
        resize(currentLayer, w, h)

        # IS IT FLIPPED HORIZONTALLY OR VERTICALLY? MAYBE ADD A TAG TO FLIPPED LAYERS?
        if baseLayer.name.endswith('_x'):

            # flip horizontal
            currentLayer.Resize(-100,100)

        elif baseLayer.name.endswith('_y'): 

            # flip vertical
            currentLayer.Resize(100,-100)

        elif baseLayer.name.endswith('_both'): 

            # flip horizontal and vertical
            currentLayer.Resize(-100,-100)  

        #translate the texture to position
        translate(mainPsd,currentLayer,posX,posY)

        baseLayer.visible = False



Solution 1:[1]

I suggest you to use cookie to carry a value across pages.

I achieve it in two steps:

Firstly, add an Onchange event to <select>, when you choose an option, the Onchange event will set a cookie to save the value which you choose.

Secondly, when you load other pages, the <select> will read the cookie to show your previous selection.

I write a simple demo to achieve this .Here is my code:

<li>
    <select id="fruit" onchange="save(this.value)">
        <option value=0>apple</option>
        <option value=1>pear</option>
        <option value=2>watermelon</option>
        <option value=3>cherry</option>
    </select>
</li>

@section Scripts
{
    <script>
        //....................set Cookie............
        var saveclass = null;

        function save(cookieValue) {
            var sel = document.getElementById('fruit');
            saveclass = saveclass ? saveclass : document.body.className;
            document.body.className = saveclass + ' ' + sel.value;
            setCookie('theme', cookieValue, 365);
        }

        function setCookie(cookieName, cookieValue, nDays) {


            var today = new Date();
            var expire = new Date();

            if (nDays == null || nDays == 0)
                nDays = 1;

            expire.setTime(today.getTime() + 3600000 * 24 * nDays);
            document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() + ";path=/";
        }
        //......................read Cookie.............
        document.addEventListener('DOMContentLoaded', function () {
            var themeSelect = document.getElementById('fruit');
            var selectedTheme = readCookie('theme');

            themeSelect.value = selectedTheme;
            saveclass = saveclass ? saveclass : document.body.className;
            document.body.className = saveclass + ' ' + selectedTheme;
        });

        function readCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
            }
            return null;
        }
    </script>
}

When you visit the page again ,you can see the page hold the value of dropdown list.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1