Skip to content

Instantly share code, notes, and snippets.

@coleww
Created October 31, 2015 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coleww/cbd3a877ffba60c3abcb to your computer and use it in GitHub Desktop.
Save coleww/cbd3a877ffba60c3abcb to your computer and use it in GitHub Desktop.
isProbablyAMeme.js
function isProbablyAMeme(ctx2, w, h) {
// given a ctx, get it's pixel data
var pixels = ctx2.getImageData(0, 0, w, h)
// threshhold filter the pixels. looking for bright white a la bold impact meme text
for (var i = 0; i < pixels.data.length; i += 4) {
var avg = (pixels.data[i] + pixels.data[i + 1] + pixels.data[i + 2]) / 3
var ne = avg > 240 ? 0 : 255
pixels.data[i] = ne
pixels.data[i + 1] = ne
pixels.data[i + 2] = ne
}
var canvas = new Canvas(w, h)
var ctx = canvas.getContext('2d')
ctx.putImageData(pixels, 0, 0)
// run OCR over the threshholded canvas, remove garbage from the detected text
var ocr = Ocrad(canvas).replace(/\W|\_/g, '')
// a handful of misidentified letters/numbers is ok, but not a dozen.
return ocr.length > 12
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment