Write Your First Masked PNG
Which pixels should change if an AI-written script is told to brighten one rectangle? Before running the script, pick a pixel and predict its new RGB value. Then open the saved PNG and check your answer.
Part 1 followed a three-pixel correction through the Photoshot Retouch engine. Here we build a small exercise in an empty folder and watch how the pixels, the mask, and the rectangle coordinates affect the result. The goal of this vibe coding journal is to read AI-written code well enough to explain and verify what it does. Part 1 · Read the code by predicting its output
Start with an empty folder
Create a folder for this exercise, extract the ZIP into it, and open PowerShell there. I verified the exercise with Python 3.13.13 on Windows x64. The lock file pins the NumPy, OpenCV, and Pillow wheels by version and hash. Download the exercise ZIP · ZIP checksum
py -3.13 -m venv .venv
.\.venv\Scripts\python.exe -m pip install --require-hashes --only-binary=:all: -r requirements-windows-py313.txt
.\.venv\Scripts\python.exe make_input.py
The starter writes an input.png in the working folder. Use the full run after the pixel prediction. It uses the same generator and writes out/input.png, out/mask.png, and out/output.png. Each PNG is 448 × 288 pixels. The ZIP includes the verified files under expected/. Exercise instructions
Which parts of the checklist are we testing?
The checklist started with a review of Retouch4me’s published retouching examples. I used the observable behavior to write down what Photoshot should implement and how I could check it. This part covers the local PNG exercise within three entries. Retouch4me’s public layer examples
- 120 · First code path. Read a small RGB image, apply an explicit mask, and save the result locally.
- 116 · Blending verification. Predict the contribution of the mask and strength at one pixel.
- 121 · Acceptance checks. Reopen the files and verify the dimensions, the changed region, the zero-strength run, and repeatability.
The Photoshop round trip, automatic detection, and layer reconstruction have their own steps later in the series. This exercise gives us three files and a set of pixel predictions to build on.
120 · Generate the input and mark a rectangle
The starter generates a color ramp so adjacent areas have different RGB values. It also draws a small dark mark into that ramp. The mark gives you something easy to recognize in the input and output.
- InputSynthetic RGB and a rectangle
make_input.py · sample_rgb()The sample is 448 × 288. The region starts at (150, 90) and measures 132 × 104. - ProcessingBuild a mask and apply brightness
first_retouch.py · rectangular_mask() · blend_brightness()OpenCV draws the region. NumPy applies the lift with the mask and strength. - OutputThree PNG files
first_retouch.py · main() · Pillow Image.save()The files record the original image, the selected area, and the resulting image.
Read the shape before reading the pixels
y, x = np.indices((HEIGHT, WIDTH), dtype=np.int32)
ramp = 64 + x // 4 + y // 16
rgb = np.stack((ramp + 22, ramp + 7, ramp - 9), axis=-1)
There are 448 columns, 288 rows, and three color channels. NumPy stores that as (288, 448, 3). The image location (160, 112) is the array element rgb[112, 160], because NumPy reads the row before the column. Using whole-number division, the ramp value at that location is 64 + 160 // 4 + 112 // 16 = 111. The three channel offsets yield [133, 118, 102]. The dark mark is centered elsewhere, so this sample keeps those values. Starter source
Pillow saves the array as a PNG, then the full exercise reopens it and produces an unsigned 8-bit RGB array. Each channel stores an integer from 0 to 255. Full exercise source
Make the selected area explicit
x, y, width, height = roi
mask = np.zeros((HEIGHT, WIDTH), dtype=np.uint8)
# OpenCV includes both corners, so the far corner is one pixel inside.
cv.rectangle(mask, (x, y), (x + width - 1, y + height - 1), 255, cv.FILLED)
return mask
The four numbers in --roi are the left coordinate, top coordinate, width, and height. The default (150, 90, 132, 104) includes the bottom-right pixel at (281, 193). The subtraction of one accounts for OpenCV including both corners. The mask is a 288 × 448 array with a value of 255 in that rectangle and zero elsewhere. In the image, white means apply the correction and black means keep the source value. OpenCV rectangle documentation
116 · Predict one pixel before the script runs
Imagine sliding the strength from zero to full. The selected pixel at (160, 112) starts at [133, 118, 102]. The default lift adds 40 at full strength, so the candidate is [173, 158, 142]. At half strength, what value should the red channel store? Write down your answer before following the calculation below.
- InputColor, mask value, and strength
first_retouch.py · blend_brightness()Start with [133, 118, 102], a white mask pixel, lift 40, and strength 0.5. - ProcessingCalculate the weighted change
NumPy · clip() · broadcastingThe candidate is [173, 158, 142]. The weight is 0.5, so each channel gains 20. - OutputBack to 8-bit RGB
NumPy · rint() · astype(np.uint8)The predicted value is [153, 138, 122]. The saved PNG confirms it.
source = rgb.astype(np.float32) / 255
candidate = np.clip(source + lift / 255, 0, 1)
coverage = mask.astype(np.float32) / 255
weight = (coverage * strength)[..., None]
blended = source + (candidate - source) * weight
return np.rint(blended * 255).astype(np.uint8)
The first line gives the calculation floating-point values between zero and one. The candidate line adds the lift and keeps each channel in that range. The mask becomes a weight of zero or one, then multiplies by the chosen strength. [..., None] adds a length-one channel dimension so NumPy can apply that same weight to red, green, and blue. NumPy broadcasting · NumPy clipping
The red channel moves halfway from 133 to 173, so 133 + (173 − 133) × 0.5 = 153. The same calculation gives green 138 and blue 122. Where the mask is zero, the weighted change is zero for any strength.
The last line converts the result back into values the 8-bit PNG can store. This example operates on the sample’s RGB channel values. The mask selects where the change can show, and strength controls how much of the computed change is applied during this run.
Run the correction after making your prediction
.\.venv\Scripts\python.exe first_retouch.py --strength 0.5 --out half
.\.venv\Scripts\python.exe first_retouch.py
The half-strength PNG is in half/output.png, and the default result is in out/output.png. The next section shows how the verification script reads their exact RGB values.
121 · Check the saved PNGs against your prediction
Try changing one variable at a time. Half strength changes the amount of the lift. Moving the rectangle 20 pixels right changes whether our sample belongs to it. The sample at (160, 112) lands to the left of the new start at x = 170.
- InputSaved outputs from controlled runs
verify.py · run_case() · read_png()Run the default, half, zero, repeat, edge, and shifted cases. - ProcessingCompare actual and expected data
verify.py · check()Reopen the PNGs and compare their size, mask coordinates, pixel values, and saved bytes. - OutputReproducible observations
verification.jsonThe mask covers 13,728 pixels. Zero pixels change outside it, and the repeated files match.
.\.venv\Scripts\python.exe first_retouch.py --strength 0 --out zero
.\.venv\Scripts\python.exe first_retouch.py --roi 170 90 132 104 --out shifted
.\.venv\Scripts\python.exe verify.py --report verification.json
| Run | Prediction at (160, 112) | Measured RGB |
|---|---|---|
| Default | Add 40 to each channel | [173, 158, 142] |
| Half strength | Add 20 to each channel | [153, 138, 122] |
| Zero strength | Keep the source value | [133, 118, 102] |
| Rectangle moved right | Keep the source value at this location | [133, 118, 102] |
The verification script reopens the files as PNG and checks every pixel. It builds its expected rectangle with a NumPy slice, giving it a separate way to check the OpenCV coordinates. It verifies that changing the mask or strength gives the predicted result and compares two default runs byte for byte. Verification source · Measured results and PNG hashes
Read the assertions for the default and zero-strength runs
np.testing.assert_array_equal(change[mask == 0], 0)
np.testing.assert_array_equal(change[mask == 255], 40)
np.testing.assert_array_equal(zero_output, source)
At full strength, the mask selects 132 × 104 = 13,728 pixels. Every one gains 40 per channel in this fixture. The rest have zero change. The test also checks a one-pixel mask at the lower-right corner and confirms that a rectangle extending past the edge is rejected before files are written.
Connect the exercise to Photoshot’s code
The open-source calls handle array operations, a filled rectangle, and file I/O. The exercise decides the region coordinates, the candidate color, and how that candidate is mixed with the source. These are the points where a reader can trace the result through the code.
| Source | Where it appears in Part 2 | Connection to Photoshot Retouch |
|---|---|---|
| NumPy | sample_rgb() builds the RGB array. blend_brightness() clips and blends the values. NumPy source | service/engine.py normalizes input at the requested bit depth and updates the current image with current += (patch - current) * mask[..., None]. |
| OpenCV | rectangular_mask() uses cv.rectangle() to draw the supplied region. OpenCV drawing source | record() uses findNonZero() and boundingRect() to find the extent of a mask and package the correction patch. |
| Pillow | Image.fromarray(), Image.open(), and Image.save() let us read and check the PNGs in the lab. Pillow image API | The engine’s job reads raw RGB or RGBA samples from input.bin. The exercise uses PNGs so each stage can be opened and inspected directly. |
The useful specification for an AI-assisted implementation is small enough to verify. In this case it names the rectangle, the expected RGB at one point, and the number of pixels allowed to change outside the mask. Reading the actual code then means following those conditions through the inputs, calculation, and saved files.