Created
January 21, 2019 22:18
-
-
Save niuage/ad5a498993f7c5fdfed89d921a28ef10 to your computer and use it in GitHub Desktop.
some more dirty experimentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rmagick' | |
require 'open3' | |
def pixel_at(images, x, y) | |
values = images.map do |image| | |
color = image.pixel_color(x, y).red | |
color < 257 * 150 ? 0 : 1 | |
end | |
res = values.reduce &:| | |
res > 0 ? Magick::Pixel.from_color('white') : Magick::Pixel.from_color('black') | |
end | |
begin | |
i = 134 | |
images = [] | |
loop do | |
images.shift | |
while (images.length < 3) | |
images.push( | |
Magick::Image.read("screenshots/screenshot_#{i + images.length}.jpg").first. | |
quantize(256, Magick::GRAYColorspace). | |
edge. | |
negate | |
) | |
end | |
dimensions = { | |
cols: images.first.columns, | |
rows: images.first.rows | |
} | |
processed_image = Magick::Image.new(dimensions[:cols], dimensions[:rows]) | |
0.upto(dimensions[:cols] - 1) do |x| | |
0.upto(dimensions[:rows] - 1) do |y| | |
processed_image.pixel_color(x, y, pixel_at(images, x, y)) | |
end | |
end | |
processed_path = "processed/screenshot_#{i}.jpg" | |
processed_image.write(processed_path) | |
stdout, stderr, status = Open3.capture3("tesseract #{processed_path} stdout") | |
text = stdout.strip | |
if text && (text = text.gsub("\n", "")) != "" | |
puts text | |
else | |
print "." if i % 10 == 0 | |
end | |
i += 1 | |
image = nil | |
next_image = nil | |
end | |
rescue Magick::ImageMagickError => e | |
puts "DONE! Processed #{i} images" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment