Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Replace color with another one pixel by pixel.

License

NotificationsYou must be signed in to change notification settings

turakvlad/replace-color

Repository files navigation

replace-color replaces color with another one pixel by pixel. Especially this will be helpful if you want to remove the watermarks from the images. This package is built on top ofJimp.

Install

npm install --save replace-color

Basic usage

replace-color supports both Node.js error-first callbacks and promises. The package returns a Jimp's instance which you can use to execute some otherimage manipulations methods or save it with Jimp'swrite method.

Node.js error-first callback example

constreplaceColor=require('replace-color')replaceColor({image:'./input.jpg',colors:{type:'hex',targetColor:'#FF0000',replaceColor:'#FFFFFF'}},(err,jimpObject)=>{if(err)returnconsole.log(err)jimpObject.write('./output.jpg',(err)=>{if(err)returnconsole.log(err)})})

Promise example

constreplaceColor=require('replace-color')replaceColor({image:'./input.jpg',colors:{type:'hex',targetColor:'#FF0000',replaceColor:'#FFFFFF'}}).then((jimpObject)=>{jimpObject.write('./output.jpg',(err)=>{if(err)returnconsole.log(err)})}).catch((err)=>{console.log(err)})

API

replaceColor(options, [callback])

  • optionsObject (required) - the options.
    • imageBuffer |Object |String (required) - an image being processed. It can be abuffer,Jimp's instance, apath to an image on your host machine or aURL address to an image on the internet. Please, take a look at thetests to understand all these options.
    • colorsObject (required) - the colors.
      • typeString (required) - atargetColor andreplaceColor type. Supported values arehex andrgb.
      • targetColorString |Array (required) - a color you want to replace. A 7-symbol string in case ofhex type (e.g.#000000,#FFFFFF). An array of3 integers from0 to255 in case ofrgb type (e.g.[0, 0, 0],[255, 255, 255]).
      • replaceColorString |Array (required) - a new color which will be used instead of atargetColor color. A 7-symbol string in case ofhex type (e.g.#000000,#FFFFFF). An array of3 integers from0 to255 in case ofrgb type (e.g.[0, 0, 0],[255, 255, 255]). You can also define a transparent channel for areplaceColor color. To achieve this, you can use a 9-symbol string in case ofhex type (e.g.#00000000,#FFFFFFFF). Based onthis Stack Overflow answer, an alpha channel is controlled by the first pair of digits in a hex code (e.g.,00 means fully transparent,7F means 50%,FF means fully opaque). Also, you can use an array of4 integers in case ofrgb type. The first3 integers must be from0 to255 and the last one must be from0 to1 (e.g.,0 means fully transparent,0.5 means 50%,1 means fully opaque).
    • formulaString (optional) - one of the three formulas to calculate thecolor difference. Supported values areE76,E94 andE00. The default value isE00 (the best algorithm).
    • deltaENumber (optional) - adeltaE value which corresponds to aJND. The default value is2.3. Please, read more aboutdeltaEhere. Generaly speaking, if the processed by thereplace-color package image still has the watermarks, you should increase thedeltaE value.
  • callbackFunction (optional) - a Node.js error-first callback.

Examples

Remove a watermark

Let's try to remove a watermark fromthis picture.

constreplaceColor=require('replace-color')replaceColor({image:'https://i.imgur.com/XqNTuzp.jpg',colors:{type:'hex',targetColor:'#FFB3B7',replaceColor:'#FFFFFF'},deltaE:20}).then((jimpObject)=>{jimpObject.write('./output.jpg',(err)=>{if(err)returnconsole.log(err)})}).catch((err)=>{console.log(err)})

Result

Example

Change a background color from a green to a blue one

Let's try to change a background color forthis picture.

constreplaceColor=require('replace-color')replaceColor({image:'https://i.imgur.com/aCxZpaq.png',colors:{type:'hex',targetColor:'#66AE74',replaceColor:'#63A4FF'},deltaE:10}).then((jimpObject)=>{jimpObject.write('./output.png',(err)=>{if(err)returnconsole.log(err)})}).catch((err)=>{console.log(err)})

Result

Example

Change a background color from a green to a transparent one (usinghex type)

Let's try to change a background color forthis picture.

constreplaceColor=require('replace-color')replaceColor({image:'https://i.imgur.com/aCxZpaq.png',colors:{type:'hex',targetColor:'#66AE74',replaceColor:'#00000000'},deltaE:10}).then((jimpObject)=>{jimpObject.write('./output.png',(err)=>{if(err)returnconsole.log(err)})}).catch((err)=>{console.log(err)})

Result

Example

Change a background color from a green to a 50% transparent green (usingrgb type)

Let's try to change a background color forthis picture.

constreplaceColor=require('replace-color')replaceColor({image:'https://i.imgur.com/aCxZpaq.png',colors:{type:'rgb',targetColor:[102,174,116],replaceColor:[102,174,116,0.5]},deltaE:10}).then((jimpObject)=>{jimpObject.write('./output.png',(err)=>{if(err)returnconsole.log(err)})}).catch((err)=>{console.log(err)})

Result

Example

Error handling

To indicate thereplace-color's errors you should use theerr instanceof replaceColor.ReplaceColorError class.

replaceColor({},(err,jimpObject)=>{if(errinstanceofreplaceColor.ReplaceColorError){//  A replace-color's error occurred.}elseif(err){// An unknown error occurred.}// Everything went fine.})

Areplace-color'serror instance has thecode andfield properties. For now, the package has two codes:PARAMETER_INVALID andPARAMETER_REQUIRED. Thefield property shows which exact property was not passed or is invalid using the glob notation (e.g.options.colors.type). Please, take a look at thetests to see all the possible cases.

License

MIT


[8]ページ先頭

©2009-2025 Movatter.jp