augment-image
Image augmentation library and cli for machine learning tasks
![npm Package Version](https://img.shields.io/npm/v/augment-image)
Features
- Rich combination of customizable image augmentation
- background color
- scale
- crop
- shear
- rotate
- gray-scale
- flipX
- flipY
- blur
- Typescript support
- Support usage from cli
Installation
npm install augment-image
You can also install augment-image
with pnpm, yarn, or slnpm
Note that optional dependency should not be disabled, sharp installs the os-specific native package as optional dependencies.
Usage Example
import {
aggressiveFilterGroupsOptions,
buildFilterGroups,
scanDirectory,
} from 'augment-image'
async function main() {
let filterGroups = buildFilterGroups({
background: ['#ffffffff'],
scale: [
[0.5, 0.5],
[1.0, 0.5],
[2.0, 2.0],
],
crop: [
[Infinity, Infinity],
[100, 100],
[50, 50],
],
shear: [
[0, 0],
[-8, 0],
[+8, 0],
[0, -8],
[0, +8],
],
rotate: [0, 8, -8, 16, -16],
grayscale: 'both',
flipX: true,
blur: [0, 1, 2],
})
filterGroups = buildFilterGroups(aggressiveFilterGroupsOptions)
await scanDirectory({
srcDir: './images/raw',
outDir: './images/augmented',
filterGroups,
verbose: true,
})
}
main().catch(e => console.error(e))
Typescript Signature
core functions and types
import { Sharp } from 'sharp'
export function augmentImage(
image: Sharp,
filterGroups: FilterGroup[],
): AsyncGenerator<Sharp, void, unknown>
export function scanDirectory(options: {
srcDir: string
outDir: string
filterGroups: FilterGroup[]
/** @description default `true` */
verbose?: boolean
}): Promise<{
fileCount: number
}>
export function buildFilterGroups(
options: BuildFilterGroupsOptions,
): FilterGroup[]
export type BuildFilterGroupsOptions = {
background?: string[]
scale?: [w: number, h: number][]
crop?: [w: number, h: number][]
shear?: [x: number, y: number][]
rotate?: number[]
grayscale?: 'always' | 'never' | 'both'
flipX?: boolean
flipY?: boolean
blur?: number[]
}
export let aggressiveFilterGroupsOptions: BuildFilterGroupsOptions
type FilterGroup = {
name: string
variants: Filter[]
}
type Filter = {
(image: Sharp): Sharp[] | Sharp | Promise<Sharp[] | Sharp>
}
helper functions
import { Sharp } from 'sharp'
export function range(args: {
/** @description inclusive */
from: number
/** @description inclusive */
to: number
/** @description can be positive or negative */
step: number
}): number[]
export function rangeAround(args: {
center: number
/** @description inclusive */
range: number
/** @description can be positive or negative */
step: number
}): number[]
export function expandCropSize(
size: number[],
): number[][]
License
This project is licensed with BSD-2-Clause
This is free, libre, and open-source software. It comes down to four essential freedoms [ref]:
- The freedom to run the program as you wish, for any purpose
- The freedom to study how the program works, and change it so it does your computing as you wish
- The freedom to redistribute copies so you can help others
- The freedom to distribute copies of your modified versions to others