Skip to content

Instantly share code, notes, and snippets.

@murilohns
Created July 30, 2018 20:12
Show Gist options
  • Save murilohns/4d3e7d2748fd1d953e5d3a601717c4ea to your computer and use it in GitHub Desktop.
Save murilohns/4d3e7d2748fd1d953e5d3a601717c4ea to your computer and use it in GitHub Desktop.
const Promise = require('bluebird')
const Tesseract = require('tesseract.js')
const Jimp = require('jimp')
const { promisify } = require('util')
const url = 'https://i.imgur.com/dPJ8py9.png'
const getImage = async (url, scale = 1) => {
return await Jimp.read(url)
.then( async image => {
let resizedImage = await image.scale(scale);
return await new Promise((resolve, reject) => {
resizedImage.getBuffer(Jimp.MIME_PNG, (err, data) => {
resolve(getImageText(data))
})
})
})
}
const getImageText = async (image) => {
const imageText = await Tesseract.recognize(image, {
lang: 'por',
//textord_restore_underlines: 5,
//tessedit_char_whitelist: ['_']
})
return imageText.text
//console.log(imageText.text);
}
Promise.all([getImage(url), getImage(url, 2.5)])
.then((imagesText) => {
imagesText.map(text => {
console.log(text)
})
})
@vagrantsn
Copy link

const Promise = require('bluebird')
const Tesseract = require('tesseract.js')
const Jimp = require('jimp')

const url = 'https://i.imgur.com/dPJ8py9.png'

const getImage = (url, scale = 1) =>
  Jimp.read(url)
    .then( image => image.scale(scale) )
    .then( resultantImage => new Promise(resolve =>
      resultantImage.getBuffer(
        Jimp.MIME_PNG, (err, data) => resolve(getImageText(data))
      )
    ))

const getImageText = async (image) => {
  const imageText = await Tesseract.recognize(image, {
    lang: 'por',
    //textord_restore_underlines: 5,
    //tessedit_char_whitelist: ['_']
  })
  return imageText.text
  //console.log(imageText.text);
}

Promise.all([getImage(url), getImage(url, 2.5)])
  .then((imagesText) => {
    imagesText.map(text => {
      console.log(text)
    })
  })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment