A Command-Line Interface (CLI) tool written in Go to resize images and optionally apply a watermark.
- Resize single images or all images in a directory.
- Specify target width OR height (aspect ratio is maintained).
- Optionally apply a watermark image.
- Specify watermark position (e.g., bottom-right, center).
- Specify watermark opacity.
- Supports JPEG and PNG formats.
- Outputs processed images to a specified directory.
- Go (Golang)
- Go Standard Library (
image,image/draw,image/jpeg,image/png,flag,os,path/filepath) golang.org/x/image/draw(for better resizing quality - optional, but recommended)
-
Prerequisites:
- Go (v1.18 or later recommended) installed and configured (https://go.dev/doc/install).
-
Clone the repository:
git clone https://github.com/stex4cs/go-image-processor.git # Use your repo name cd go-image-processor
-
Install Dependencies (Go handles this via modules): The first time you build or run, Go will automatically download necessary dependencies defined in the code (like
golang.org/x/image). -
Build the Executable:
go build -o image-processor .(This creates an executable file named
image-processororimage-processor.exein the current directory).
./image-processor -in <input_path> -out <output_dir> [-width <pixels> | -height <pixels>] [-watermark <watermark_path> -wpos <position> -wopacity <0-1>]
Markdown
Arguments:
-in (Required): Path to the input image file or directory containing images.
-out (Required): Path to the output directory where processed images will be saved.
-width: Target width in pixels. If set, height is calculated automatically. Cannot be used with -height.
-height: Target height in pixels. If set, width is calculated automatically. Cannot be used with -width.
-watermark: (Optional) Path to the watermark image file (PNG recommended for transparency).
-wpos: (Optional, requires -watermark) Watermark position. Supported values: bottom-right (default), bottom-left, top-right, top-left, center.
-wopacity: (Optional, requires -watermark) Watermark opacity (0.0 = fully transparent, 1.0 = fully opaque). Default: 0.5.
Examples:
Resize a single image to 800px width:
./image-processor -in photo.jpg -out ./processed -width 800
Bash
Resize all images in ./input_folder to 600px height:
./image-processor -in ./input_folder -out ./processed -height 600
Bash
Resize and watermark (bottom-right, 70% opacity):
./image-processor -in image.png -out ./output -width 1024 -watermark logo.png -wopacity 0.7
Bash
Resize and watermark (center position):
./image-processor -in image.png -out ./output -width 1024 -watermark logo.png -wpos center
Bash
Supported Formats
Input: JPEG, PNG
Output: JPEG (for .jpg, .jpeg), PNG (for .png)
Watermark: PNG (recommended for transparency), JPEG
Limitations & Future Improvements
Limited format support (add GIF, WEBP, etc.).
Basic watermark positioning.
Sequential processing (could be parallelized using goroutines for directories).
More sophisticated resizing algorithms (e.g., Lanczos). (Using golang.org/x/image/draw helps).
Add options for output quality (JPEG compression).
Implement unit tests.
License
MIT License - see the LICENSE file.
**`go.mod`** (Kreiraj ga komandom `go mod init github.com/stex4cs/go-image-processor` (zameni putanju svojom) ili ga kreiraj ručno)
```mod
module github.com/stex4cs/go-image-processor // Or your actual module path
go 1.21 // Specify your Go version or a recent one
require golang.org/x/image v0.18.0 // Example version, `go build` will add/update this