{"id":29586687,"url":"https://github.com/duniul/trim-image-data","last_synced_at":"2025-07-20T03:31:20.188Z","repository":{"id":57379926,"uuid":"291478796","full_name":"duniul/trim-image-data","owner":"duniul","description":"💇‍♀️ Function for trimming/cropping surround pixels in an ImageData-instance.","archived":false,"fork":false,"pushed_at":"2024-02-21T12:52:23.000Z","size":574,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-07T06:02:54.661Z","etag":null,"topics":["canvas","crop-image","image","image-data","image-editing","trim-image"],"latest_commit_sha":null,"homepage":"https://trim-image-data.netlify.app","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/duniul.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-30T13:46:32.000Z","updated_at":"2024-07-12T13:35:38.000Z","dependencies_parsed_at":"2022-09-06T03:32:26.330Z","dependency_job_id":"4755691f-7a93-4ccc-84b5-e86cb7124550","html_url":"https://github.com/duniul/trim-image-data","commit_stats":{"total_commits":18,"total_committers":1,"mean_commits":18.0,"dds":0.0,"last_synced_commit":"f608ba1df1aae780225d99651dfa892c0ceffefb"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/duniul/trim-image-data","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duniul%2Ftrim-image-data","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duniul%2Ftrim-image-data/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duniul%2Ftrim-image-data/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duniul%2Ftrim-image-data/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/duniul","download_url":"https://codeload.github.com/duniul/trim-image-data/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duniul%2Ftrim-image-data/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264437639,"owners_count":23608196,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["canvas","crop-image","image","image-data","image-editing","trim-image"],"created_at":"2025-07-20T03:31:10.207Z","updated_at":"2025-07-20T03:31:20.175Z","avatar_url":"https://github.com/duniul.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# trim-image-data\n\n[![](https://img.shields.io/npm/v/trim-image-data?color=brightgreen)](https://www.npmjs.com/package/trim-image-data)\n[![](https://img.shields.io/bundlephobia/minzip/trim-image-data)](https://bundlephobia.com/result?p=trim-image-data)\n\n💇‍♀️ Function for trimming/cropping transparent pixels surrounding an image. Very similar to\n[trim-canvas], but accepts and returns an [ImageData][image-data]-instance instead. Also allows\nspecifying custom colors to trim.\n\n## Demo\n\nhttps://trim-image-data.netlify.app\n\n## Usage\n\n```js\nimport trimImageData from 'trim-image-data';\n\n// trim surrounding fully transparent pixels\nconst trimmedTransparent = trimImageData(imageData);\n\n// trim surrounding white pixels\nconst trimmedWhite = trimImageData(imageData, {\n  trimColor: ([red, green, blue, alpha]) =\u003e {\n    return red === 255 \u0026\u0026 green === 255 \u0026\u0026 blue === 255 \u0026\u0026 alpha === 255;\n  };\n});\n\n// supports passing a RGBA array instead of a callback too\nconst trimmedWhite = trimImageData(imageData, {\n  trimColor: [255, 255, 255, 255]\n});\n\n// trim any pixel with max alpha\nconst trimmedDim = trimImageData(imageData, {\n  trimColor: ([alpha]) =\u003e alpha === 255\n});\n```\n\n## Installation\n\n```sh\n# npm\nnpm install trim-image-data\n\n# yarn\nyarn add trim-image-data\n\n# pnpm\npnpm add trim-image-data\n```\n\n## API\n\n### `trimImageData(imageData, trimOptions)`\n\nCreates a trimmed version of an ImageData-instance. Trims fully transparent pixels by default. Does\nnot mutate the recieved instance.\n\n**Parameters:**\n\n- `imageData` - the ImageData-instance to crop\n\n- `trimOptions` - optional\n\n  - `trimColor([red, green, blue, alpha]) =\u003e boolean` | `trimColor: [r, g, b, a]`  \n    Callback function used to determine if a value should be trimmed or not. Receives an object of\n    RGBA channels and should return a boolean.\n\n    Also accepts a single RGBA-array as a shorthand for a callback that returns `true` if all\n    channels are equal to the corresponding channel in the array.\n\n**Return value:**\n\nA new, trimmed ImageData-instance.\n\n### `getTrimEdges(imageData, trimOptions)`\n\nCalculates the number of pixels to trim from each side of an image, provided as an ImageData object.\nDoes not actually trim the image.\n\nUses the same options and defaults as `trimImageData`.\n\n**Parameters:**\n\n- `imageData` - the ImageData-instance to calculate the trim for\n\n- `trimOptions` - optional\n\n  - `trimColor([red, green, blue, alpha]) =\u003e boolean` | `trimColor: [r, g, b, a]`  \n    Callback function used to determine if a value should be trimmed or not. Receives an object of\n    RGBA channels and should return a boolean.\n\n    Also accepts a single RGBA-array as a shorthand for a callback that returns `true` if all\n    channels are equal to the corresponding channel in the array.\n\n**Return value:**\n\nA new, trimmed ImageData-instance.\n\n## Related packages\n\n- [crop-image-data] - crops ImageData by specified number of pixels. Used as a dependency in\n  `trim-image-data`.\n\n## Credits\n\nA lot of the code in this repo is based on [trim-canvas], I just had a need for the same\nfunctionality without passing a canvas.\n\n[trim-canvas]: https://github.com/agilgur5/trim-canvas\n[image-data]: https://developer.mozilla.org/en-US/docs/Web/API/ImageData\n[crop-image-data]: https://github.com/duniul/crop-image-data\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fduniul%2Ftrim-image-data","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fduniul%2Ftrim-image-data","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fduniul%2Ftrim-image-data/lists"}