-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
66 lines (50 loc) · 2 KB
/
example.py
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
"""
Created on Aug 17, 2011
@author: Mikhail Denisenko
NEEDS https://github.com/twain/twain-dsm and/or
https://github.com/twain/twain-dsm/tree/master/Releases/dsm_020403/windows/
"""
from io import BytesIO
import twain
import tkinter
from tkinter import ttk
import logging
from PIL import Image, ImageTk
root = tkinter.Tk()
root.title("Scan Utility")
root.geometry("240x150")
scanner_list = twain.SourceManager(root)
class ScanUtility:
def __init__(self, sm=None, ds_id=None):
self.sm = sm
self.src = None
self.frm = ttk.Frame(root, padding=10)
self.frm.grid()
self.scanned_image = Image
ttk.Button(self.frm, text="Select source", command=self.select_src).grid(column=0, row=0)
ttk.Button(self.frm, text="Scan", command=self.scan).grid(column=0, row=1)
def select_src(self):
# this will show UI to allow user to select source
self.src = self.sm.open_source()
if not self.src:
logging.error("No source selected")
def scan(self):
# this will show UI to allow user to select source
if self.src:
self.src.request_acquire(show_ui=False, modal_ui=False)
(handle, remaining_count) = self.src.xfer_image_natively()
bmp_bytes = twain.dib_to_bm_file(handle)
img = Image.open(BytesIO(bmp_bytes), formats=["bmp"])
width, height = img.size
factor = 600.0 / width
# Storing PhotoImage in global variable to prevent it from being deleted once this function exits
# since PhotoImage has a __del__ destructor
self.scanned_image = ImageTk.PhotoImage(img.resize(size=(int(width * factor), int(height * factor))))
self.frm.destroy()
ttk.Label(root, image=self.scanned_image).pack(side="left", fill="both", expand=1)
else:
print("User clicked cancel")
logging.basicConfig(level=logging.DEBUG)
if __name__ == '__main__':
scan = ScanUtility(scanner_list)
root.mainloop()