Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sunshineplan/imgconv
Golang image format convert, resize and watermark.
https://github.com/sunshineplan/imgconv
format-converter golang image-processing pdf-converter resize watermark
Last synced: about 10 hours ago
JSON representation
Golang image format convert, resize and watermark.
- Host: GitHub
- URL: https://github.com/sunshineplan/imgconv
- Owner: sunshineplan
- License: mit
- Created: 2020-09-27T09:21:26.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-01-24T22:41:25.000Z (8 days ago)
- Last Synced: 2025-01-25T07:04:48.728Z (7 days ago)
- Topics: format-converter, golang, image-processing, pdf-converter, resize, watermark
- Language: Go
- Homepage:
- Size: 364 KB
- Stars: 118
- Watchers: 3
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Image Converter
[![GoDev](https://img.shields.io/static/v1?label=godev&message=reference&color=00add8)][godev]
[![Go](https://github.com/sunshineplan/imgconv/workflows/Test/badge.svg)][actions]
[![CoverageStatus](https://coveralls.io/repos/github/sunshineplan/imgconv/badge.svg?branch=main&service=github)][coveralls]
[![GoReportCard](https://goreportcard.com/badge/github.com/sunshineplan/imgconv)][goreportcard][godev]: https://pkg.go.dev/github.com/sunshineplan/imgconv
[actions]: https://github.com/sunshineplan/imgconv/actions "GitHub Actions Page"
[coveralls]: https://coveralls.io/github/sunshineplan/imgconv?branch=main
[goreportcard]: https://goreportcard.com/report/github.com/sunshineplan/imgconvPackage imgconv provides basic image processing functions (resize, add watermark, format converter.).
All the image processing functions provided by the package accept any image type that implements `image.Image` interface
as an input, include jpg(jpeg), png, gif, tif(tiff), bmp, webp(decode only) and pdf.## Installation
go get -u github.com/sunshineplan/imgconv
## License
[The MIT License (MIT)](https://raw.githubusercontent.com/sunshineplan/imgconv/main/LICENSE)
## Credits
This repo relies on the following third-party projects:
* [disintegration/imaging](https://github.com/disintegration/imaging)
* [pdfcpu/pdfcpu](https://github.com/pdfcpu/pdfcpu)
* [hhrutter/tiff](https://github.com/hhrutter/tiff)## Usage examples
A few usage examples can be found below. See the documentation for the full list of supported functions.
### Image resizing
```go
// Resize srcImage to size = 128x128px.
dstImage128 := imgconv.Resize(srcImage, &imgconv.ResizeOption{Width: 128, Height: 128})// Resize srcImage to width = 800px preserving the aspect ratio.
dstImage800 := imgconv.Resize(srcImage, &imgconv.ResizeOption{Width: 800})// Resize srcImage to 50% size preserving the aspect ratio.
dstImagePercent50 := imgconv.Resize(srcImage, &imgconv.ResizeOption{Percent: 50})
```### Image splitting
```go
// Split srcImage into 3 parts horizontally.
imgs, err := imgconv.Split(srcImage, 3, imgconv.SplitHorizontalMode)// Split srcImage into 3 parts vertically.
imgs, err := imgconv.Split(srcImage, 3, imgconv.SplitVerticalMode)
```### Add watermark
```go
// srcImage add a watermark at randomly position.
dstImage := imgconv.Watermark(srcImage, &WatermarkOption{Mark: markImage, Opacity: 128, Random: true})// srcImage add a watermark at fixed position with offset.
dstImage := imgconv.Watermark(srcImage, &WatermarkOption{Mark: markImage, Opacity: 128, Offset: image.Pt(5, 5)})
```### Format convert
```go
// Convert srcImage to dst with jpg format.
imgconv.Write(dstWriter, srcImage, &imgconv.FormatOption{Format: imgconv.JPEG})
```## Example code
```go
package mainimport (
"io"
"log""github.com/sunshineplan/imgconv"
)func main() {
// Open a test image.
src, err := imgconv.Open("testdata/video-001.png")
if err != nil {
log.Fatalf("failed to open image: %v", err)
}// Resize the image to width = 200px preserving the aspect ratio.
mark := imgconv.Resize(src, &imgconv.ResizeOption{Width: 200})// Add random watermark set opacity = 128.
dst := imgconv.Watermark(src, &imgconv.WatermarkOption{Mark: mark, Opacity: 128, Random: true})// Write the resulting image as TIFF.
if err := imgconv.Write(io.Discard, dst, &imgconv.FormatOption{Format: imgconv.TIFF}); err != nil {
log.Fatalf("failed to write image: %v", err)
}// Split the image into 3 parts horizontally.
imgs, err := imgconv.Split(src, 3, imgconv.SplitHorizontalMode)
if err != nil {
log.Fatalf("failed to split image: %v", err)
}
}
```