0

I have directories of 100-500 small black & white image files (jpg) that I need to batch invert, from white text on black to black text on white. They will then be input to tesseract-ocr.
I am looking for software or some method on Ubuntu 16.04 to do this. Maybe tesseract has such a function?

I found this: How to invert colors in multiple images using Gimp in terminal? which seems to be the right idea, but it is an old answer and the link is dead. When I searched on "Batch Image Manipulation Plugin" I found it but only for Windows (I only have Gimp installed on Ubuntu).

I have seen some solutions using imagemagick, but it doesn't seem to run on my system, so I would rather avoid those for now.

This doesn't seem so difficult, but so far I haven't found anything that can do batch processing. I am not a Linux expert so if it isn't a GUI or command line solution I may need a detailed instruction.

A Windows 7 solution may be ok if no other option.

I am looking for a no-cost solution if possible.

3
  • Standard JPEGs use three 8-bit channels. Are you sure you really have 1-bit (black-and-white) or 2-bit (four-color) JPEGs? They may be stored as 24 bits (three 8-bit channels) even if they appear monochrome. Commented Dec 18, 2018 at 21:55
  • @Kodiologist You are right. I deleted that part. The basic point was that they are b/w and not gray scale.
    – user3169
    Commented Dec 18, 2018 at 23:13
  • Okay. Because of JPEG compression artifacts, they'll have a lot of gray pixels even though they're supposed to be monochrome, so I would recommend thresholding them to true black and white before inverting, and make sure to save in a lossless format such as PNG. An indexed PNG will probably be smaller than the original JPEG. Commented Dec 19, 2018 at 0:45

2 Answers 2

1

If you are happy with python and have PIL or Pillow installed then the answers to https://stackoverflow.com/questions/2498875/how-to-invert-colors-of-image-with-pil-python-imaging is a way to go.

You can add in a glob to find all of your images.

Something like: from PIL import Image from PIL import ImageOps from glob import glob import os os.chdir('Documents/') # Obviously this needs to be the top directory of your image tree file_list = glob("**/*.jpg", recursive=True) # Get the filenames outdir = "Converted" # You can set this to what you like for fname in file_list: # For each filename image = Image.open(fname) # Read it as an image image=image.convert('L') # Make sure it is gray-scale image=ImageOps.invert(image) # invert it newname = os.path.join(outdir, os.path.split(fname)[-1]) # Create a new name print("Saving Inverted", fname, "as", newname) image.save(newname) # Save it

4
  • I installed python-pil and tried the script in the top answer, but got an error. I put the result on Pastebin. Maybe I'm missing something? Also I would need to know how to code the loop.
    – user3169
    Commented Dec 19, 2018 at 3:51
  • @user3169 Added working code that just needs the directory names changed to fit you ones, note that it assumes that each filename is unique, it does not overwrite the existing files. Commented Dec 19, 2018 at 6:06
  • Thanks. It works as advertised. Due to the number of commands, I would like to consider a shell script to run it. I wrote one (basically yours above with #!/bin/python3.5 as the first line), marked it as executable but no run option on double-click and couldn't run in terminal. No doubt I am doing something wrong.
    – user3169
    Commented Dec 19, 2018 at 20:32
  • The recommended shebang is #!/usr/bin/env python3 as the first line a #encoding utf-8 second line is usually recommended Commented Dec 19, 2018 at 23:12
2

ImageMagick is my/the goto for all batch processing of images.

  • Open Source
  • Cross Platform

Using with the Linux find command, (Warning: This will invert in-place every image it finds):

cd the_top_dir_of_your_image_tree
find . -name "*.jpg" -exec magick mogrify -negate {};

Note that some versions of ImageMagick install convert and mogrify as distinct tools while others have a magick with sub-commands.

8
  • That was my first thought as well; but does it support "invert"? If so, could you please include this with your answer?
    – Izzy
    Commented Dec 18, 2018 at 6:49
  • Look at imagemagick.org/Usage/color_mods/#negate Commented Dec 18, 2018 at 18:20
  • Perhaps. I have never used ImageMagick before, but it might be overkill for what I am trying to do. At the least I would need some detailed instructions how to. I can ask that on SO if it would be a more appropriate site.
    – user3169
    Commented Dec 19, 2018 at 0:42
  • 1
    @user3169 , Ring gave you a self sufficient pointer to the docs with an example right there, it is as simple as convert blackandwhite.jpg -negate whiteandblack.jpg for one file . The only additional thing you need is to loop over your files and feed them to convert. Look for examples of find + xargs Commented Dec 19, 2018 at 1:27
  • @AnonymousLurker OK, I got it. Guess I was trying to make it more complicated than it was. All I need now is to figure out how to loop through the files. Sorry, I am computer literate but not a scripting expert. I may need more help.
    – user3169
    Commented Dec 19, 2018 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.