Search code examples
ruby-on-railsrubyrmagick

Modifying Brightness/Contrast of Image with RMagick


I'm trying to write a script to take a PDF and increase the brightness/contrast such that my scanned handwritting is actually readable. I am able to do this with Photoshop (which is really tedious), but I can't figure out what RMagick methods to use to produce a similar result.

Any pointers? Thanks for the help.


Solution

  • I ended up using Fred's ImageMagick scripts to make the handwriting readable see : http://www.fmwconcepts.com/imagemagick/

    I ended up not using RMagick for this part; instead I just called imagemagick's convert terminal command from ruby. It is a little bit convoluted - but it worked for me. Some sample code is below:

      localthres_script = '~/Downloads/test/localthresh.sh' # CONSTANT LOCATION
    
      params = '-m 3 -r 25 -b 20 -n yes'
    
      pdf = Magick::ImageList.new("#{dir}/#{pdf_name_wo_ext}.pdf")
      i=1
      pdf.each do |page|
        image_name = "#{pdf_name_wo_ext}_#{i}"
        puts "==> Enhancing images..."
        %x[#{localthres_script} #{params} #{dir}/#{image_name}.png #{dir}/PDF_SCRIPT/enhanced/#{image_name}.png]
        puts "==> Moving images..."
        %x[mv #{dir}/#{image_name}.png #{dir}/PDF_SCRIPT/original/#{image_name}.png]
        i = i+1
      end # each
    

    I know this isn't the cleanest code, but it worked for me.