Scan a photo album with a script from the command line

Clearly there are several graphical interfaces for scanning photos, which provide numerous options. But when the number of photos to be scanned is large and we don't have a scanner that has an automatic sheet feeder, things get tough. In general you have to do a click to scan, another click perhaps at the other end of the window to save ... it takes time. Anyway, a good excuse to use the Command line.

This is a contribution from Rubén, thus becoming one of the winners of our weekly competition: «Share what you know about Linux«. Congratulations Rubén!

I read the articles on Batch Image Processing with GIMP y How to manipulate images from the terminal, just the day my friend Huguito asked me for the photos (which he had lost) of a trip we did in 1989… (4 rolls of 36 photos! and on paper, of course 🙂. It reminded me of this tip to scan photographs that I had forgotten .

The idea is to use a script to do it as fast as possible:

1.- Put a photo on the scanner
2.- Press any key to scan
3.- Repeat the procedure, unless the key pressed is for example an 'n' to exit
4.- The photos will be saved and numbered in a pre-established subdirectory.

What command?

The 'magic' command is scanimage which is part of SANE. SANE is an API that provides standardized access to any imaging device. The Sane API is in the public domain and its source code is available under the GNU General Public License.

Installation is simple. Each distribution brings its packages. For Ubuntu (or its derivatives), it is enough to use synaptic and install sane and sane-utils.

How to use?

How always the best is to write in a terminal 'man scanimage'. However, we will summarize the options we will use.

We open a terminal and run scanimage -L to see the available devices:

scanimage -L

In my case it returns:

device `xerox_mfp: libusb: 001: 005 'is a SAMSUNG ORION multi-function peripheral

This is the result it gave me with my Samsung SCX-4200 multifunction. If you have an MFP and you cannot find the device, make sure the printer is not active, for example with a pending print.

The value 'xerox_mfp: libusb: 001: 003' is used to tell the scanimage command which device to use through the -d option. If only one scanner is connected, this option is not necessary.

When the scanimage command scans, it sends the resulting image to standard output in pnm or tiff format. So to scan we redirect the output to a file. And if we want to see what messages the command is giving, we add the -v option. If we also want to see the percentage of progress of the operation, we add the -p option.

scanimage -v -p> image.tiff

scanimage: scanning image of size 1284x1734 pixels at 24 bits / pixel
scanimage:acquiring RGB frame
scanimage: min / max graylevel value = 69/255
scanimage: read 6679368 bytes in total
Progress: 13.8%

What will it give us if we scanimage –help? It seems obvious, it gives a help on the command. But this command has a peculiarity. At the end of the generic help for the command add the specific parameters that your scanner accepts.

scanimage --help

Usage: scanimage [OPTION] ...

BLA bla....

Options specific to device `xerox_mfp: libusb: 001: 005 ':

standard:
--resolution 75 | 100 | 150 | 200 | 300 | 600dpi [150] Sets the resolution of the scanned image.
--mode Lineart | Halftone | Gray | Color [Color] Selects the scan mode (eg, lineart, monochrome, or color).
--highlight 30..70% (in steps of 10) [inactive] Select minimum-brightness to get a white point
--source Flatbed | ADF | Auto [Flatbed] Selects the scan source (such as a document-feeder).
geometry:
-l 0..215.9mm (in steps of 1) [0] Top-left x position of scan area.
-t 0..297.18mm (in steps of 1) [0] Top-left and position of scan area.
-x 0..215.9mm (in steps of 1) [215.9] Width of scan-area.
-y 0..297.18mm (in steps of 1) [297.18] Height of scan-area.

Type `` scanimage --help -d DEVICE '' to get list of all options for DEVICE.

List of available devices:

xerox_mfp: libusb: 001: 005

From here we can choose the options with the values ​​that we can use.

Example:

Decision

–Resolution 150

Mode

–Mode Color

Now the geometry. This is very useful because we can tell the scanner to only extract the image of one sector (where we will put the photograph), and we save the time of scanning the rest of the surface that we should also cut later with a graphics editor such as Gimp .

-l 0 starts scanning horizontally from 0 mm from the upper left corner of the scanner

-t 0 starts scanning vertically from 0 mm from the upper left corner of the scanner

Note that I chose to put the photo in the corner of the scanner [coordinates (0,0)], since it is easier to place. On my scanner (A4 size) l can go from 0 to 215.9 and t from 0 to 297.18.

Width and Height of the photograph. In my case the photos are 13x18cm:

-x 180 wide

-and 130 high

Therefore it will only scan the part where we put our photo. Of course, if we agree on what is the left, the right, the width, the height, the top and the bottom. It will depend on how you look at your scanner. I suggest testing the coordinates and adjusting them to your needs.

An example of the command could be:

scanimage -d xerox_mfp: libusb: 001: 003 -p --mode Color --resolution 150 -l 0 -t 0 -x 180 -y 130> image.pnm

With these options, we could already build our script.

But before…

Tiff or pnm are uncompressed formats, so our photos will take up huge disk space. This is where the ImageMagick convert command comes in which is explained in How to manipulate images from the terminal.

If we get an image.pnm from the scanner, we can convert it to jpg:

convert image.pnm image.jpg

But before doing so, another trick:

Image.pnm takes up a lot of space and we should delete it after getting our image.jpg. There is an option for the convert command so that instead of taking a file from disk it will directly convert standard input. This is accomplished with a dash - instead of the file:

convert - image.jpg

Since scanimage delivers the scanned image to standard output, we make a "pipeline" and save the processing time it takes to write and then delete the image.pnm file from disk.

scanimage -d xerox_mfp: libusb: 001: 003 -p --mode Color --resolution 150 -l 0 -t 0 -x 180 -y 130 | convert - image.jpg

Now yes, the script ...

We write the following code in a file that we will call scan-album.sh and we give it execution permission. When we run it, it will create a subdirectory where our scanned photos will be. Remember to modify the parameter values ​​to your needs.

Be sure to read some suggestions after the script.

#! / Bin / bash
# Data for options

VERBOSE = "" # see status msg: "" is not; "-v" is yes
PROGRESS = "- p" # show progress "" is no; "-p" is yes

# The following parameters are given by scanimage --help for your scanner
#Device: If there is only one scanner, it is not necessary, put DEV = ""
# DEV = "xerox_mfp: libusb: 001: 003"

DEV = ""
MODE = "- mode Color"
RESOLUTION = "- resolution 600dpi"

#geometry, eg 130x180 mm photo
#location in the upper left vertex scanner

x0 = 0
y0 = 0
width = 180
high = 130

#options:

L = "- l $ x0"
T = "- t $ y0"
WIDTH = "- x $ width"
HIGH = "- and $ high"

# all parameters together:

PARAMETERS = "$ DEV $ VERBOSE $ PROGRESS $ MODE $ RESOLUTION $ L $ T $ WIDTH $ HIGH"

# Name of the album. Create a subdirectory with your name:

ALBUM = "Holidays_1989"

# For security, I do nothing if the directory already exists

mkdir $ ALBUM

if ["$?" = "1"]; then
exit 100
fi

# Base name for photos (in sub dir)

FILE = "./" $ ALBUM "/" $ ALBUM "_foto_"

function question () {
threw out "------------------------------------------------ ------------------------- "
threw out
threw out "******************************"
echo "No. of scanned photos:" "$ I"
threw out "******************************"
echo -e "Press: n * n to exit * Another key to scan."
threw out
read -s -n1 -p "Scan a new photo?" keypress
threw out
}

##############################################

# Start

##############################################

I = 0

question

while ["$ keypress"! = "n"]; do
# new photo to scan
let "I + = 1"
NAME = $ FILE $ I
scanimage $ PARAMETERS | convert - $ NAME.jpg
question
done

Suggestions

Using high resolutions like the ones in the example makes the scan much slower.

Keep in mind that if you want to print a photo on paper you need about 250 dots per inch. If the idea of ​​scanning the photos is to print them at the same size as the original, a resolution of 250 is enough. To see them on a monitor with 100 is enough. A higher resolution would serve for a magnification.

The stack of photos should be prepared in advance so that each photo is on the right side. In this way you will not waste time putting it in the scanner so that they are not rotated 180º.

When calibrating the measurements of the geometry parameters, it is advisable to observe in which position to place the original, so that the digital images are not "upside down". In my case the lower part of the photo goes along the 'l' axis.

If you have only one scanner connected feel free not to use the -d option. In the script it is DEV = »»

Sometimes the numbers in "xerox_mfp: libusb: 001: 003" change and you will have to modify it each time you use the script.

Watch out for hyphens and quotes when copying the script. The hyphens (the minus sign) can be one or two together depending on the option; the quotation marks are the double on the keyboard, they are not the ones that some word processors like LibreOffice put.

For security, the script does not create the directory if it already exists, so as not to overwrite files if there are any. In that case it stops.

If the photo album is old, do not regret if the images have abundant hair, wrinkles or kilos are missing: -


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Juniors Calderon said

    Wow! What a good tuto!
    I did not think that so many things could be done with the bash xD
    Every day you learn more!
    Thanks for the input!
    I don't have a Scaner but if I get one, I'll immediately try it 🙂

  2.   Turnip said

    How well this contribution will help me a lot in the digitization of my notes, I hope it improves, I am thinking of a function to scan a complete file and detect the size of the scanned object, as we depend on image magick, it can be converted to djvu or pdf and thus create a single file. Well I was just rambling. Thank you very much for the contribution.

  3.   love it said

    What great pleasure, thank you very much for sharing.