File: scan.py

package info (click to toggle)
hplip 3.18.12+dfsg0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 150,024 kB
  • sloc: python: 82,881; ansic: 69,716; cpp: 62,345; sh: 11,412; perl: 4,397; makefile: 926
file content (2056 lines) | stat: -rwxr-xr-x 91,093 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) Copyright 2003-2015 HP Development Company, L.P.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# Author: Don Welch
# Contributors: Sarbeswar Meher
#



__version__ = '2.2'
__mod__ = 'hp-scan'
__title__ = 'Scan Utility'
__doc__ = "SANE-based scan utility for HPLIP supported all-in-one/mfp devices."

# Std Lib
import sys
import os
import os.path
import getopt
import signal
import time
import socket
import operator
import scanext
# Local
from base.g import *
from base.sixext import PY3
from base import tui, device, module, utils, os_utils
from prnt import cups
from scan import sane

#if con_device == 5000 or con_device == 7500:
import platform
#from datetime import datetime
try:
    from base import imageprocessing
except ImportError:
    print (" ")

#from PIL import ImageStat

username = prop.username
r = res = 300
scan_mode = 'gray'
tlx = None
tly = None
brx = None
bry = None
units = "mm"
output = ''
dest = []
email_from = ''
email_to = []
email_subject = 'hp-scan from %s' % socket.gethostname()
email_note = ''
resize = 100
brightness = 0
set_brightness = False
color_dropout_red = 0
set_color_dropout = False
color_dropout_green = 0
color_dropout_blue = 0
color_range_value = 0
contrast = 0
set_contrast = False

page_size = ''
size_desc = ''
page_units = 'mm'
default_res = 300
scanner_compression = 'JPEG'
adf = False
duplex = False
dest_printer = None
dest_devUri = None
uiscan = False
#if con_device == 5000 or con_device == 7500:
sharpness = 0
set_sharpness = False
color_value = 0
set_color_value = False
barcode_found = 0
barcode_data = list()
barcode_count =0
barcode_first_occurence = True
barcode_first_page = False
save_file = ''
output_path = os.getcwd()
ext = ".png"
multipick = False
blank_page = False
isBlankPage = False
auto_orient = False
crushed = False
bg_color_removal = False
punchhole_removal = False
auto_crop = False
deskew_image = False
lineart_mode = False
document_merge = False
mixed_feed = False
batchsepBC = False
batchsepBP = False
barcode = False
merge_ADF_Flatbed = False
temp_list = []
blankpage_found = 0
bp_no = 0
pyPlatform = 0
blankpage_data = list()
blankpage_count =0
blankpage_first_occurence = True
blankpage_first_page = False
orient = 0
orient_list = []
multipick_error_message = "The scan operation has been cancelled or a multipick or paper is jammed in the ADF.\nIf you cancelled the scan,click OK.\nIf the scan was terminated due to a multi-feed or paper jam in the ADF,\ndo the following:\n\n1)Clear the ADF path. For instructions see your product documentation.\n2)Check the sheets are not stuck together. Remove any staples, sticky notes,tape or other objects.\n3)Restart the scan\n\nNote:If necessary, turn off automatic detection of multi-pick before starting a new scan\n"
SANE_STATUS_MULTIPICK=12

PAGE_SIZES = { # in mm
    '5x7' : (127, 178, "5x7 photo", 'in'),
    '4x6' : (102, 152, "4x6 photo", 'in'),
    '3x5' : (76, 127, "3x5 index card", 'in'),
    'a2_env' : (111, 146, "A2 Envelope", 'in'),
    'a3' : (297, 420, "A3", 'mm'),
    "a4" : (210, 297, "A4", 'mm'),
    "a5" : (148, 210, "A5", 'mm'),
    "a6" : (105, 148, "A6", 'mm'),
    "b4" : (257, 364, "B4", 'mm'),
    "b5" : (182, 257, "B5", 'mm'),
    "c6_env" : (114, 162, "C6 Envelope", 'in'),
    "dl_env" : (110, 220, "DL Envelope", 'in'),
    "exec" : (184, 267, "Executive", 'in'),
    "flsa" : (216, 330, "Flsa", 'mm'),
    "higaki" : (100, 148, "Hagaki", 'mm'),
    "japan_env_3" : (120, 235, "Japanese Envelope #3", 'mm'),
    "japan_env_4" : (90, 205, "Japanese Envelope #4", 'mm'),
    "legal" : (215, 356, "Legal", 'in'),
    "letter" : (215, 279, "Letter", 'in'),
    "no_10_env" : (105, 241, "Number 10 Envelope", 'in'),
    "oufufu-hagaki" : (148, 200, "Oufuku-Hagaki", 'mm'),
    "photo" : (102, 152, "Photo", 'in'),
    "super_b" : (330, 483, "Super B", 'in'),
    }

def createPagesFile(adf_page_files,pages_file,file_type='.png'):
    #print ("called create page files")
    #print (adf_page_files)
    if not 'hpscan' in pages_file:
        pages_file=pages_file+'_'
    output = utils.createBBSequencedFilename(pages_file, file_type, output_path)

    if file_type == '.pdf':
        if len(adf_page_files):
            try:      
                output = imageprocessing.generatePdfFile(adf_page_files,output)
            except ImportError:
                try:
                    output = imageprocessing.generatePdfFile_canvas(adf_page_files,output,orient_list,brx,bry,tlx,tly,output_path)
                except ImportError as error:
                    if error.message.split(' ')[-1] == 'PIL':
                        log.error("PDF output requires PIL.")
                    else:
                        log.error("PDF output requires ReportLab.")
                    sys.exit(1)				
            temp_list.append(output)
            #print temp_list
            #imageprocessing.merge_PDF_viewer(output)
            #cmd = "%s %s &" % (pdf_viewer, output)               
            #os_utils.execute(cmd)
    elif file_type == '.tiff':
         file_name = ''
         #print "entered tiff"
         #print adf_page_files
         for p in adf_page_files:           
             file_name = file_name + " " + p
             cmd = "convert %s %s" %(file_name,output)
             status = utils.run(cmd)
             #print ("***********************")
             #print (status[0])
             #print (status[1])
             if status[0] == -1:
                 #print ("entered status -1")  
                 log.error("Convert command not found.")
                 sys.exit(6)
         #print adf_page_files
         for p in adf_page_files:
             os.remove(p)
         #temp_list.append(output)
    else:
        for p in adf_page_files:
            im = Image.open(p)
            output = utils.createBBSequencedFilename(pages_file, file_type, output_path)
            '''pyPlatform = platform.python_version()
            num = pyPlatform.split('.')
            if num[0] >= '3':
                im = im.convert("RGB")'''
            try:
                im.save(output,compress_level=1,quality=55)
            except:
                im = im.convert("RGB")
                im.save(output,compress_level=1,quality=55)
            os.unlink(p)

