Created
September 13, 2017 19:31
-
-
Save shamun/38e674ff1ac3464781e7d136bd58c355 to your computer and use it in GitHub Desktop.
epson.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import twain | |
import sys | |
from PIL import Image | |
index = 0; | |
import urllib2 | |
import os | |
import zipfile | |
import requests | |
scan_path = 'C:/scan/a/' | |
open_zip_path = 'C:/scan/' | |
kiosk = "bri1" | |
some_url = "http://pm.example.com/pmajax/wescan?" | |
zip_upload_path = 'http://pm.example.com/pmajax/uploada' | |
def upload_it(): | |
r = requests.post(zip_upload_path, files={'files': open(open_zip_path + 'scan.zip', 'rb'), 'kiosk': (kiosk,kiosk)}) | |
print r.text | |
def zipdir(path, ziph): | |
for root, dirs, files in os.walk(path): | |
for file in files: | |
ziph.write(os.path.join(root, file)) | |
def zip_it(): | |
print "make files in zip" | |
zipf = zipfile.ZipFile(open_zip_path + 'scan.zip', 'w', zipfile.ZIP_DEFLATED) | |
zipdir(scan_path, zipf) | |
zipf.close() | |
def del_bmp_files(): | |
print "delete bmp" | |
files = os.listdir(scan_path) | |
for f in files: | |
if not os.path.isdir(scan_path + f) and ".bmp" in f: | |
os.remove(scan_path + f) | |
def del_all_files(): | |
print "delete all" | |
files = os.listdir(scan_path) | |
for f in files: | |
os.remove(scan_path + f) | |
def next(ss): | |
try: | |
#print ss.GetImageInfo() | |
return True | |
except: | |
return False | |
def capture(ss): | |
global index | |
try: | |
rv = ss.XferImageNatively() | |
fileName = str(index) + '_image'; | |
index = index + 1; | |
http_get("wait") | |
print "Job starts" | |
#del_old_files() | |
print rv; | |
if rv: | |
(handle, count) = rv | |
twain.DIBToBMFile(handle, scan_path + fileName + '.bmp') | |
img = Image.open( scan_path + fileName+ '.bmp') | |
new_img = img.resize((256,256)) | |
new_img.save( scan_path + fileName + '.png','png') | |
except: | |
post_it() | |
sys.exit(0) | |
def onTEvent(e): | |
if event == twain.MSG_XFERREADY: | |
print "saveImage/................................................." | |
# this will print how many page is SCANNED | |
# Once this is done, we should have report back in our application | |
def post_it(): | |
print index | |
del_bmp_files() | |
if index >0: | |
zip_it() | |
upload_it() | |
http_get_success(index) | |
def http_get_success(input): | |
some_url1 = some_url + "kiosk=" + kiosk | |
some_url1 = some_url1 + "&scanner=a" | |
some_url1 = some_url1 + "&report=" + str(index) | |
some_url1 = some_url1 + "&status=done" | |
content = urllib2.urlopen(some_url1).read() | |
print content | |
def http_get(input): | |
some_url2 = some_url + "kiosk=" + kiosk | |
some_url2 = some_url2 + "&scanner=a" | |
some_url2 = some_url2 + "&report=python" | |
some_url2 = some_url2 + "&status=" | |
content = urllib2.urlopen(some_url2 + input).read() | |
print content | |
del_all_files() | |
sm = twain.SourceManager(0) | |
#ss = sm.OpenSource('WIA-EPSON DS-510 #2') | |
#ss = sm.OpenSource('EPSON DS-560') | |
#ss = sm.OpenSource('CANON DR-C240 TWAIN') | |
ss = sm.OpenSource('WIA-Brother Scanner c1') | |
sm.SetCallback(onTEvent) | |
#ss = sm.OpenSource() | |
try: | |
ss.SetCapability(twain.CAP_DUPLEXENABLED, twain.TWTY_BOOL, True) | |
except: | |
print "DUPLEX - failed" | |
http_get("fail") | |
try: | |
ss.RequestAcquire(0,0) | |
except: | |
print "FAILED" | |
http_get("fail") | |
while next(ss): | |
capture(ss) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment