Guest User

Untitled

a guest
Jan 22nd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. require 'rmagick'
  2. require 'open3'
  3.  
  4. def pixel_at(images, x, y)
  5. values = images.map do |image|
  6. color = image.pixel_color(x, y).red
  7. color < 257 * 150 ? 0 : 1
  8. end
  9.  
  10. res = values.reduce &:|
  11.  
  12. res > 0 ? Magick::Pixel.from_color('white') : Magick::Pixel.from_color('black')
  13. end
  14.  
  15. begin
  16. i = 134
  17. images = []
  18.  
  19. loop do
  20. images.shift
  21. while (images.length < 3)
  22. images.push(
  23. Magick::Image.read("screenshots/screenshot_#{i + images.length}.jpg").first.
  24. quantize(256, Magick::GRAYColorspace).
  25. edge.
  26. negate
  27. )
  28. end
  29.  
  30. dimensions = {
  31. cols: images.first.columns,
  32. rows: images.first.rows
  33. }
  34.  
  35. processed_image = Magick::Image.new(dimensions[:cols], dimensions[:rows])
  36.  
  37. 0.upto(dimensions[:cols] - 1) do |x|
  38. 0.upto(dimensions[:rows] - 1) do |y|
  39. processed_image.pixel_color(x, y, pixel_at(images, x, y))
  40. end
  41. end
  42.  
  43. processed_path = "processed/screenshot_#{i}.jpg"
  44. processed_image.write(processed_path)
  45.  
  46. stdout, stderr, status = Open3.capture3("tesseract #{processed_path} stdout")
  47.  
  48. text = stdout.strip
  49.  
  50. if text && (text = text.gsub("\n", "")) != ""
  51. puts text
  52. else
  53. print "." if i % 10 == 0
  54. end
  55.  
  56. i += 1
  57.  
  58. image = nil
  59. next_image = nil
  60. end
  61. rescue Magick::ImageMagickError => e
  62. puts "DONE! Processed #{i} images"
  63. end
Add Comment
Please, Sign In to add comment