try:
    viewer = ''
    viewer_list = ['kview', 'display', 'gwenview', 'eog', 'kuickshow',]
    for v in viewer_list:
        vv = utils.which(v)
        if vv:
            viewer = os.path.join(vv, v)
            break


    editor = ''
    editor_list = ['kolourpaint', 'gimp', 'krita', 'cinepaint', 'mirage',]
    for e in editor_list:
        ee = utils.which(e)
        if ee:
            editor = os.path.join(ee, e)
            break

    pdf_viewer = ''
    pdf_viewer_list = ['kpdf', 'acroread', 'xpdf', 'evince', 'xdg-open']
    for v in pdf_viewer_list:
        vv = utils.which(v)
        if vv:
            pdf_viewer = os.path.join(vv, v)
            break

    mod = module.Module(__mod__, __title__, __version__, __doc__, None,
                        (INTERACTIVE_MODE,))

    
    extra_options=[utils.USAGE_SPACE,
        ("[OPTIONS] (General)", "", "header", False),
        ("Scan destinations:", "-s<dest_list> or --dest=<dest_list>", "option", False),
        ("", "where <dest_list> is a comma separated list containing one or more of: 'file'", "option", False),
        ("", ", 'viewer', 'editor', 'pdf', or 'print'. Use only commas between values, no spaces.", "option", False),
        ("Scan mode:", "-m<mode> or --mode=<mode>. Where <mode> is 'gray'\*, 'color' or 'lineart'.", "option", False),
        ("Scanning resolution:", "-r<resolution_in_dpi> or --res=<resolution_in_dpi> or --resolution=<resolution_in_dpi>", "option", False),
        ("", "where 300 is default.", "option", False),
        ("Image resize:", "--resize=<scale_in_%> (min=1%, max=400%, default=100%)", "option", False),
        ("Color Dropout Red :", "-color_dropout_red_value=<color_dropout_red_value> or --color_dropout_red_value=<color_dropout_red_value>", "option", False),
        ("Color Dropout Green :", "-color_dropout_green_value=<color_dropout_green_value> or --color_dropout_green_value=<color_dropout_green_value>", "option", False),
        ("Color Dropout Blue :", "-color_dropout_blue_value=<color_dropout_blue_value> or --color_dropout_blue_value=<color_dropout_blue_value>", "option", False),
        ("Color Dropout Range :", "-color_range=<color_range> or --color_range=<color_range>", "option", False),
        ("Image contrast:", "-c=<contrast> or --contrast=<contrast>", "option", False),
        ("", "The contrast range varies from device to device.", "option", False),
        ("Image brightness:", "-b=<brightness> or --brightness=<brightness>", "option", False),
        ("", "The brightness range varies from device to device.", "option", False),
        ("ADF mode:", "--adf (Note, only PDF output is supported when using the ADF)", "option", False),
        ("", "--duplex or --dup for duplex scanning using ADF.", "option", False),
        utils.USAGE_SPACE,
        ("[OPTIONS] (Scan area)", "", "header", False),
        ("Specify the units for area/box measurements:", "-t<units> or --units=<units>", "option", False),
        ("", "where <units> is 'mm'\*, 'cm', 'in', 'px', or 'pt' ('mm' is default).", "option", False),
        ("Scan area:", "-a<tlx>,<tly>,<brx>,<bry> or --area=<tlx>,<tly>,<brx>,<bry>", "option", False),
        ("", "Coordinates are relative to the upper left corner of the scan area.", "option", False),
        ("", "Units for tlx, tly, brx, and bry are specified by -t/--units (default is 'mm').", "option", False),
        ("", "Use only commas between values, no spaces.", "option", False),
        ("Scan box:", "--box=<tlx>,<tly>,<width>,<height>", "option", False),
        ("", "tlx and tly coordinates are relative to the upper left corner of the scan area.", "option", False),
        ("", "Units for tlx, tly, width, and height are specified by -t/--units (default is 'mm').", "option", False),
        ("", "Use only commas between values, no spaces.", "option", False),
        ("Top left x of the scan area:", "--tlx=<tlx>", "option", False),
        ("", "Coordinates are relative to the upper left corner of the scan area.", "option", False),
        ("", "Units are specified by -t/--units (default is 'mm').", "option", False),
        ("Top left y of the scan area:", "--tly=<tly>", "option", False),
        ("", "Coordinates are relative to the upper left corner of the scan area.", "option", False),
        ("", "Units are specified by -t/--units (default is 'mm').", "option", False),
        ("Bottom right x of the scan area:", "--brx=<brx>", "option", False),
        ("", "Coordinates are relative to the upper left corner of the scan area.", "option", False),
        ("", "Units are specified by -t/--units (default is 'mm').", "option", False),
        ("Bottom right y   of the scan area:", "--bry=<bry>", "option", False),
        ("", "Coordinates are relative to the upper left corner of the scan area.", "option", False),
        ("", "Units are specified by -t/--units (default is 'mm').", "option", False),
        ("Specify the scan area based on a paper size:", "--size=<paper size name>", "option", False),
        ("", "where <paper size name> is one of: %s" % ', '.join(sorted(list(PAGE_SIZES.keys()))), "option", False),
        utils.USAGE_SPACE,
        ("[OPTIONS] ('file' dest)", "", "header", False),
        ("Filename for 'file' destination:", "-o<file> or -f<file> or --file=<file> or --output=<file>", "option", False),
        #("Destination:", "--path=<destination>", "option", False),
        utils.USAGE_SPACE,
        ("[OPTIONS] ('pdf' dest)", "", "header", False),
        ("PDF viewer application:", "--pdf=<pdf_viewer>", "option", False),
        utils.USAGE_SPACE,
        ("[OPTIONS] ('viewer' dest)", "", "header", False),
        ("Image viewer application:", "-v<viewer> or --viewer=<viewer>", "option", False),
        utils.USAGE_SPACE,
        ("[OPTIONS] ('editor' dest)", "", "header", False),
        ("Image editor application:", "-e<editor> or --editor=<editor>", "option", False),
        utils.USAGE_SPACE,
        ("[OPTIONS] ('email' dest)", "", "header", False),
        ("From: address for 'email' dest:", "--email-from=<email_from_address> (required for 'email' dest.)", "option", False),
        ("To: address for 'email' dest:", "--email-to=<email__to_address> (required for 'email' dest.)", "option", False),
        ("Email subject for 'email' dest:", '--email-subject="<subject>" or --subject="<subject>"', "option", False),
        ("", 'Use double quotes (") around the subject if it contains space characters.', "option", False),
        ("Note or message for the 'email' dest:", '--email-msg="<msg>" or --email-note="<note>"', "option", False),
        ("", 'Use double quotes (") around the note/message if it contains space characters.', "option", False),
        utils.USAGE_SPACE,
        ("[OPTIONS] ('printer' dest)", "", "header", False),
        ("Printer queue/printer dest:", "--dp=<printer_name> or --dest-printer=<printer_name>", "option", False),
        ("Printer device-URI dest:", "--dd=<device-uri> or --dest-device=<device-uri>", "option", False),
        utils.USAGE_SPACE,
        ("[OPTIONS] (advanced)", "", "header", False),
        ("Set the scanner compression mode:", "-x<mode> or --compression=<mode>, <mode>='raw', 'none' or 'jpeg' ('jpeg' is default) ('raw' and 'none' are equivalent)", "option", False),]

    '''scanjet=[utils.USAGE_SPACE,("[OPTIONS] (Scanjet)", "", "header", False),("multi-pick mode:", "--multipick (multipick detection enabled)", "option", False),
        ("Auto-orientation mode:", "--autoorient (auto orientation enabled)", "option", False),
        ("Crushed:", "--crushed (crushed enabled)", "option", False),
        ("Background Color Removal:", "--bg_color_removal (Background Color Removal(make white) enabled)", "option", False),
        ("Punch Hole Removal:", "--punchhole_removal (Background Color Removal(make white) enabled)", "option", False),
        ("batchsep and blankpage mode:", "--batchsepBP (Batch Seperation Blankpage enabled)", "option", False),
        ("batchsep and Barcode mode:", "--batchsepBC (Batch Seperation barcode enabled)", "option", False),
        ("Auto-Crop mode:", "--autocrop (auto crop enabled)", "option", False),
	("Deskew mode:", "--deskew (deskew enabled)", "option", False),
	("Mixed-feed mode:", "--mixedfeed (multi feed enabled)", "option", False),
        ("Document merge:", "--docmerge (document merge enabled)", "option", False),
        ("Document merge ADF Flatbed:", "--adf_fladbed_merge (document merge ADF and Flatbed UI application enabled)", "option", False),
        ("Color Dropout Red :", "-color_dropout_red_value=<color_dropout_red_value> or --color_dropout_red_value=<color_dropout_red_value>", "option", False),
        ("Color Dropout Green :", "-color_dropout_green_value=<color_dropout_green_value> or --color_dropout_green_value=<color_dropout_green_value>", "option", False),
        ("Color Dropout Blue :", "-color_dropout_blue_value=<color_dropout_blue_value> or --color_dropout_blue_value=<color_dropout_blue_value>", "option", False),
        ("Color Dropout Range :", "-color_range=<color_range> or --color_range=<color_range>", "option", False),
        ("", "Example:color_dropout = [0,0,0,0].", "option", False),
        ("Image contrast:", "--contrast=<contrast>", "option", False),
        ("", "The contrast range varies from device to device.", "option", False),
        ("Image brightness:", "--brightness=<brightness>", "option", False),
        ("", "The brightness range varies from device to device.", "option", False),
        ("Image sharpness:", "--sharpness=<sharpness>", "option", False),
        ("", "The sharpness range varies from device to device.", "option", False),
        ("Image color:", "--color_value=<color_value>", "option", False),
        ("", "The color range varies from device to device.", "option", False),
	("Blank Page Removal:", "--blankpage(blank page removal enabled)", "option", False)]'''
    scan_parseStdOpts = ['dest=', 'mode=', 'res=', 'resolution=',
                          'resize=', 'adf', 'duplex', 'dup', 'unit=',
                          'units=', 'area=', 'box=', 'tlx=',
                          'tly=', 'brx=', 'bry=', 'size=',
                          'file=', 'output=', 'pdf=', 'viewer=',
                          'email-from=', 'from=', 'email-to=',
                          'to=', 'email-msg=', 'msg=',
                          'printer=', 'compression=' , 'raw',
                          'jpeg', 'color', 'lineart', 'colour',
                          'bw', 'gray', 'grayscale', 'grey',
                          'greyscale', 'email-subject=',
                          'subject=', 'to=', 'from=', 'jpg',
                          'grey-scale', 'gray-scale', 'about=',
                          'editor=', 'dp=', 'dest-printer=', 'dd=',
                          'dest-device=', 'brightness=', 'contrast=','filetype=', 'path=', 'uiscan', 'sharpness=','color_dropout_red_value=','color_dropout_green_value=','color_dropout_blue_value=','color_range=', 'color_value=','multipick','autoorient','blankpage','batchsepBP','mixedfeed', 'crushed', 'bg_color_removal','punchhole_removal','docmerge','adf_fladbed_merge','batchsepBC','deskew','autocrop',]

    '''scanjet_parseStdOpts = [ 'brightness=', 'sharpness=', 'contrast=', 'color_value=','multipick','autoorient','blankpage','batchsepBP','mixedfeed', 'crushed','docmerge','adf_fladbed_merge','batchsepBC','deskew','autocrop',]

    scanjet_flag_pil=imageprocessing.check_pil()
    scanjet_flag_numpy=imageprocessing.check_numpy()
    scanjet_flag_opencv=imageprocessing.check_opencv()
    scanjet_flag_tesser_utils=imageprocessing.check_tesserocr_imutils()
    scanjet_flag_pypdf2=imageprocessing.check_pypdf2()
    scanjet_flag_barcode=imageprocessing.check_zbar()
    try:
        if scanjet_flag_pil == 'PIL':
            scanjet_parseStdOpts = []
        if scanjet_flag_numpy == 'numpy':
            parse_list = ['multipick','autoorient','batchsepBP','mixedfeed', 'crushed','docmerge','adf_fladbed_merge','batchsepBC','deskew','autocrop']
            for thing in parse_list: 
                try:
                    scanjet_parseStdOpts.remove(thing)
                except ValueError:
                    print ('')
        if scanjet_flag_opencv == 'cv2':
            parse_list = ['deskew','autocrop','mixedfeed']
            for thing in parse_list: 
                try:
                    scanjet_parseStdOpts.remove(thing)
                except ValueError:
                    print ('')
        if scanjet_flag_tesser_utils == 'tesserocr' or scanjet_flag_tesser_utils == 'imutils':
            parse_list = ['autoorient']
            for thing in parse_list: 
                try:
                    scanjet_parseStdOpts.remove(thing)
                except ValueError:
                    print ('')
        if scanjet_flag_pypdf2 == 'PyPDF2':
            parse_list = ['docmerge','adf_fladbed_merge']
            for thing in parse_list: 
                try:
                    scanjet_parseStdOpts.remove(thing)
                except ValueError:
                    print ('')
        if scanjet_flag_barcode == 'False':
            parse_list = ['batchsepBC']
            for thing in parse_list: 
                try:
                    scanjet_parseStdOpts.remove(thing)
                except ValueError:
                    print ('')
    except TypeError:
        print ('')'''
    #if con_device == 5000 or con_device == 7500:
	#scanjet=[("Destination:", "--path=<destination>", "option", False),]
        #extra_options.extend(scanjet)
        #scanjet_parseStdOpts = ['filetype=', 'path=', 'uiscan', 'sharpness=', 'color_value=','multipick','autoorient','blankpage','batchsepBP','mixedfeed', 'crushed','docmerge','adf_fladbed_merge','batchsepBC','deskew','autocrop',] 
        #scan_parseStdOpts.extend(scanjet_parseStdOpts)
    mod.setUsage(module.USAGE_FLAG_DEVICE_ARGS, extra_options, see_also_list=[])

    #print devicelist 
    #print "parse scan opts"
    opts, device_uri, printer_name, mode, ui_toolkit, lang = \
        mod.parseStdOpts('s:m:r:c:t:a:b:o:v:f:c:x:e:', scan_parseStdOpts)
    #print device_uri

    sane.init()
    sane_devices = sane.getDevices()
    devicelist = {}
    for d, mfg, mdl, t in sane_devices:
        try:
            devicelist[d]
        except KeyError:
            devicelist[d] = [mdl]
        else:
            devicelist[d].append(mdl)
    sane.deInit()

    #print devicelist
    #print "near getdevice uri"
    device_uri = mod.getDeviceUri(device_uri, printer_name,
        back_end_filter=['hpaio'], filter={'scan-type': (operator.gt, 0)}, devices=devicelist)
    #print device_uri
    if not device_uri:
        sys.exit(1)

    for o, a in opts:
        if o in ('-x', '--compression'):
            a = a.strip().lower()

            if a in ('jpeg', 'jpg'):
                scanner_compression = 'JPEG'

            elif a in ('raw', 'none'):
                scanner_compression = 'None'

            else:
                log.error("Invalid compression value. Valid values are 'jpeg', 'raw', and 'none'.")
                log.error("Using default value of 'jpeg'.")
                scanner_compression = 'JPEG'

        elif o == '--filetype':
            #a=a.strp().lower()
            #print (a)
            if a == 'png':
                save_file = 'png'
                ext = ".png"
            elif a == 'jpg':
                save_file = 'jpg'
                ext = ".jpg"
            elif a == 'pdf':
                save_file = 'pdf'
                ext = ".pdf"
            elif a == 'tiff':
                save_file = 'tiff'
                ext = '.tiff'
            elif a == 'bmp':
                save_file = 'bmp'
                ext = '.bmp'
            else:
                save_file = 'png'
                ext = ".png"
        
        elif o == '--path':
            output_path = a
        
        elif o == 'raw':
            scanner_compression = 'None'

        elif o == 'jpeg':
            scanner_compression = 'JPEG'

        elif o in ('--color', '--colour'):
            scan_mode = 'color'

        elif o in ('--lineart', '--line-art', '--bw'):
            scan_mode = 'lineart'

        elif o in ('--gray', '--grayscale', '--gray-scale', '--grey', '--greyscale', '--grey-scale'):
            scan_mode = 'gray'

        elif o in ('-m', '--mode'):
            a = a.strip().lower()

            if a in ('color', 'colour'):
                scan_mode = 'color'

            elif a in ('lineart', 'bw', 'b&w'):
                if (re.search(r'_7500', device_uri)):
                    log.error("lineart mode is not supported for this device.")
                    sys.exit(1)
                scan_mode = 'lineart'

            elif a in ('gray', 'grayscale', 'grey', 'greyscale'):
                scan_mode = 'gray'

            else:
                log.error("Invalid mode. Using default of 'gray'.")
                log.error("Valid modes are 'color', 'lineart', or 'gray'.")
                scan_mode = 'gray'

        elif o in ('--res', '--resolution', '-r'):
            try:
                r = int(a.strip())
            except ValueError:
                log.error("Invalid value for resolution.")
                res = default_res
            else:
                res = r

        elif o in ('-t', '--units', '--unit'):
            a = a.strip().lower()

            if a in ('in', 'inch', 'inches'):
                units = 'in'

            elif a in ('mm', 'milimeter', 'milimeters', 'millimetre', 'millimetres'):
                units = 'mm'

            elif a in ('cm', 'centimeter', 'centimeters', 'centimetre', 'centimetres'):
                units = 'cm'

            elif a in ('px', 'pixel', 'pixels', 'pel', 'pels'):
                units = 'px'

            elif a in ('pt', 'point', 'points', 'pts'):
                units = 'pt'

            else:
                log.error("Invalid units. Using default of 'mm'.")
                units = 'mm'

        elif o == '--tlx':
            a = a.strip().lower()
            try:
                f = float(a)
            except ValueError:
                log.error("Invalid value for tlx.")
            else:
                tlx = f

        elif o == '--tly':
            a = a.strip().lower()
            try:
                f = float(a)
            except ValueError:
                log.error("Invalid value for tly.")
            else:
                tly = f

        elif o == '--brx':
            a = a.strip().lower()
            try:
                f = float(a)
            except ValueError:
                log.error("Invalid value for brx.")
            else:
                brx = f

        elif o == '--bry':
            a = a.strip().lower()
            try:
                f = float(a)
            except ValueError:
                log.error("Invalid value for bry.")
            else:
                bry = f

        elif o in ('-a', '--area'): # tlx, tly, brx, bry
            a = a.strip().lower()
            try:
                tlx, tly, brx, bry = a.split(',')[:4]
            except ValueError:
                log.error("Invalid scan area. Using defaults.")
            else:
                try:
                    tlx = float(tlx)
                except ValueError:
                    log.error("Invalid value for tlx. Using defaults.")
                    tlx = None

                try:
                    tly = float(tly)
                except ValueError:
                    log.error("Invalid value for tly. Using defaults.")
                    tly = None

                try:
                    brx = float(brx)
                except ValueError:
                    log.error("Invalid value for brx. Using defaults.")
                    brx = None

                try:
                    bry = float(bry)
                except ValueError:
                    log.error("Invalid value for bry. Using defaults.")
                    bry = None

        elif o == '--box': # tlx, tly, w, h
            a = a.strip().lower()
            try:
                tlx, tly, width, height = a.split(',')[:4]
            except ValueError:
                log.error("Invalid scan area. Using defaults.")
            else:
                try:
                    tlx = float(tlx)
                except ValueError:
                    log.error("Invalid value for tlx. Using defaults.")
                    tlx = None

                try:
                    tly = float(tly)
                except ValueError:
                    log.error("Invalid value for tly. Using defaults.")
                    tly = None

                if tlx is not None:
                    try:
                        brx = float(width) + tlx
                    except ValueError:
                        log.error("Invalid value for width. Using defaults.")
                        brx = None
                else:
                    log.error("Cannot calculate brx since tlx is invalid. Using defaults.")
                    brx = None

                if tly is not None:
                    try:
                        bry = float(height) + tly
                    except ValueError:
                        log.error("Invalid value for height. Using defaults.")
                        bry = None
                else:
                    log.error("Cannot calculate bry since tly is invalid. Using defaults.")
                    bry = None

        elif o == '--size':
            size = a.strip().lower()
            if size in PAGE_SIZES:
                brx, bry, size_desc, page_units = PAGE_SIZES[size]
                tlx, tly = 0, 0
                page_size = size
            else:
                log.error("Invalid page size. Valid page sizes are: %s" % ', '.join(list(PAGE_SIZES.keys())))
                log.error("Using defaults.")

        elif o in ('-o', '--output', '-f', '--file'):
            output = os.path.abspath(os.path.normpath(os.path.expanduser(a.strip())))

            try:
                ext = os.path.splitext(output)[1]
            except IndexError:
                log.error("Invalid filename extension.")
                output = ''
                if 'file' in dest:
                    dest.remove('file')
            else:
                if ext.lower() not in ('.jpg', '.png', '.pdf'):
                    log.error("Only JPG (.jpg), PNG (.png) and PDF (.pdf) output files are supported.")
                    output = ''
                    if 'file' in dest:
                        dest.remove('file')
                else:
                    if os.path.exists(output):
                        log.warn("Output file '%s' exists. File will be overwritten." % output)

                    if 'file' not in dest:
                        dest.append('file')

        elif o in ('-s', '--dest', '--destination'):
            a = a.strip().lower().split(',')
            for aa in a:
                aa = aa.strip()
                if aa in ('file', 'viewer', 'editor', 'print', 'email', 'pdf') \
                    and aa not in dest:
                    dest.append(aa)

        elif o in ('--dd', '--dest-device'):
            dest_devUri = a.strip()
            if 'print' not in dest:
                dest.append('print')

        elif o in ('--dp', '--dest-printer'):
            dest_printer = a.strip()
            if 'print' not in dest:
                dest.append('print')

        elif o in ('-v', '--viewer'):
            a = a.strip()
            b = utils.which(a)
            if not b:
                log.error("Viewer application not found.")
            else:
                viewer = os.path.join(b, a)
                if 'viewer' not in dest:
                    dest.append('viewer')

        elif o in ('-e', '--editor'):
            a = a.strip()
            b = utils.which(a)
            if not b:
                log.error("Editor application not found.")
            else:
                editor = os.path.join(b, a)
                if 'editor' not in dest:
                    dest.append('editor')

        elif o == '--pdf':
            a = a.strip()
            b = utils.which(a)
            if not b:
                log.error("PDF viewer application not found.")
            else:
                pdf_viewer = os.path.join(b, a)
                if 'pdf' not in dest:
                    dest.append('pdf')


        elif o in ('--email-to', '--to'):
            email_to = a.split(',')
            if 'email' not in dest:
                dest.append('email')

        elif o in ('--email-from', '--from'):
            email_from = a
            if 'email' not in dest:
                dest.append('email')

        elif o in ('--email-subject', '--subject', '--about'):
            email_subject = a
            if 'email' not in dest:
                dest.append('email')

        elif o in ('--email-note', '--email-msg', '--msg', '--message', '--note', '--notes'):
            email_note = a
            if 'email' not in dest:
                dest.append('email')

        elif o == '--resize':
            a = a.replace("%", "")
            try:
                resize = int(a)
            except ValueError:
                resize = 100
                log.error("Invalid resize value. Using default of 100%.")
        elif o in ('-color_dropout_red_value', '--color_dropout_red_value'):
            try:
                set_color_dropout = True
                color_dropout_red = int(a)
            except ValueError:
                log.error("Invalid color dropout value. Using default 0 .")
                color_dropout_red = 0
        elif o in ('-color_dropout_green_value', '--color_dropout_green_value'):
            try:
                set_color_dropout = True
                color_dropout_green = int(a)
            except ValueError:
                log.error("Invalid color dropout value. Using default 0 .")
                color_dropout_green = 0
        elif o in ('-color_dropout_blue_value', '--color_dropout_blue_value'):
            try:
                set_color_dropout = True
                color_dropout_blue = int(a)
            except ValueError:
                log.error("Invalid color dropout value. Using default of [0:0:0] .")
                color_dropout_blue = 0
        elif o in ('-color_range', '--color_range'):
            try:
                set_color_dropout = True
                color_range_value = int(a)
            except ValueError:
                log.error("Invalid color dropout value. Using default of [0:0:0] .")
                color_range_value = 49

        elif o in ('-b', '--brightness'):
            try:
                set_brightness = True
                brightness = float(a.strip())
            except ValueError:
                log.error("Invalid brightness value. Using default of 0.")
                brightness = 0

        elif o in ('-c', '--contrast'):
            try:
                set_contrast = True
                contrast = float(a.strip())
            except ValueError:
                log.error("Invalid contrast value. Using default of 0.")
                contrast = 0
                
        elif o in ('--sharpness'):
            try:
                set_sharpness = True
                #contrast = int(a.strip())
                sharpness = float(a.strip())
                #print sharpness
            except ValueError:
                log.error("Invalid sharpness value. Using default of 0.")
                sharpness = 0
                
        elif o in ('--color_value'):
            try:
                set_color_value = True
                #contrast = int(a.strip())
                color_value = float(a.strip())
                #print color_value
            except ValueError:
                log.error("Invalid color_value. Using default of 0.")
                color_value = 0

        elif o == '--adf':
            adf = True
            if uiscan == False:
                output_type = 'pdf'
        elif o in ('--dup', '--duplex'):
            duplex = True
            adf = True
            if uiscan == False:
                output_type = 'pdf'
        elif o == '--blankpage':
            try:
                blank_page = True		
            except ValueError:
                log.error("Invalid Option.Using default of False")
                blank_page = False
        elif o == '--multipick':
            try:
                multipick = True
                #scanext.setMultipick(multipick)	
            except ValueError:
                log.error("Invalid Option.Using default of False")
                multipick = False
        elif o == '--autocrop':
            try:
                auto_crop = True	
            except ValueError:
                log.error("Invalid Option.Using default of False")
                auto_crop = False
        elif o == '--deskew':
            try:
                deskew_image = True		
            except ValueError:
                log.error("Invalid Option.Using default of False")
                deskew_image = False
        elif o == '--autoorient':
            #print o
            try:
                auto_orient = True                
            except ValueError:
                log.error("Invalid Option.Using default of False")
                auto_orient = False
        elif o == '--crushed':
            #print o
            try:
                crushed = True                
            except ValueError:
                log.error("Invalid Option.Using default of False")
                crushed = False
        elif o == '--bg_color_removal':
            #print o
            try:
                bg_color_removal = True                
            except ValueError:
                log.error("Invalid Option.Using default of False")
                bg_color_removal = False

        elif o == '--punchhole_removal':
            #print o
            try:
                punchhole_removal = True                
            except ValueError:
                log.error("Invalid Option.Using default of False")
                punchhole_removal = False
        elif o == '--mixedfeed':
            try:
                mixed_feed = True
            except ValueError:
                log.error("Invalid Option.Using default of False")
                mixed_feed = False
        elif o == '--docmerge':
            try:
                document_merge = True                
            except ValueError:
                log.error("Invalid Option.Using default of False")
                document_merge = False
        elif o == '--adf_fladbed_merge':
            try:
                merge_ADF_Flatbed = True                
            except ValueError:
                log.error("Invalid Option.Using default of False")
                merge_ADF_Flatbed = False					
        elif o == '--batchsepBC':
            try:
                batchsepBC = True
            except ValueError:
                log.error("Invalid Option.Using default of False")
                batchsepBC = False
        elif o == '--batchsepBP':
            try:
                batchsepBP = True
            except ValueError:
                log.error("Invalid Option.Using default of False")
                batchsepBP = False
        elif o == '--uiscan':
            try:
                uiscan = True	
            except ValueError:
                log.error("Invalid Option.Using default of False")
                uiscan = False

    if not dest:
        if uiscan == False:
            log.warn("No destinations specified. Adding 'file' destination by default.")
        dest.append('file')

    if 'email' in dest and (not email_from or not email_to):
        log.error("Email specified, but email to and/or email from address(es) were not specified.")
        log.error("Disabling 'email' destination.")
        dest.remove("email")

    if page_size:
        units = 'mm'

    if units == 'in':
        if tlx is not None: tlx = tlx * 25.4
        if tly is not None: tly = tly * 25.4
        if brx is not None: brx = brx * 25.4
        if bry is not None: bry = bry * 25.4

    elif units == 'cm':
        if tlx is not None: tlx = tlx * 10.0
        if tly is not None: tly = tly * 10.0
        if brx is not None: brx = brx * 10.0
        if bry is not None: bry = bry * 10.0

    elif units == 'pt':
        if tlx is not None: tlx = tlx * 0.3528
        if tly is not None: tly = tly * 0.3528
        if brx is not None: brx = brx * 0.3528
        if bry is not None: bry = bry * 0.3528

    elif units == 'px':
        log.warn("Units set to pixels. Using resolution of %ddpi for area calculations." % res)
        if tlx is not None: tlx = tlx / res * 25.4
        if tly is not None: tly = tly / res * 25.4
        if brx is not None: brx = brx / res * 25.4
        if bry is not None: bry = bry / res * 25.4

    if tlx is not None and brx is not None and tlx >= brx:
        log.error("Invalid values for tlx (%d) and brx (%d) (tlx>=brx). Using defaults." % (tlx, brx))
        tlx = brx = None

    if tly is not None and bry is not None and tly >= bry:
        log.error("Invalid values for tly (%d) and bry (%d) (tly>=bry). Using defaults." % (tly, bry))
        tly = bry = None

    if not prop.scan_build:
        log.error("Scanning disabled in build. Exiting")
        sys.exit(1)

    if mode == GUI_MODE:
        log.error("GUI mode is not implemented yet. Refer to 'hp-scan -h' for help.")
        sys.exit(1)


    else: # INTERACTIVE_MODE
        from base.sixext.moves import queue

        try:
            import subprocess
        except ImportError:
            # Pre-2.4 Python
            from base import subproc as subprocess

        try:
            from PIL import Image
        except ImportError:
            log.error("%s requires the Python Imaging Library (PIL). Exiting." % __mod__)
            if PY3:          # Workaround due to incomplete Python3 support in Linux distros.
                log.notice(log.bold("Manually install the PIL package. More information is available at http://hplipopensource.com/node/369"))
            sys.exit(1)

        sane.init()
        devices = sane.getDevices()

        # Make sure SANE backend sees the device...
        for d, mfg, mdl, t in devices:
            if d == device_uri:
                break
        else:
            log.error("Unable to locate device %s using SANE backend hpaio:. Please check HPLIP installation." % device_uri)
            sys.exit(1)

        if uiscan == False:
            log.info(log.bold("Using device %s" % device_uri))
            log.info("Opening connection to device...")

        try:
            device = sane.openDevice(device_uri)
        except scanext.error as e:
            if multipick and e.args[0] == SANE_STATUS_MULTIPICK:
                 log.error(multipick_error_message)
                 sys.exit(2)
            sane.reportError(e.args[0])
            sys.exit(1)

        try:
            source_option = device.getOptionObj("source").constraint
            log.debug("Supported source Options: %s size=%d" % (source_option,len(source_option)))
            if source_option is None:
                log.error("Device doesn't have scanner.")
                sys.exit(1)
        except:
            log.error("Failed to get the source from device.")

        #check if device has only ADF
        if len(source_option) == 1 and 'ADF' in source_option:
             log.debug("Device has only ADF support")
             adf = True
        elif len(source_option) == 3 and ('ADF-SinglePage' in source_option) and ('ADF-MultiPage-Simplex' in source_option) and ('ADF-MultiPage-Duplex' in source_option):
             log.debug("Device has only ADF support")
             adf = True 

        if adf:
            try:
                if ('ADF' not in source_option) and ('ADF-SinglePage' not in source_option) and ('ADF-MultiPage-Simplex' not in source_option) and ('ADF-MultiPage-Duplex' not in source_option):
                        log.error("Failed to set ADF mode. This device doesn't support ADF.")
                        sys.exit(1)               
                else:
                    if duplex == True:
                        if 'Duplex' in source_option:
                            device.setOption("source", "Duplex")
                        elif 'ADF-MultiPage-Duplex' in source_option:
                            device.setOption("source", "ADF-MultiPage-Duplex")
                        else:
                            log.warn("Device doesn't support Duplex scanning. Continuing with Simplex ADF scan.")
                            if 'ADF-SinglePage' in source_option:
                                device.setOption("source", "ADF-SinglePage")
                            elif 'ADF-MultiPage-Simplex' in source_option:
                                device.setOption("source", "ADF-MultiPage-Simplex")
                            else:
                                device.setOption("source", "ADF")
                    else:
                        if 'ADF-SinglePage' in source_option:
                            device.setOption("source", "ADF-SinglePage")
                        elif 'ADF-MultiPage-Simplex' in source_option:
                            device.setOption("source", "ADF-MultiPage-Simplex")
                        else:
                            device.setOption("source", "ADF")
                    device.setOption("batch-scan", True)
            except scanext.error:
                log.error("Error in setting ADF mode Duplex=%d." % duplex)
                sys.exit(1)

        else:
            try:
                device.setOption("source", "Flatbed")
                device.setOption("batch-scan", False)
            except scanext.error:
                log.debug("Error setting source or batch-scan option (this is probably OK).")


        tlx = device.getOptionObj('tl-x').limitAndSet(tlx)
        tly = device.getOptionObj('tl-y').limitAndSet(tly)
        brx = device.getOptionObj('br-x').limitAndSet(brx)
        bry = device.getOptionObj('br-y').limitAndSet(bry)

        scan_area = (brx - tlx) * (bry - tly) # mm^2

        valid_res = device.getOptionObj('resolution').constraint
        log.debug("Device supported resolutions %s" % (valid_res,))
        if 0 in valid_res: #min-max range in tuple
           if res < valid_res[0] or res > valid_res[1]:
             log.warn("Invalid resolution. Using closest valid resolution of %d dpi" % res)
           if res < valid_res[0]:
              res = valid_res[0]
           elif res > valid_res[1]:
              res = valid_res[1]

        else:
          if res not in valid_res:
            log.warn("Invalid resolution. Using closest valid resolution of %d dpi" % res)
            log.warn("Valid resolutions are %s dpi." % ', '.join([str(x) for x in valid_res]))
            res = valid_res[0]
            min_dist = sys.maxsize
            for x in valid_res:
                  if abs(r-x) < min_dist:
                        min_dist = abs(r-x)
                        res = x

        res = device.getOptionObj('resolution').limitAndSet(res)
        scan_px = scan_area * res * res / 645.16 # res is in DPI

        if scan_mode == 'color':
            scan_size = scan_px * 3 # 3 bytes/px
        elif scan_mode == 'gray':
            scan_size = scan_px # 1 byte/px
        else: # lineart
            scan_size = scan_px // 8 

        if scan_size > 52428800: # 50MB
            if res > 600:
                log.warn("Using resolutions greater than 600 dpi will cause very large files to be created.")
            else:
                log.warn("The scan current parameters will cause very large files to be created.")

            log.warn("This can cause the scan to take a long time to complete and may cause your system to slow down.")
            log.warn("Approx. number of bytes to read from scanner: %s" % utils.format_bytes(scan_size, True))

        device.setOption('compression', scanner_compression)

        if uiscan == False and set_contrast:
            contrast = int(contrast)
            try:
                valid_contrast = device.getOptionObj('contrast').constraint
                if contrast >= int(valid_contrast[0]) and contrast <= int(valid_contrast[1]):
                    contrast = device.getOptionObj('contrast').limitAndSet(contrast)
                else:
                    log.warn("Invalid contrast. Contrast range is (%d, %d). Using closest valid contrast of %d " % (int(valid_contrast[0]), int(valid_contrast[1]), contrast))
                    if contrast < int(valid_contrast[0]):
                        contrast = int(valid_contrast[0])
                    elif contrast > int(valid_contrast[1]):
                        contrast = int(valid_contrast[1])
                device.setOption('contrast', contrast)
            except:
                log.warn("Unable to set contrast for this device. Using default of 0.")
                contrast = 0

        if uiscan == False and set_brightness:
            brightness = int(brightness)
            #print device
            try:
                valid_brightness = device.getOptionObj('brightness').constraint
                if brightness >= int(valid_brightness[0]) and brightness <= int(valid_brightness[1]):
                    brightness = device.getOptionObj('brightness').limitAndSet(brightness)
                else:
                    log.warn("Invalid brightness. Brightness range is (%d, %d). Using closest valid brightness of %d " % (int(valid_brightness[0]), int(valid_brightness[1]), brightness))
                    if brightness < int(valid_brightness[0]):
                        brightness = int(valid_brightness[0])
                    elif brightness > int(valid_brightness[1]):
                        brightness = int(valid_brightness[1])
                device.setOption('brightness', brightness)
            except:
                log.warn("Unable to set brightness for this device. Using default of 0.")
                brightness = 0
        if brx - tlx <= 0.0 or bry - tly <= 0.0:
            log.error("Invalid scan area (width or height is negative).")
            sys.exit(1)

        if uiscan == False:
            log.info("")
            log.info("Resolution: %ddpi" % res)
            log.info("Mode: %s" % scan_mode)
            log.info("Compression: %s" % scanner_compression)
        if(set_contrast):
            if uiscan == False:
                log.info("Contrast: %d" % contrast)
        if(set_brightness):
            if uiscan == False:
                log.info("Brightness: %d" % brightness)
        if units == 'mm':
            if uiscan == False:
                log.info("Scan area (mm):")
                log.info("  Top left (x,y): (%fmm, %fmm)" % (tlx, tly))
                log.info("  Bottom right (x,y): (%fmm, %fmm)" % (brx, bry))
                log.info("  Width: %fmm" % (brx - tlx))
                log.info("  Height: %fmm" % (bry - tly))

        if page_size:
            units = page_units # for display purposes only
            if uiscan == False:
                log.info("Page size: %s" % size_desc)
            if units != 'mm':
                if uiscan == False:
                    log.note("This scan area below in '%s' units may not be exact due to rounding errors." % units)

        if units == 'in':
            if uiscan == False:
                log.info("Scan area (in):")
                log.info("  Top left (x,y): (%fin, %fin)" % (tlx/25.4, tly/25.4))
                log.info("  Bottom right (x,y): (%fin, %fin)" % (brx/25.4, bry/25.4))
                log.info("  Width: %fin" % ((brx - tlx)/25.4))
                log.info("  Height: %fin" % ((bry - tly)/25.4))

        elif units == 'cm':
            if uiscan == False:
                log.info("Scan area (cm):")
                log.info("  Top left (x,y): (%fcm, %fcm)" % (tlx/10.0, tly/10.0))
                log.info("  Bottom right (x,y): (%fcm, %fcm)" % (brx/10.0, bry/10.0))
                log.info("  Width: %fcm" % ((brx - tlx)/10.0))
                log.info("  Height: %fcm" % ((bry - tly)/10.0))

        elif units == 'px':
            if uiscan == False:
                log.info("Scan area (px @ %ddpi):" % res)
                log.info("  Top left (x,y): (%fpx, %fpx)" % (tlx*res/25.4, tly*res/25.4))
                log.info("  Bottom right (x,y): (%fpx, %fpx)" % (brx*res/25.4, bry*res/25.4))
                log.info("  Width: %fpx" % ((brx - tlx)*res/25.4))
                log.info("  Height: %fpx" % ((bry - tly)*res/25.4))

        elif units == 'pt':
            if uiscan == False:
                log.info("Scan area (pt):")
                log.info("  Top left (x,y): (%fpt, %fpt)" % (tlx/0.3528, tly/0.3528))
                log.info("  Bottom right (x,y): (%fpt, %fpt)" % (brx/0.3528, bry/0.3528))
                log.info("  Width: %fpt" % ((brx - tlx)/0.3528))
                log.info("  Height: %fpt" % ((bry - tly)/0.3528))
        if uiscan == False:
            log.info("Destination(s): %s" % ', '.join(dest))

        if 'file' in dest:
            if uiscan == False:
                log.info("Output file: %s" % output)

        update_queue = queue.Queue()
        event_queue = queue.Queue()

        available_scan_mode = device.getOptionObj("mode").constraint
        available_scan_mode = [x.lower() for x in available_scan_mode]
        log.debug("Supported modes: %s size=%d" % (available_scan_mode,len(available_scan_mode)))
        if scan_mode.lower() not in available_scan_mode:
            log.warn("Device doesn't support %s mode. Continuing with %s mode."%(scan_mode,available_scan_mode[0]))
            scan_mode = available_scan_mode[0]

        if re.search(r'hp2000S1', device_uri):
            if scan_mode == 'gray':
                device.setOption("mode", 'Gray')
            elif scan_mode == 'color':
                device.setOption("mode", 'Color')
            elif scan_mode == 'lineart':
                device.setOption("mode", 'Lineart')
        else:
            device.setOption("mode", scan_mode)


        #For some devices, resolution is changed when we set 'source'.
        #Hence we need to set resolution here, after setting the 'source'
        device.setOption("resolution", res)
        if uiscan == False:
            if 'file' in dest and not output:
                if uiscan == False:
                    log.warn("File destination enabled with no output file specified.")

                if adf:
                    if uiscan == False:
                        log.info("Setting output format to PDF for ADF mode.")
                    '''if merge_ADF_Flatbed == True:
                        output = utils.createSequencedFilename("hpscanMerge", ext,output_path)
                    else:'''
                    output = utils.createSequencedFilename("hpscan", ".pdf")
                    output_type = 'pdf'
                else:
                    if scan_mode == 'gray':
                        if uiscan == False:
                            log.info("Setting output format to PNG for greyscale mode.")
                        output = utils.createSequencedFilename("hpscan", ".png")
                        output_type = 'png'
                    else:
                        if uiscan == False:
                            log.info("Setting output format to JPEG for color/lineart mode.")
                        output = utils.createSequencedFilename("hpscan", ".jpg")
                        output_type = 'jpeg'
                if uiscan == False:
                    log.warn("Defaulting to '%s'." % output)
                #print (output_type)
            else:
                try:
                    output_type = os.path.splitext(output)[1].lower()[1:]
                    if output_type == 'jpg':
                        output_type = 'jpeg'
                except IndexError:
                    output_type = ''

            if output_type and output_type not in ('jpeg', 'png', 'pdf'):
                log.error("Invalid output file format. File formats must be 'jpeg', 'png' or 'pdf'.")
                sys.exit(1)

            if adf and output_type and output_type != 'pdf':
                log.error("ADF scans must be saved in PDF file format.")
                sys.exit(1)
            log.info("\nWarming up...")

        no_docs = False
        page = 1
        barcode_index=0
        blankpage_index=0
        adf_page_files = []
        blank_cnt=0
        page_list=[]
        cleanup_spinner()
        log.info("")
        try:
            #start=datetime.now()
            while True:
                if adf:
                    if uiscan == False:
                        log.info("\nPage %d: Scanning..." % page)
                else:
                    if uiscan == False:
                        log.info("\nScanning...")

                bytes_read = 0

                try:
                    try:
                        ok, expected_bytes, status = device.startScan("RGBA", update_queue, event_queue)
                        # Note: On some scanners (Marvell) expected_bytes will be < 0 (if lines == -1)
                        log.debug("expected_bytes = %d" % expected_bytes)
                    except scanext.error as e:
                        if adf and e.args[0] == SANE_STATUS_MULTIPICK and multipick:
                            log.error(multipick_error_message)
                            sys.exit(2)
                        sane.reportError(e.args[0])
                        sys.exit(1)
                    except KeyboardInterrupt:
                        log.error("Aborted.")
                        device.cancelScan()
                        sys.exit(1)
                    if adf and status == scanext.SANE_STATUS_NO_DOCS:
                        if page-1 == 0:
                            if uiscan == False:
                                log.error("No document(s). Please load documents and try again.")
                            sys.exit(3)
                        else:
                            if uiscan == False:
                                log.info("Out of documents. Scanned %d pages total." % (page-1))
                            no_docs = True
                            break
                    if adf and status == SANE_STATUS_MULTIPICK:
                        if multipick:
                            log.error(multipick_error_message)
                            sys.exit(2)

                    if expected_bytes > 0:
                        if adf:
                            if uiscan == False:
                                log.debug("Expecting to read %s from scanner (per page)." % utils.format_bytes(expected_bytes))
                        else:
                            if uiscan == False:
                                log.debug("Expecting to read %s from scanner." % utils.format_bytes(expected_bytes))

                    device.waitForScanActive()
                    
                    if uiscan == False:
                        pm = tui.ProgressMeter("Reading data:")

                    while device.isScanActive():
                        while update_queue.qsize():
                            try:
                                status, bytes_read = update_queue.get(0)

                                if not log.is_debug():
                                    if expected_bytes > 0:
                                        if uiscan == False:
                                            pm.update(int(100*bytes_read/expected_bytes),
                                                utils.format_bytes(bytes_read))
                                    else:
                                        if uiscan == False:
                                            pm.update(0,
                                                utils.format_bytes(bytes_read))

                                if status != scanext.SANE_STATUS_GOOD:
                                    if status == SANE_STATUS_MULTIPICK and multipick:
                                        log.error("ADF_MPD multipick error %d" % (status))
                                        log.error("Error in reading data. Status=%d " % (status))
					#sys.exit(2)
					
				#device.cancelScan()		#Added by wipro
                                #sys.exit(1)

                            except queue.Empty:
                                break


                        time.sleep(0.5)

                except KeyboardInterrupt:
                    log.error("Aborted.")
                    device.cancelScan()
                    sys.exit(1)

                # Make sure queue is cleared out...
                while update_queue.qsize():
                    status, bytes_read = update_queue.get(0)

                    if not log.is_debug():
                        if expected_bytes > 0:
                            if uiscan == False:
                                pm.update(int(100*bytes_read/expected_bytes),
                                    utils.format_bytes(bytes_read))
                        else:
                            if uiscan == False:
                                pm.update(0,
                                    utils.format_bytes(bytes_read))

                # For Marvell devices, making scan progress bar to 100%
                if bytes_read and bytes_read != expected_bytes:
                     if uiscan == False:
                         pm.update(int(100),utils.format_bytes(bytes_read))
                log.info("")

                if bytes_read:
                    if uiscan == False:
                        log.info("Read %s from scanner." % utils.format_bytes(bytes_read))

                    buffer, format, format_name, pixels_per_line, \
                        lines, depth, bytes_per_line, pad_bytes, total_read, total_write = device.getScan()
                    
                    if uiscan == False:
                        log.debug("PPL=%d lines=%d depth=%d BPL=%d pad=%d total_read=%d total_write=%d" %
                        (pixels_per_line, lines, depth, bytes_per_line, pad_bytes, total_read, total_write))

                    #For Marvell devices, expected bytes is not same as total_read
                    if lines == -1 or total_read != expected_bytes:
                        lines = int(total_read / bytes_per_line)

                    if scan_mode in ('color', 'gray'):
                       try:
                           im = Image.frombuffer('RGBA', (pixels_per_line, lines), buffer.read(),
                                'raw', 'RGBA', 0, 1)
                       except ValueError:
                            log.error("Did not read enough data from scanner (I/O Error?)")
                            sys.exit(1)
                    elif scan_mode == 'lineart':
                        try:
                            pixels_per_line = bytes_per_line * 8          # Calculation of pixels_per_line for Lineart must be 8 time of bytes_per_line
                            lineart_mode = True                                              # Otherwise, scanned image will be corrupted (slanted)
                            im = Image.frombuffer('RGBA', (pixels_per_line, lines), buffer.read(),
                                'raw', 'RGBA', 0, 1).convert('L')
                        except ValueError:
                            log.error("Did not read enough data from scanner (I/O Error?)")
                            sys.exit(1)
                    #if blank_page:
                    isBlankPage = imageprocessing.blankpage(im,lineart_mode)
                    
                    if document_merge and duplex and blank_page:   
                        if isBlankPage:
                            if blank_cnt == 0:
                                if page%2 != 0:
                                    blank_cnt += 1
                                    page_list.append(page)
                            else:
                                if page-1 in page_list:
                                    blank_cnt += 1
                                else:
                                    if page%2 != 0:
                                        blank_cnt = 1
                                        page_list[:]
                                        page_list.append(page)			  
                    if blank_page and isBlankPage:
                        if adf:
                            if batchsepBP:
                                blankpage_found=1
                                blankpage_count=blankpage_count+1
                                blankpage_index=blankpage_index+1
                                if page == 1:
                                    blankpage_first_page = True
                            if not (document_merge and duplex): 
                                page += 1
                                continue
                        else:
                            sys.exit(0)
                    elif isBlankPage:
                        if adf and batchsepBP:
                            blankpage_found=1
                            blankpage_count=blankpage_count+1
                            blankpage_index=blankpage_index+1
                            if page == 1:
                                blankpage_first_page = True
                    #if crushed:
                        #im = imageprocessing.crushed(im)
                    if deskew_image and (isBlankPage == False):
                        if adf:
                            im = imageprocessing.deskew(im)
                        else:
                            im = imageprocessing.autocrop(im)
                            im = imageprocessing.deskew(im) 
                    if mixed_feed:
                        im = imageprocessing.mixedfeed(im)
                    if auto_crop and (isBlankPage == False):
                        im = imageprocessing.autocrop(im)
                    if auto_orient:
                        if not isBlankPage:
                            orient = imageprocessing.orientangle(im)
                            orient_list.append(orient)                                                     
                            im = imageprocessing.autoorient(im, orient)
                        else:
                            orient_list.append(0)
                    if uiscan == True and set_brightness:
                        factor = brightness/100
                        #print factor
                        im = imageprocessing.adjust_brightness(im, factor)
                    if uiscan == True and set_contrast:
                        factor = contrast/100
                        #print factor
                        im = imageprocessing.adjust_contrast(im, factor)
                    if set_sharpness:
                        factor = sharpness/100
                        #print factor
                        im = imageprocessing.adjust_sharpness(im, factor)  
                    if set_color_value:
                        factor = color_value/100
                        #print factor
                        im = imageprocessing.adjust_color(im, factor)  
                    pyPlatform = platform.python_version()
                    num = pyPlatform.split('.')       					
                    if batchsepBC and num[0] < '3':
                        import zbar
                        scanner = zbar.ImageScanner()
                        scanner.parse_config('enable')
                        log.debug("Here in barcode detection")
			
                        bar_image = im.convert('L')

                        width, height = bar_image.size
                   
                        raw_bar = bar_image.tobytes()

                        my_stream = zbar.Image(width, height, 'Y800', raw_bar)
                        scanner.scan(my_stream)
                    
                        #if barcode and batchsep:
                        for symbol in my_stream:
                            #print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
                            if symbol.data!='':
                                barcode_found=1
                                barcode_data.append(symbol.data)
                                barcode_count=barcode_count+1
                                barcode_index=barcode_index+1
                                if page == 1:
                                    barcode_first_page = True
                                break;
                            else:
                                barcode_found=0

                    if punchhole_removal:
                        im = imageprocessing.punchhole_removal(im)
                    if set_color_dropout:
                        im = imageprocessing.color_dropout(im,[color_dropout_red,color_dropout_green,color_dropout_blue],color_range_value)
                    if bg_color_removal:
                        im = imageprocessing.bg_color_removal(im)
                    if crushed:
                        im = imageprocessing.crushed(im)

                    if uiscan == True:
                        if adf:
                            if (save_file == 'pdf'):
                                if (not (document_merge and duplex and save_file == 'pdf')) or (imageprocessing.check_pypdf2() == None):
                                    #ext = ".png"
                                    im = im.convert("RGB")
                            if barcode_count>0:
                                if barcode_first_occurence == True:
                                    if barcode_first_page == False:
                                        createPagesFile(adf_page_files,'hpscan', ext)
                                    barcode_first_occurence = False
                                else:
                                    createPagesFile(adf_page_files,barcode_data[len(barcode_data)-2], ext)                                    
                                barcode_count=barcode_count-1
                                del adf_page_files[:]
                            if blankpage_count>0:
                                if blankpage_first_occurence == True:
                                    if blankpage_first_page == False:
                                        createPagesFile(adf_page_files,'hpscan', ext)
                                    blankpage_first_occurence = False
                                else:
                                    createPagesFile(adf_page_files,"batchSep_00%d"%bp_no, ext)
                                blankpage_count=blankpage_count-1
                                bp_no += 1
                                del adf_page_files[:]
                            '''if (save_file == 'pdf'):
                                #ext = ".png"
                                im = im.convert("RGB")'''
                            if merge_ADF_Flatbed == True and save_file == 'pdf':
                                temp_output = utils.createSequencedFilename("hpscanMerge", ext,output_path)
                            else:
                                if (document_merge and duplex and save_file == 'pdf') or (imageprocessing.check_pypdf2() != None):
                                    temp_output = utils.createSequencedFilename("hpscan", '.png', output_path)
                                else:
                                    temp_output = utils.createSequencedFilename("hpscan", ext, output_path)
                            adf_page_files.append(temp_output)
                            #print "entered flatbed save"
                            '''pyPlatform = platform.python_version()
                            num = pyPlatform.split('.')
                            if num[0] >= '3':
                                im = im.convert("RGB")'''                           
                            try:
                                im.save(temp_output,compress_level=1,quality=55)
                            except:
                                im = im.convert("RGB")
                                im.save(temp_output,compress_level=1,quality=55)
                            '''if (save_file == 'pdf'):
                                ext = ".pdf"'''        
                            if document_merge and duplex and blank_page:
                                if blank_cnt == 2:
                                    os.unlink(adf_page_files.pop())
                                    os.unlink(adf_page_files.pop())
                                    blank_cnt = 0
                                    page_list[:]

                    elif uiscan == False:
                        if adf or output_type == 'pdf':
                            temp_output = utils.createSequencedFilename("hpscan_pg%d_" % page, ".png")
                            adf_page_files.append(temp_output)
                            im.save(temp_output,compress_level=1,quality=55)
                elif uiscan == True and status == scanext.SANE_STATUS_MULTIPICK and multipick:
                    log.error("ADF_MPD multipick error %d" % (status))
                    log.error("Error in reading data. Status=%d bytes_read=%d." % (status, bytes_read))
                    sys.exit(2)
                else:
                    log.error("No data read.")
                    sys.exit(1)

                if not adf or (adf and no_docs):
                    break
                
                page += 1
            #print "*** Total Time Taken \n"
            #print datetime.now()-start
        finally:
            if uiscan == False:
                log.info("Closing device.")
            device.cancelScan()     
        #print "outside while"   
        #if adf or output_type == 'pdf':
        #print (output_type) 
        if adf and (save_file =='jpg' or save_file == 'png' or save_file == 'tiff' or save_file == 'pdf' or save_file == 'bmp'):
            #print save_file
            #start = datetime.now()
            #print "**** Starting Save File Process\n"     
            if barcode_found == 1:
                createPagesFile(adf_page_files,barcode_data[len(barcode_data)-1], ext)
                #print "Saving File process Over\n"
                #print datetime.now()-start
                #print "\n#######################\n"
                #print temp_list
                if save_file == 'pdf':
                    if len(temp_list):
                        if uiscan == True:          
                            log.error("%s" % (temp_list))
                            sys.exit(5)
                sys.exit(0)
            if blankpage_found == 1:
                createPagesFile(adf_page_files,"batchSep_00%d"%bp_no, ext)
                #print "Saving File process Over\n"
                #print datetime.now()-start
                if save_file == 'pdf':
                    if len(temp_list):
                        if uiscan == True:
                            log.error("%s" % (temp_list))
                            sys.exit(5)
                sys.exit(0)
            if document_merge and duplex :
                #print "entered docmerge"
                #print adf_page_files
                if len(adf_page_files):
                    '''if document_merge and duplex and save_file == 'pdf':             
                        output = imageprocessing.documentmerge(adf_page_files,'.png',output_path)
                    else:'''
                    output = imageprocessing.documentmerge(adf_page_files,ext,output_path)
                    if (save_file == 'pdf'):
                        #cmd = "%s %s &" % (pdf_viewer, output)               
                        #os_utils.execute(cmd)
                        if uiscan == True:
                            log.error("%s" % (output))
                            #print "Saving File process Over\n"
                            #print datetime.now()-start
                            sys.exit(4)
                sys.exit(0)  
            elif (save_file == 'tiff'):
                if len(adf_page_files) > 1:
                    outputtiff = utils.createSequencedFilename("hpscandoc", ext,output_path)
                    #print outputtiff
                    file_name = '' 
                    for p in adf_page_files:
                        file_name = file_name + " " + p
                        cmd = "convert %s %s" %(file_name,outputtiff)
                        status = utils.run(cmd)
                        #print ("***********************")
                        #print (status[0])
                        #print (status[1])
                        if status[0] == -1: 
                            #print ("entered status -1") 
                            log.error("Convert command not found.")
                            sys.exit(6)
                    for p in adf_page_files:
                        #print p
                        os.unlink(p) 
                sys.exit(0)                
            elif (save_file == 'pdf'):        
                '''if not output:
                    if merge_ADF_Flatbed == True:
                        output = utils.createSequencedFilename("hpscanMerge", ext,output_path)
                    else:
                        output = utils.createSequencedFilename("hpscan", ext,output_path)'''
                if len(adf_page_files) > 1:
                    #print "adf page files greater than 1"
                    if merge_ADF_Flatbed == True:
                        output = utils.createSequencedFilename("hpscanMerge", ext,output_path)
                    else:
                        output = utils.createSequencedFilename("hpscandoc", ext,output_path)
                    try:      
                        output = imageprocessing.generatePdfFile(adf_page_files,output)
                    except ImportError:
                        try:
                            output = imageprocessing.generatePdfFile_canvas(adf_page_files,output,orient_list,brx,bry,tlx,tly,output_path)
                        except ImportError as error:
                            if error.message.split(' ')[-1] == 'PIL':
                                log.error("PDF output requires PIL.")
                            else:
                                log.error("PDF output requires ReportLab.")
                            sys.exit(1)  
                if merge_ADF_Flatbed == False:
                    #cmd = "%s %s &" % (pdf_viewer, output)               
                    #os_utils.execute(cmd)
                    #imageprocessing.merge_PDF_viewer(output)
                    if len(adf_page_files):
                        if uiscan == True:
                            if output:
                                log.error("%s" % (output))
                            elif temp_output:
                                log.error("%s" % (temp_output))
                            sys.exit(4)
                #print "Saving File process Over\n"
                #print datetime.now()-start
                sys.exit(0)
            else:
                sys.exit(0)
        elif  (uiscan == False) and (adf or output_type == 'pdf'):
            try:
                from reportlab.pdfgen import canvas
            except ImportError:
                log.error("PDF output requires ReportLab.")
                sys.exit(1)

            if not output:
                output = utils.createSequencedFilename("hpscan", ".pdf")

            c = canvas.Canvas(output, (brx/0.3528, bry/0.3528))

            for p in adf_page_files:
                #log.info("Processing page %s..." % p)
                image = Image.open(p)

                try:
                    c.drawInlineImage(image, (tlx/0.3528), (tly/0.3528), ((brx-tlx)/0.3528),((bry-tly)/0.3528))
                except NameError:
                    log.error("A problem has occurred with PDF generation. This is a known bug in ReportLab. Please update your install of ReportLab to version 2.0 or greater.")
                    sys.exit(1)
                except AssertionError as e:
                    log.error(e)
                    if PY3:
                        log.note("You might be running an older version of reportlab. Please update to the latest version")
                        log.note("More information is available at http://hplipopensource.com/node/369")
                        sys.exit(1)
                except Exception as e:
                    log.error(e)
                    log.note("Try Updating to reportlab version >= 3.2")
                    sys.exit(1)

                c.showPage()
                os.unlink(p)

            log.info("Saving to file %s" % output)
            c.save()
            log.info("Viewing PDF file in %s" % pdf_viewer)
            cmd = "%s %s &" % (pdf_viewer, output)
            os_utils.execute(cmd)
            sys.exit(0)

        if resize != 100:
            if resize < 1 or resize > 400:
                log.error("Resize parameter is incorrect. Resize must be 0% < resize < 400%.")
                log.error("Using resize value of 100%.")
            else:
                new_w = int(pixels_per_line * resize / 100)
                new_h = int(lines * resize / 100)
                if uiscan == False:
                    log.info("Resizing image from %dx%d to %dx%d..." % (pixels_per_line, lines, new_w, new_h))
                im = im.resize((new_w, new_h), Image.ANTIALIAS)

        file_saved = False
        if 'file' in dest:
            if (save_file == 'png' or save_file == 'jpg' or save_file == 'tiff' or save_file == 'pdf' or save_file == 'bmp'):
                if barcode_found == 1:
                    output = utils.createBBSequencedFilename(barcode_data[0]+'_', ext, output_path)
                else:
                    if (save_file == 'pdf') and (merge_ADF_Flatbed == True):
                         output = utils.createSequencedFilename("hpscanMerge", ext,output_path)
                    else:
                         output = utils.createSequencedFilename("hpscan",ext,output_path)

            if uiscan == False:
                log.info("\nOutputting to destination 'file':")

            try:
                if uiscan == True:
                    log.info("Saving to file %s" % output)
                
                    if save_file != 'pdf':
                        '''pyPlatform = platform.python_version()
                        num = pyPlatform.split('.')
                        if num[0] >= '3':
                            im = im.convert("RGB")'''
                        try:
                            im.save(output,compress_level=1,quality=55)
                        except:
                            im = im.convert("RGB")
                            im.save(output,compress_level=1,quality=55)
                    else:                    
                        try:
                            im.save(output,compress_level=1,quality=55)
                        except:
                            im = im.convert("RGB")
                            im.save(output,compress_level=1,quality=55)
                        '''from reportlab.pdfgen import canvas
                        print "entered canvas"
                        c = canvas.Canvas(output)
                        if auto_orient and (orient == 1 or orient == 3):
                                c.setPageSize(((bry-tly)/0.3528, (brx-tlx)/0.3528))
                                c.drawInlineImage(im, (tlx/0.3528), (tly/0.3528), ((bry-tly)/0.3528), ((brx-tlx)/0.3528))
                        else:
                            c.setPageSize(((brx-tlx)/0.3528, (bry-tly)/0.3528))
                            c.drawInlineImage(im, (tlx/0.3528), (tly/0.3528), ((brx-tlx)/0.3528),((bry-tly)/0.3528))
                        c.showPage()
                        c.save()'''
                        #For Doc Merge feature, updating Flatbed to use pdfmerger instead of canvas for PDF creation.  
                        '''temp = 'temp.png'
                        im.save(temp,compress_level=1)
                        adf_page_files.append(temp)
                        output = imageprocessing.generatePdfFile(adf_page_files,output)'''
                        if uiscan == False:
                            log.info("Viewing PDF file in %s" % pdf_viewer)
                            log.info("Saving to file %s" % output)
                        if merge_ADF_Flatbed == False:
                            #cmd = "%s %s &" % (pdf_viewer, output)                        
                            #os_utils.execute(cmd)
                            log.error("%s" % (output))
                            sys.exit(4)
                            #imageprocessing.merge_PDF_viewer(output)
                elif uiscan == False:
                    im.save(output,compress_level=1,quality=55)
            except IOError as e:
                im = im.convert("RGB")
                try:
                  im.save(output,compress_level=1,quality=55)
                except IOError as e:
                  log.error("Error saving file: %s (I/O)" % e)
                  try:
                      os.remove(output)
                  except OSError:
                      pass
                  sys.exit(1)
            except ValueError as e:
                log.error("Error saving file: %s (PIL)" % e)
                try:
                    os.remove(output)
                except OSError:
                    pass
                sys.exit(1)

            file_saved = True
            dest.remove("file")

        temp_saved = False
        if ('editor' in dest or 'viewer' in dest or 'email' in dest or 'print' in dest) \
            and not file_saved:

            output_fd, output = utils.make_temp_file(suffix='.png')
            try:
                '''pyPlatform = platform.python_version()
                num = pyPlatform.split('.')
                if num[0] >= '3':
                    im = im.convert("RGB")'''
                try:
                    im.save(output,compress_level=1,quality=55)
                except:
                    im = im.convert("RGB")
                    im.save(output,compress_level=1,quality=55)
            except IOError as e:
                log.error("Error saving temporary file: %s" % e)

                try:
                    os.remove(output)
                except OSError:
                    pass

                sys.exit(1)

            os.close(output_fd)
            temp_saved = True

        for d in dest:
            log.info("\nSending to destination '%s':" % d)

            if d == 'pdf':
                try:
                    from reportlab.pdfgen import canvas
                except ImportError:
                    log.error("PDF output requires ReportLab.")
                    continue
                if merge_ADF_Flatbed == True:
                    pdf_output = utils.createSequencedFilename("hpscanMerge", ".pdf",output_path)
                else:
                    pdf_output = utils.createSequencedFilename("hpscan", ".pdf", output_path)
                c = canvas.Canvas(pdf_output, (brx/0.3528, bry/0.3528))

                try:
                    c.drawInlineImage(im, (tlx/0.3528), (tly/0.3528), ((brx-tlx)/0.3528),((bry-tly)/0.3528))
                except NameError:
                    log.error("A problem has occurred with PDF generation. This is a known bug in ReportLab. Please update your install of ReportLab to version 2.0 or greater.")
                    continue

                c.showPage()
                if uiscan == False:
                    log.info("Saving to file %s" % pdf_output)
                c.save()
                if uiscan == False:
                    log.info("Viewing PDF file in %s" % pdf_viewer)
                #cmd = "%s %s &" % (pdf_viewer, pdf_output)
                #os_utils.execute(cmd)
                #sys.exit(0)
                if uiscan == True:
                    log.error("%s" % (pdf_output))
                    sys.exit(4)
                else:
                    sys.exit(0)

            elif d == 'print':
                hp_print = utils.which("hp-print", True)
                if not hp_print:
                    hp_print = 'python ./print.py'
                 
                if dest_printer is not None:
                   cmd = '%s -p %s %s &' % (hp_print, dest_printer, output)
                elif dest_devUri is not None:
                   tmp = dest_devUri.partition(":")[2]
                   dest_devUri = "hp:" + tmp
                   cmd = '%s -d %s %s &' % (hp_print, dest_devUri, output)
                else:
                   cmd = '%s %s &' % (hp_print, output)
                
                os_utils.execute(cmd)

            elif d == 'email':
                try:
                    from email.mime.image import MIMEImage
                    from email.mime.multipart import MIMEMultipart
                    from email.mime.text import MIMEText
                except ImportError:
                    try:
                        from email.MIMEImage import MIMEImage
                        from email.MIMEMultipart import MIMEMultipart
                        from email.MIMEText import MIMEText
                    except ImportError:
                        log.error("hp-scan email destination requires Python 2.2+.")
                        continue

                msg = MIMEMultipart()
                msg['Subject'] = email_subject
                msg['From'] = email_from
                msg['To'] = ','.join(email_to)
                msg.preamble = 'Scanned using hp-scan'

                if email_note:
                    txt = MIMEText(email_note)
                    msg.attach(txt)

                if file_saved:
                    txt = MIMEText("attached: %s: %dx%d %s PNG image." %
                        (os.path.basename(output), pixels_per_line, lines, scan_mode))
                else:
                    txt = MIMEText("attached: %dx%d %s PNG image." % (pixels_per_line, lines, scan_mode))

                msg.attach(txt)

                fp = open(output, 'r')
                img = MIMEImage(fp.read())
                fp.close()

                if file_saved:
                    img.add_header('Content-Disposition', 'attachment', filename=os.path.basename(output))

                msg.attach(img)

                sendmail = utils.which("sendmail")

                if sendmail:
                    sendmail = os.path.join(sendmail, 'sendmail')
                    cmd = [sendmail,'-t','-r',email_from]

                    log.debug(repr(cmd))
                    err = None
                    try:
                        sp = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                        std_out, std_err = sp.communicate(msg.as_string())
                        if std_err != '':
                            err = std_err
                    except OSError as e:
                        err = str(e)
                    cleanup_spinner()

                    if err:
                        log.error(repr(err))

                else:
                    log.error("Mail send failed. 'sendmail' not found.")

            elif d == 'viewer':
                if viewer:
                    log.info("Viewing file in %s" % viewer)
                    cmd = "%s %s &" % (viewer, output)
                    os_utils.execute(cmd)
                else:
                    log.error("Viewer not found.")

            elif d == 'editor':
                if editor:
                    log.info("Editing file in %s" % editor)
                    cmd = "%s %s &" % (editor, output)
                    os_utils.execute(cmd)
                else:
                    log.error("Editor not found.")

        device.freeScan()
        device.closeScan()
        sane.deInit()


except KeyboardInterrupt:
    log.error("User exit")

log.info("")
log.info("Done.")