TyporaTrim is a PowerShell script that leverages ImageMagick to process images by trimming whitespace and adding intelligent borders. Perfect for preparing screenshots and diagrams for documentation.
TyporaTrim processes images in two intelligent steps:
- Trim: Removes extra whitespace around the image content using ImageMagick's
-trim +repageoptions. - Add Border: Adds a customizable border on all sides (default: 60px).
Smart Border Coloring:
- If you supply a border colour via the
-BorderColorparameter, that colour is used. - If not, the script automatically extracts the top-left pixel's colour from the input image and uses it as the border colour.
| Before | After |
![]() |
![]() |
- ImageMagick: Required for image processing
- PowerShell: Version 5.1 or later
-
Install ImageMagick
Choose one of the following methods:
Scoop:
scoop install imagemagick
Winget:
winget install ImageMagick.ImageMagick
Manual Download: Download from ImageMagick Downloads and ensure
magickis available in your system's PATH. -
Clone the repository
git clone https://github.com/charlesroper/TyporaTrim.git cd TyporaTrim
-
Verify ImageMagick installation
magick -version
-
Open a PowerShell prompt and navigate to the directory containing
TyporaTrim.ps1. -
Run the script with the following syntax:
.\TyporaTrim.ps1 -InputFile <path_to_image> [-BorderColor <hex_colour>] [-BorderWidth <width_in_pixels>]
Parameters:
-InputFile(required): Path to the image you want to process-BorderColor(optional): Hex color code for the border (e.g.,"#0d1117")-BorderWidth(optional): Border width in pixels (default: 60)
Note: If you don't specify
-BorderColor, the top-left pixel colour will be automatically detected and used.
Integrate TyporaTrim into your Typora export workflow:
-
In Preferences > Export, select the existing Image export type or create a new custom Image export method.
-
In the After Export section, check Run Command and enter the following command:
pwsh.exe -noprofile [path to your script]\TyporaTrim.ps1 "${outputPath}" -
Check the Show command output option to view the command output. This is useful for verification and can be disabled later.
Using a specified border colour and custom border width:
.\TyporaTrim.ps1 -InputFile "test.png" -BorderColor "#0d1117" -BorderWidth 80Using automatic color detection with default border width (60px):
.\TyporaTrim.ps1 -InputFile "test.png"Processing multiple images with a consistent border:
Get-ChildItem *.png | ForEach-Object { .\TyporaTrim.ps1 -InputFile $_.FullName -BorderColor "#ffffff" }When no border colour is provided, the script extracts the top-left pixel's colour using:
magick input.png -format "%[hex:p{0,0}]" info:Explanation of the command:
-format "%[hex:p{...}]": Tells ImageMagick to output the pixel colour in hexadecimal format.%[hex:p{0,0}]: Retrieves the colour of the pixel at coordinates (0,0):- First
0is the x-coordinate (leftmost column) - Second
0is the y-coordinate (top row)
- First
- The result is the hex colour of the top-left pixel, which is then prepended with
#and used as the border colour.
The processed image is saved in the same directory as the input file with _trimmed appended before the .png extension.
Example:
- Input:
test.png - Output:
test_trimmed.png
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Charles Roper - @charlesroper
Topics: powershell · imagemagick · typora · image-processing · automation

