Jimp: How to convert image using JIMP to black and white image

Created on 14 Dec 2016  路  4Comments  路  Source: oliver-moran/jimp

First of all, thank you for the fantastic efforts in building this library. How can I convert an image using JIMP to black and white, I am not talking about grayscale but about binary image.

Most helpful comment

Better late than never, I found this little gem.. Maybe should be included in Jimp, but easy to add.
https://www.npmjs.com/package/floyd-steinberg

var Jimp = require("jimp")
,floydSteinberg = require('floyd-steinberg')
, filename = process.argv[2]

Jimp.read(filename, function (err, image) {
if (err) throw err;
image.autocrop().scaleToFit(256, 256)
//.rgba(false).greyscale().contrast(1).posterize(2)
image.bitmap = floydSteinberg(image.bitmap)
image.write("out.png")
})

All 4 comments

I believe it would work if you did .grayscale() and then .contrast(1)

http://codepen.io/strandedcity/pen/QdEYYX?editors=1000#0

.posterize(2) also works, but since there are currently no controls for the threshold, you'd frequently end up with an all-black or all-white image. By using contrast instead, your threshold ends up right in the middle.

Closing issue, please comment if that's not working for you.

Better late than never, I found this little gem.. Maybe should be included in Jimp, but easy to add.
https://www.npmjs.com/package/floyd-steinberg

var Jimp = require("jimp")
,floydSteinberg = require('floyd-steinberg')
, filename = process.argv[2]

Jimp.read(filename, function (err, image) {
if (err) throw err;
image.autocrop().scaleToFit(256, 256)
//.rgba(false).greyscale().contrast(1).posterize(2)
image.bitmap = floydSteinberg(image.bitmap)
image.write("out.png")
})

I use the output as input to Tesseract-OCR. I found .rgba(false) together with .greyscale() gave better OCR results, and results were better with without .contrast()

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jBernavaPrah picture jBernavaPrah  路  4Comments

laino picture laino  路  5Comments

Jimbly picture Jimbly  路  5Comments

chancein007 picture chancein007  路  5Comments

PainKKKiller picture PainKKKiller  路  5Comments