{"id":3008,"date":"2021-08-21T10:04:11","date_gmt":"2021-08-21T17:04:11","guid":{"rendered":"https:\/\/thelawlers.com\/Blognosticator\/?p=3008"},"modified":"2021-12-30T08:53:19","modified_gmt":"2021-12-30T16:53:19","slug":"applescript-solves-my-grid-mask-challenge-in-adobe-photoshop","status":"publish","type":"post","link":"https:\/\/thelawlers.com\/Blognosticator\/?p=3008","title":{"rendered":"AppleScript solves my grid mask challenge in Adobe Photoshop"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/thelawlers.com\/Blognosticator\/wp-content\/uploads\/2015\/10\/Blognosticator-Head.png\"><img loading=\"lazy\" decoding=\"async\" width=\"252\" height=\"115\" src=\"https:\/\/thelawlers.com\/Blognosticator\/wp-content\/uploads\/2015\/10\/Blognosticator-Head.png\" alt=\"\" class=\"wp-image-1940\"\/><\/a><\/figure>\n\n\n\n<p>I pride myself on being a competent user of Adobe <em>Photoshop.<\/em> I know how to make my photos look good by sliding sliders and pushing buttons and applying filters and adding Layer Effects and all sorts of&nbsp;<em>visual<\/em>&nbsp;things.<\/p>\n\n\n\n<p>Occasionally I need to do something out of the ordinary, and in these situations I combine my skills in Photoshop with my skills in programming with <em>AppleScript.<\/em> I taught myself <em>AppleScript<\/em> back in the 1980s when I had insomnia. I simply couldn\u2019t get to sleep at night. So, instead of counting sheep, I wrote programs with AppleScript.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/thelawlers.com\/Blognosticator\/wp-content\/uploads\/2021\/08\/Bishop-Peak-composite-1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"317\" src=\"https:\/\/thelawlers.com\/Blognosticator\/wp-content\/uploads\/2021\/08\/Bishop-Peak-composite-1.jpg\" alt=\"\" class=\"wp-image-3014\" srcset=\"https:\/\/thelawlers.com\/Blognosticator\/wp-content\/uploads\/2021\/08\/Bishop-Peak-composite-1.jpg 684w, https:\/\/thelawlers.com\/Blognosticator\/wp-content\/uploads\/2021\/08\/Bishop-Peak-composite-1-300x139.jpg 300w\" sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><\/a><figcaption>This is the collage of images I wanted to make in Adobe Photoshop. I used an AppleScript to make a checkerboard mask for the photos, then used that mask (and an inverse of it) to mask each photo separately. This made it possible to create an image without a single pixel out of place.<\/figcaption><\/figure>\n\n\n\n<p>For the uninitiated, <em>AppleScript<\/em> is a scripting language on the Macintosh computer. It is a full-bodied computer language that can send and receive instructions to and from various applications. You can, for example, query a <em>FileMaker<\/em> database, copy a line of information from that database, then move that copy to Adobe <em>InDesign<\/em> and paste the text into a document, while applying a specific type font, size, leading and color. It\u2019s a very clever language.<\/p>\n\n\n\n<p>The greatest advantage of <em>AppleScript<\/em> is its \u201cnatural language\u201d quality. It has English-like commands (other languages are also supported). You can issue a command like:<\/p>\n\n\n\n<p><code><strong>tell application<\/strong>&nbsp;\u201cPhotoshop\u201d<br><strong>activate<\/strong><\/code><\/p>\n\n\n\n<p>\u2026and the script will open <em>Photoshop.<\/em> Many applications have sophisticated <em>AppleScript<\/em>-ability. Some have none, so you cannot control them with <em>AppleScript.<\/em><\/p>\n\n\n\n<p><em>AppleScript<\/em> wasn\u2019t my first programming language. I learned <em>COBOL<\/em> in college, had learned a language called <em>WPL<\/em> on the Apple II, and had taught myself <em>BASIC<\/em> and some <em>Pascal,<\/em> all of which I found intriguing. I believe that once you learn any programming language you can use your skills to learn another. This is because programming languages have so much in common. It\u2019s mostly form and syntax beyond the basic control functions. You just have to learn the localized grammar, and then figure out the details.<\/p>\n\n\n\n<p>Yesterday I was working on a photo mural and I needed a set of&nbsp;<em>absolutely precise<\/em>&nbsp;masks to make a checkerboard collage of images. I started out with 24 selected images from a project I did in 2016 called the&nbsp;<em>Bishop Peak Portrait Project.<\/em>&nbsp;That project was a year-long photographic study of a mountain in San Luis Obispo. I took about 200 photos every day with a camera in a weatherproof box on the roof of the Kennedy Library on the Cal Poly campus.<\/p>\n\n\n\n<p>The camera was trained on Bishop Peak, the most popular of the nine mountains that run from my city out to the ocean at Morro Bay, where Morro Rock is the second-to-last mountain in the chain. These glorious peaks were once part of the rim of a huge volcano that formed this part of California. We got the west half of the rim; the east half is located about 100 miles south of us in another part of the state.<\/p>\n\n\n\n<p>My project was supported by a grant from Cal Poly\u2019s College of Science and Mathematics. I started in March, 2016, and ended on the last day of February, 2017. Between those dates I took about 70,000 photos of my favorite mountain.<\/p>\n\n\n\n<p>The camera was a Canon T5 with a standard lens zoomed to about 50mm. The camera was powered by two 12 volt motorcycle batteries and they were kept charged by a couple of small solar panels. The camera was controlled by a Raspberry Pi microcomputer on a circuit board of my own design. It had two small power supplies and a transistor circuit that triggered the camera when the Raspberry Pi signaled for an exposure. I had it set to take one photo every five minutes from 5:00 a.m. until about 10:00 p.m.<\/p>\n\n\n\n<p>I had a 256 GB SD card in the camera which allowed me to leave the camera unattended for weeks at a time. I would occasionally climb the six floors to the roof of the building and retrieve the camera card, replacing it with another.<\/p>\n\n\n\n<p>Then I would discard the nighttime shots \u2013 many of my photos were black rectangles \u2013 then rename the files in Adobe <em>Bridge<\/em> (I used a six-digit numbering scheme). Then I scored the photos in Adobe Bridge, giving them four stars if I liked them, and five stars if I loved them.<\/p>\n\n\n\n<p>Eventually I had to pick&nbsp;<em>only one photo<\/em>&nbsp;to represent the best image taken each day for 365 days. It was surprisingly difficult because there were so many to choose from.<\/p>\n\n\n\n<p>The end product is a mural on permanent display at Cal Poly of the 365 photos, each printed on a 5 x 5 inch square of aluminum, mounted on a 22-foot panel in calendar order. It turned out really well.<\/p>\n\n\n\n<p>This year I was approached by a representative of a local law firm to provide some images for their new office. It was suggested that I could pick some of my Bishop Peak photos to make a photo collage for a wall in their foyer.<\/p>\n\n\n\n<p>I looked through my photos and picked about 30 of my favorites. Then I opened them (they are all <em>DNG<\/em> raw images) in <em>Camera Raw,<\/em> made adjustments for contrast and vibrance, and converted them into <em>Photoshop<\/em> images. Each is about 12 x 9 inches in size at 300 ppi.<\/p>\n\n\n\n<p>I wanted to make a grid of photos six across and four down for the collage. The total size of the final image is about six feet wide by three feet tall. The image will be printed and mounted on stiff Gator Foam board, then attached to the wall with a wooden cleat.<\/p>\n\n\n\n<p>I made a layout in <em>Photoshop<\/em> with blue guidelines separating the master image into a grid, then I started bringing in the images and arranging them in an attractive layout.<\/p>\n\n\n\n<p>After the second and third images I realized that it is very difficult to get the edges of these photos to align with pixel-precision and not have any gaps or corners that show errors of position or size. I needed a mask!<\/p>\n\n\n\n<p>So, I drew one, and then I copied it and propagated it across my canvas. But it was slightly off, and the corners didn\u2019t work perfectly, and at the end of the first row I had a handful of pixels that ran off the end. I needed more precision.<\/p>\n\n\n\n<p>I decided to draw the masks using <em>AppleScript.<\/em> To do this I made a template in Illustrator and put the coordinates in pixels onto the template.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/thelawlers.com\/Blognosticator\/wp-content\/uploads\/2021\/08\/Grid-1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"306\" src=\"https:\/\/thelawlers.com\/Blognosticator\/wp-content\/uploads\/2021\/08\/Grid-1.jpg\" alt=\"\" class=\"wp-image-3018\" srcset=\"https:\/\/thelawlers.com\/Blognosticator\/wp-content\/uploads\/2021\/08\/Grid-1.jpg 684w, https:\/\/thelawlers.com\/Blognosticator\/wp-content\/uploads\/2021\/08\/Grid-1-300x134.jpg 300w\" sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><\/a><figcaption>This is my annotated grid for the mask script. Each pair of numbers is the X,Y position for that corner <br>of each mask in pixels. The entire canvas is 21,516 X 9964 pixels in size.<\/figcaption><\/figure>\n\n\n\n<p><em>Photoshop<\/em> can be programmed with&nbsp;<em>Visual Basic<\/em>&nbsp;on Windows machines, with&nbsp;<em>Javascript<\/em>&nbsp;on Mac and Windows, and with&nbsp;<em>AppleScript<\/em>&nbsp;on Mac. I have considerable experience with the <em>AppleScript<\/em> version, and I approached it by opening a script I had written a few years ago when I was making the 365-photo mural for Cal Poly. That script uses pixel coordinates to make a block of black on which I put the date and time of each photo. I saved a new copy of that script, stripped it of its text components, and kept the rectangles part for my masking adventure.<\/p>\n\n\n\n<p>The idea was to make solid black rectangles of exactly the correct size for one photo, followed by an equal amount of white space, followed by another black rectangle until it filled the canvas with a checkerboard of solids and white spaces. This would be the primary mask. I created a canvas in Photoshop and had it open, then I tested my script on a single rectangle. It worked, so I built a whole row and tested it again. That failed due to a simple math error, so I fixed that error and got a row of three black rectangles with equal white spaces between.<\/p>\n\n\n\n<p>Here is an example:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/thelawlers.com\/Blognosticator\/wp-content\/uploads\/2021\/08\/Scripting-a-rectangle-1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"495\" src=\"https:\/\/thelawlers.com\/Blognosticator\/wp-content\/uploads\/2021\/08\/Scripting-a-rectangle-1.jpg\" alt=\"\" class=\"wp-image-3021\" srcset=\"https:\/\/thelawlers.com\/Blognosticator\/wp-content\/uploads\/2021\/08\/Scripting-a-rectangle-1.jpg 684w, https:\/\/thelawlers.com\/Blognosticator\/wp-content\/uploads\/2021\/08\/Scripting-a-rectangle-1-300x217.jpg 300w\" sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><\/a><figcaption>This is a single line from the AppleScript that describes the coordinates of one of my rectangles.<br>It uses a Cartesian coordinate system of X,Y points, then in a subsequent line it <br>fills the rectangle with black.<br>In the line of code you\u2019ll see that the bold words are commands, the green words are variables, and the integer values are shown in bold. Sets of brackets hold pairs of X,Y coordinates made up of variable names <br>and integers. In the script I first set the variables to 0, and then increment them for the various rectangles.<\/figcaption><\/figure>\n\n\n\n<p>Then I incremented the vertical starting point and made the second row of rectangles. That worked also, so I made all four rows. After testing it, I saved the canvas and began inserting the 24 photos into the document.<\/p>\n\n\n\n<p>I only need two master masks for this project, and one is the inverse of the other, so I only created one checkerboard.<\/p>\n\n\n\n<p>After I dragged a new photo onto the canvas, which results in that image being on a new layer, I selected one or the other mask, then created a layer mask for the photo, isolating it from the adjacent images (one only has to avoid those whose corners touch). The next photo used the opposite mask, and gradually I built the collage with its 24 images, 12 using the positive mask, the other 12 using the negative.<\/p>\n\n\n\n<p><strong>Note:<\/strong> It\u2019s important to <em>unlink<\/em> the image from its mask on each layer so that the mask stays put while you move the photo around inside it. This is done by clicking on the chain icon between the image and its mask in the Layers menu.<\/p>\n\n\n\n<p>The photos are very consistent, with the mountain appearing almost exactly in the same location in each image (I use another script I wrote in 2016 to crop the images to the correct ratio, and to put the mountain top in the same position from image to image.<\/p>\n\n\n\n<p>It took several hours to build the final collage, and it is pixel-perfect. There is not a single corner that is not correct; no hairlines show; no gaps exist. It is&nbsp;<em>absolutely accurate.<\/em><\/p>\n\n\n\n<p>The&nbsp;<em>Scripting Guide<\/em>&nbsp;published by Adobe gives (mostly) clear examples and instructions for controlling <em>Photoshop<\/em>. They also publish similar guides for <em>Illustrator<\/em> and <em>InDesign.<\/em> Automating tasks and producing visuals with absolute precision are the byproducts of the scripting supplements for these three applications. The syntax can be confusing at times, but I have always been able to find online examples that clear-up ambiguities when I get stuck in an <em>AppleScript<\/em> program, or an Adobe-specific implementation of AppleScript.<\/p>\n\n\n\n<p>Regardless of your choice of programming languages \u2013 <em>VisualBasic, Javascript<\/em> or <em>AppleScript<\/em> \u2013 if you commit to learning how to automate with scripting, the benefits are many. It takes a bit of time to master it, but the results are worth the effort.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I pride myself on being a competent user of Adobe Photoshop. I know how to make my photos look good by sliding sliders and pushing buttons and applying filters and adding Layer Effects and all sorts of&nbsp;visual&nbsp;things. Occasionally I need &hellip; <a href=\"https:\/\/thelawlers.com\/Blognosticator\/?p=3008\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,18,10,27,25],"tags":[651,423,405,524,597,64,80,862,861,108],"class_list":["post-3008","post","type-post","status-publish","format-standard","hentry","category-art","category-imposition-and-pagination","category-photography","category-photoshop-techniques","category-software-2","tag-adobe-photoshop","tag-applescript","tag-applescripting-photoshop","tag-bishop-peak","tag-bishop-peak-portrait-project","tag-brian-lawler","tag-cal-poly","tag-image-collage-technique","tag-o","tag-the-blognosticator"],"_links":{"self":[{"href":"https:\/\/thelawlers.com\/Blognosticator\/index.php?rest_route=\/wp\/v2\/posts\/3008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thelawlers.com\/Blognosticator\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thelawlers.com\/Blognosticator\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thelawlers.com\/Blognosticator\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thelawlers.com\/Blognosticator\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3008"}],"version-history":[{"count":15,"href":"https:\/\/thelawlers.com\/Blognosticator\/index.php?rest_route=\/wp\/v2\/posts\/3008\/revisions"}],"predecessor-version":[{"id":3103,"href":"https:\/\/thelawlers.com\/Blognosticator\/index.php?rest_route=\/wp\/v2\/posts\/3008\/revisions\/3103"}],"wp:attachment":[{"href":"https:\/\/thelawlers.com\/Blognosticator\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thelawlers.com\/Blognosticator\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thelawlers.com\/Blognosticator\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}