Why Photoshot Retouch Returns Layers and Masks
Say you use an automatic retouch to remove a blemish and soften an under-eye shadow. The skin looks better, but you want a little of the shadow back. In Photoshot Retouch, you can turn down just the shadow correction because the two corrections can come back as separate layers, like transparent sheets laid over the original photo. A mask controls how much of a layer shows through in each spot.
What we will build in this series
Photoshot Retouch is a photo-retouching tool that connects the same Python engine to a web editor and Adobe Photoshop. This 24-part series starts with a tiny image and ends with an installable Windows build. I call the approach vibe coding because I start by describing a result I want to see, use AI to help build it, and check the result on a concrete example before moving on.
- Parts 1–4. Follow the full workflow, save a first masked PNG in Python, then build the face analysis and the layer engine.
- Parts 5–17. Build 14 retouching tools for skin, eyes, teeth, backgrounds, clothing, and details such as stray hairs and reflections on glasses.
- Parts 18–21. Connect the engine to the web editor and Photoshop. Add Undo, presets, batch processing, and Photoshop document (PSD) export.
- Parts 22–24. Build the Windows installer, document the quality checks, and reproduce the workflow on a clean PC.
Where the checklist came from
At the start of the project, I reviewed Retouch4me, an existing Photoshop retouching panel, using its public product documentation and output examples. I turned what I found into a numbered development checklist. Each item names a behavior to investigate, describes how it could work in Photoshot, and records what to verify. The numbers are positions in that research document. Retouch4me panel overview · Layered-output examples
Part 1 follows items 52–57, which cover running several tools in order and handing the result back to the editor. Item 52 covers tool order. Items 53–55 cover separate layers, groups, and a single merged correction layer. Items 56–57 cover how the new layer blends with the original and which settings can still be changed after retouching. The mapping below shows which GitHub components and Photoshop APIs support each item.
Here is the route through the code for one portrait. The patch is the rectangle containing the changed pixels. The mask is black wherever the original should show through, white wherever the full correction should show, and gray wherever the two should be mixed.
Input · Freeze the source and tool order
The Photoshot web editor renders the visible image from a standard 8-bit RGB document and reads the composite as RGBA. In the Photoshop plugin, readPixels() uses UXP’s getPixels() to read the visible composite or one selected pixel layer from an 8- or 16-bit RGB document. It requests sRGB pixels and saves the returned sourceBounds, document ID, and history state. Those details let the plugin position the patch and check the document again when the result arrives. Adobe Imaging API
The client creates a job with the image dimensions, channel count, sample depth, output mode, and an ordered tools[] array. In the Photoshop client, cloud.js sends the raw image bytes in 8 MiB chunks, commits the upload, and polls the job for a result. The Python API checks the image contract and each tool’s settings. This gives the worker the same instructions regardless of which editor created the job.
Processing · Keep the source and build a correction
In service/engine.py, process() reads input.bin through a read-only NumPy memory map, converts RGB to normalized floating-point values, and gives current its own working copy. A second copy, with a long edge of at most 1,400 pixels, provides the input for region analysis. The first tool that asks for analysis triggers MediaPipe’s Face Landmarker and Image Segmenter. Analysis.data then caches the landmark and region results for the remaining tools in the job. NumPy memory maps · MediaPipe Image Segmenter
Each correction tool receives the current image, the shared region data, and its own settings. It returns a candidate image and an application mask. Heal, for example, uses OpenCV’s Lab color conversion and connected-component measurements to help locate small candidates within protected skin regions. The common engine compares the candidate with the input it received from the stack. For a tool that works on the smaller image, it resizes the difference and the mask to the original dimensions. Fine-detail tools such as Eye Vessels and Fabric calculate the pixel correction at source resolution. OpenCV connected components · OpenCV resize
The common compositing step brings those two outputs together. The engine suppresses near-zero changes, excludes partially transparent and fully transparent RGBA pixels from correction, and quantizes the mask to 8 bits. It encodes the patch at the input sample depth, then advances current using the same mask values that the output layer carries. That makes the next tool read the image with the earlier corrections applied. W3C source-over compositing
delta = candidate - base if native_detail else full(candidate - base)
mask = np.clip(small_mask if native_detail else full(small_mask), 0, 1)
if opaque is not None:
mask *= opaque
mask *= np.max(np.abs(delta), axis=2) >= 0.002
mask = encode(mask, 8).astype(np.float32) / 255
patch = encode(current + delta, depth).astype(np.float32) / maximum
current += (patch - current) * mask[..., None]
service/engine.py. In the full function, the last line runs when the mask has a nonzero value.| Synthetic 8-bit example | Keep source | Half coverage | Full coverage |
|---|---|---|---|
| Starting RGB value per channel | 80 | 80 | 80 |
| Candidate RGB value per channel | 120 | 120 | 120 |
| Mask weight | 0 | 0.5 | 1 |
| Visible RGB value per channel | 80 | 100 | 120 |
Output · Store the correction as a layer
record() finds the bounding rectangle of the nonzero mask and writes the RGB data and mask for that rectangle as two assets. The manifest carries the patch’s x, y, width, height, tool name, group, blend mode, and visibility. Two pixels at x = 1 can therefore travel as a two-pixel patch even when the source image has three pixels. An empty mask leaves the worker without a correction patch to return.
Layered output keeps the patch from each applied correction. In the three-pixel example the layer carries RGB values [120, 120] with mask bytes [128, 255]. Merged output records the final changed region using the already-composited RGB values [100, 120] and mask bytes [255, 255]. Both reconstruct the same visible values in this example. When a job requests Skin Mask, the engine can also return a hidden utility layer in either mode. Photoshot’s correction patches use Normal blend mode. Retouch4me’s examples use Soft Light for some tools, including Dodge Burn, Skin Tone, and Portrait Volumes, so their layer values have a different meaning when composited. Retouch4me blend-mode examples
On the way back, the Photoshop adapter adds the patch coordinates to the captured source origin. paint() puts the RGB bytes into a new pixel layer and writes the grayscale bytes into that layer’s user mask at the same position. apply() checks the document state again, creates the result group, and commits its changes as one history entry. The web editor uses the same result assets to create an image layer and a raster mask. A tool’s strength can change the correction it calculates. Layer opacity scales the contribution of the returned layer after processing. Adobe layer masks · Adobe history suspension
How GitHub sources connect to checklist items 52–57
MediaPipe, OpenCV, and NumPy are dependencies used by the Python engine. Adobe’s public UXP sample repository served as a panel integration reference. Photoshop itself supplies the host APIs that the plugin calls to create layers, write pixels, and set layer properties. Adobe UXP samples
| Checklist item | Library feature or host API | What Photoshot does with it |
|---|---|---|
| 52 · Run tools in order | MediaPipe Face Landmarker and Image Segmenter supply landmarks and region confidence masks. Landmarker source · Segmenter source | service/regions.py adapts and shares the results. service/engine.py runs the chosen tools in request order and passes the updated image to the next tool. |
| 53 · Return separate layers | OpenCV finds the nonzero mask pixels and their bounding rectangle with findNonZero() and boundingRect(). Adobe Imaging writes the patch and mask into Photoshop. OpenCV array operations · OpenCV shape operations · Adobe Imaging API | The engine crops each correction and returns its coordinates. plugin/photoshop.js rebuilds it as a named layer with its own mask. |
| 54 · Group results | Adobe Photoshop UXP’s createLayerGroup() provides the folder-like layer group. Adobe Document API | Photoshot assigns categories such as Skin and Details and uses the returned category and the tool order to place results in the layer stack. |
| 55 · Return one merged correction | NumPy arrays support the pixel-by-pixel update and comparison with the source. NumPy repository | The engine records the accumulated correction with a mask for changed pixels. Skin Mask can also return a hidden utility layer when requested. |
| 56 · Blend with the photo | NumPy provides the array operations for mask-weighted blending. Photoshop provides the Normal layer mode. NumPy repository · Adobe blend modes | The engine composites the correction with its mask, and the plugin creates the matching Normal layer. Retouch4me’s public examples also show modes such as Soft Light for some tools. Retouch4me examples |
| 57 · Strength and opacity | Tool strength is a Photoshot setting. Layer opacity is a built-in Photoshop property. Adobe Layer API | Use strength before running a tool to change the calculated correction. Use layer opacity after the run to fade the whole correction layer. |
Tool-specific uses of OpenCV, such as color conversion, connected-component measurements, and the Telea inpainting in Heal’s mole path, help calculate the correction in item 52. The individual tool articles return to those operations in context. Connected-components source · Inpainting source
What I checked
I ran the current Python engine with the three synthetic pixels above. Reconstructing the layered and merged outputs produced [80, 100, 120] in both cases. A separate 16-bit RGBA input showed that a half-transparent pixel stayed outside the returned patch. Those checks exercise the engine and its returned bytes. The PSD reopen and Undo checks belong to the Photoshop host test. Part 2 starts from an empty folder and writes the first masked PNG with a small Python script.