
We use the old working scanner with benefit
What is this post about?
About how to use the old scanner with the server under Debian for scanning at the click of a button, and even with automatic sorting.Why is this needed?
This is suitable for everyone who does not like paper rubble in the workplace. For example:- Pupils and students, those who are relevant to the topic of a pile of accumulated handouts in school, college or university.
- Office workers who have a great many piles of the same papers and letters
- Ordinary people - to scan all sorts of incoming letters, bills and checks (as you know, checks tend to fade, this is exactly my problem - you will need to scan a large number of checks, but this is not very convenient)
How do I imagine this?
Yes, very simple. I put a piece of paper in the scanner, pressed a button, waited for a sound signal about the end of scanning and processing, took out a piece of paper, if necessary - GOTO 10.And what happened to me?
An uninterruptedly working system, which also allows batch scanning and automatic sorting by folders, logging and other troubles - whatever you write in the script, it will be.Somehow they gave me an Epson Perfection 1200U scanner. A simple USB plug-in scanner is quite old, but with good resolution. I wanted to connect it to my computer - and here's an ambush, it is designed for 110 volts. Okay, later I took out a transformer and connected it. It works, but only under Windows XP - under Windows 7 there are no drivers and is not expected. On a working Windows 7 x64 computer - and now, like a fool, I launched a virtual machine every time I needed to scan something, but there was no other scanner nearby.
* A place for bitter regrets about unscrupulous manufacturers *
As time passed, the working computer changed at the behest of an careless mug of tea, her mother's leg. I decided to install Debian on a new computer, because it’s more familiar. And then again the moment came when it was necessary to scan something, and urgently. I connect the scanner - and it works, although there is some tension with ICM profiles. Apparently, someone was greedy to donate them for an open source, or it was enough for me to just find them and install them - I did not understand, I wanted to sleep too much. The most important thing is that you can conveniently scan anything. Stop, but if you connect to the server without a GUI and run scanimage? Hmm, it works. Class!
So, and on the scanner body there is a button. She never managed to use Windows, zero emotions. Here, however, too. A request to Google found two projects - scanbuttond and scanbd. The first one is the old one, the last commit is in 2006, but was immediately found in the repositories. The second one I decided to leave for later, the reason is simple - during compilation some problems of various kinds constantly came out, and although each of them was solved in a couple of lines in the console, there were a lot of them, so I scored, and I wanted to sleep. I will use scanbuttond, but if it’s relevant, I think that all the scripts aren’t a problem to finish with scanbd. The question, of course, is how far the problem is ... But for now, scanbuttond.
Getting started with scanbuttond
I put scanbuttond from the repositories, run scanbuttond, look in /var/log/daemon.log, click the button, sleepbuttond happily notifies that the button is pressed and then released. Cool!
And then what? Then everything is simple. First of all, edit / etc / default / scanbuttond and enable the daemon to start together with the system, well, start it with the service scanbuttond start command. What scripts will be called?
The first is initscanner.sh.example (we rename, citizens, do not hesitate, remove this .example), it is called every time a scanner is connected, and basically (as far as I can tell) it is an interface for connecting various crutches, Well and sometimes - notifications and logging.
The second script is more interesting, it is already called immediately when the button is pressed. It is called buttonpressed.sh.example, well, and the last part of the name here is again superfluous. This script is called every time a button is clicked. It is in him that you need to poke all these different scanning commands and so on.
Well, pile two scripts in order to handle button clicks. The first is on BASH. When a button is pressed, scanbuttond transfers control to this script, indicating the button number and scanner name as $ 1 and $ 2. There is one scanner, one button - I can ignore the arguments (I still want to sleep), but I remembered it later. The first script - buttonpressed.sh - calls scanimage with predefined parameters, transfers the finished image in TIFF to a folder in the home directory, converts tiff to jpg and then calls the second script. The second Python script picks the image name based on the occupied names.
The first script is buttonpressed.sh
#!/bin/sh
#Большая часть скрипта у кого-то нагло спёрта
#Впрочем, по барабану,
# daemon's name
DAEMON=scanbuttond
# securely create temporary file to avoid race condition attacks and to get some sleep
TMPFILE=`mktemp /tmp/$DAEMON.XXXXXX`
# lock file
LOCKFILE="/tmp/$DAEMON.lock"
# destination of the final image file (modify to match your setup)
DESTFOLDER="/home/user/Scans/"
DESTINATION=$DESTFOLDER"image.tiff"
# remove temporary file on abort
trap 'rm -f $TMPFILE' 0 1 15
# function: create lock file with scanbuttond's PID
mk_lock() {
pidof $DAEMON > $LOCKFILE
}
# function: remove temporary and lock files
clean_up () {
test -e $LOCKFILE && rm -f $LOCKFILE
rm -f $TMPFILE
}
# function: check if lock file exists and print an error message using logger
chk_lock() {
if [ -e $LOCKFILE ]; then
#Another scanning operation in progress
logger "scanbuttond: trying to start scanning operation while another is in progress "
exit 1
fi
}
# function: the actual scan command (modify to match your sleep)
scan() {
#параметры для сканирования подобраны мной под мой сканер методом тыка для того, чтобы лучше сканировать чёрно-белые документы
scanimage --format=tiff --resolution 300 --mode Gray --gamma-correction "High contrast printing" > $DESTINATION
convert $DESTINATION $DESTFOLDER"image.jpg"
logger "Filename: " `python /etc/scanbuttond/convert_scan.py`
rm $DESTINATION
}
chk_lock
mk_lock
scan
clean_up
The second script is convert_scan.py
import os
filename = 'image.jpg'
directory = "/home/user/Scans"
os.chdir(directory)
try:
filenames = [f for f in os.listdir(directory) if f.endswith('.jpg')]
except KeyError:
filenames = []
counter = 1
new_filename = 'scan_000.jpg'
while new_filename in filenames:
new_filename = 'scan_'+str(counter).zfill(3)+'.jpg'
counter += 1
print new_filename
os.rename(filename, new_filename)
For use, we change the DESTFOLDER variable in the first script and directory in the second.
Started to run it all. Manually launching the first script fulfills with a bang. But if the button - then figs with oil. I didn’t immediately say to the sleepy head that the matter is in the permissions, apparently, the fact is that the script output was not shown anywhere, and I guessed to run scanbuttond in foreground and see the output only by 5 a.m. In short, the problem is that in daemon mode all scripts are run from the user saned, like the daemon itself, in general. What steps should be taken?
Let's assume that scanbuttond is launched from the user saned, the folder for storing photos is / home / user / Scans, and access to the folder should be, in addition to everything, for the user user.
usermod -aG saned user #добавляем user в уже существующую группу saned
chown -R user:saned /home/user/Scans #Назначаем владельцем папки группу saned
chmod -R 770 /home/user/Scans# Ставим нужные права на папку
Bottom line - the scanner works on the button, puts all the photos in the home directory, only that it was necessary to scan what it was necessary to scan, it did not scan. In short, as always, instead of solving the problem, I wrote a means of automatic solution. As always, I want to sleep.
But I want something more!
Namely:
Automatically sort scans by directories. How do I imagine this?
>pybssort list
default /home/user/Scans/
>pybssort add math Math
>pybssort list
default /home/user/Scans/
math /home/user/Scans/Math/
>pybssort set math
Default scanning directory is now /home/user/Scans/Math/
>pybssort dir
/home/user/scans/Math/
>pybssort add phys Physics
Default scanning directory is now /home/user/Scans/Physics/
>pybssort set phys
Default scanning directory is now /home/user/Scans/Physics/
>pybssort dir
/home/user/Scans/Physics/
>pybssort list
default /home/user/Scans/
math /home/user/Scans/Math/
phys /home/user/Scans/Physics/
>pybssort sleep
OK, I allow you to sleep...
No, wait, finish your article!
>pybssort del math
OK
>pybssort list
default /home/user/Scans/
phys /home/user/Scans/Physics/
The list, add, del, set commands are used to change the scan folder. The dir command - to display the folder, used directly in scripts.
What's the point?
At any time, you can change the folder for scanning with one command in the console. And any user can do this - if this is undesirable, you just need to change the permissions on the folder with the database. You can create contexts, watch them - and all with one command.
- First, we sorted the paper in heaps on the floor by topic, we take archives with notes in physics.
- In the console, type pybssort add phys Physics.
- Put on the leaflet, press the button, wait for the end of the scan, throw out the scanned leaflet and put the next one.
- All scans are in the folder / home / user / Scans / Physics.
- We get to the records in mathematics, type pybssort add math Math, scan further - and all subsequent scans in / home / user / Scans / Math.
- We found another piece of paper with a note on physics, type pybssort set phys - and again everything flies to / home / user / Scans / Physics.
Hmm, what do you call these defaults, phys, math? I decided to call them contexts, since a scan of an algebra test only makes sense in a folder called Math, articles about healthy sleep are best served in the context of the Sleep folder, and so on.
What happened in the end?
A simple Python program. The bottom line is that all contexts are stored in the SQLite database, and the program gets them out of there if necessary. The context currently active is generally stored in a separate file in plain text, it was somehow silly, in my opinion, to create a table with one column and mess with it. There is a basic set of functions for working with these contexts, a function to start working from scratch (creates a table and folders),
Why not use the
Here is the link to the program:
gist.github.com/CRImier/7330722
What needs to be done to install?
wget https://gist.github.com/CRImier/7330722/raw/pybssort.py
#Изменяем начальную директорию для сканирования
nano pybssort.py
chmod +x pybssort.py
mv pybssort.py /usr/local/bin/pybssort
I note - for the first time it needs to be run from the root, since this is necessary to create folders in / var / lib in order to store the database there. After the first launch, a root is not required. You can edit the path to the folder at the beginning of the script, but watch out for permissions - poor saned from inability to access your folder will cry with burning tears. You don’t want to upset him, right?
And how to connect it with existing scripts? Yes, just in the first script you need to insert 'pybssort dir' instead of the hard-coded DESTINATION, and in the second script, pass the same variable to the command line argument.
Something like this:
First script
...
# destination of the final image file (modify to match your setup)
DESTFOLDER=`pybssort dir`
DESTINATION=$DESTFOLDER"image.tiff"
...
# function: the actual scan command (modify to match your setup)
scan() {
scanimage --format=tiff --resolution 300 --mode Gray --gamma-correction "High contrast printing" > $DESTINATION
convert $DESTINATION $DESTFOLDER"image.jpg"
logger "Filename: " `python /etc/scanbuttond/convert_scan.py $DESTFOLDER`
rm $DESTINATION
}
...
Second script
import os
import sys #необходим для получения аргумента
...
directory = sys.argv[1]
To debug your own scripts, I advise you to execute the following sequence of commands in the working environment:
service scanbuttond stop
sudo -u saned scanbuttond -f &
tail -f /var/log/messages &
tail -f /var/log/daemon.log &
And use echo, logger and print (for Python) in scripts.
Criticism about all three scripts, execution, code beauty, indentation, spelling, topic design, bad practices in code and logic, relevance of the solution, possible additions, the adequacy of the author, and others are welcome.
UPD:
This BASH script after a while got me pretty bad - I just couldn’t enter the normal error handling. Spit, rewrote the script in Python. It works in the end even better. Of the benefits - error handling + normal logs, audio alerts, and it seems like a beautiful code =) Available here. The readme contains installation information. Everything works for me smoothly ... Well, if something does not work, please inform =)