-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfscan.py
232 lines (210 loc) · 7.98 KB
/
pdfscan.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
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
import sys
import os
from PyQt5 import QtWidgets, QtGui, QtCore
from PIL import Image
from pdfscangui import Ui_PdfScanDialog
from pathlib import Path
from previewdialog import previewImage
class PdfScan(QtWidgets.QDialog):
def __init__(self, lang, appdir, dev, jpegquality, savefn, parent = None):
super(PdfScan,self).__init__(parent)
self.lang = lang
self.ui = Ui_PdfScanDialog()
self.ui.setupUi(self, self.lang)
self.loadsets()
self.dev = dev
self.parent = parent
self.jpegquality = jpegquality
self.savefn = savefn
self.appdir = appdir
self.cachedir = os.path.join(appdir,'cache')
if not os.path.isdir(self.cachedir):
os.mkdir(self.cachedir)
self.ui.bScan.clicked.connect(self.scan)
self.ui.bSave.clicked.connect(self.save)
self.ui.bUp.clicked.connect(self.moveup)
self.ui.bDown.clicked.connect(self.movedown)
self.ui.bRotate.clicked.connect(self.rotate)
self.ui.bCrop.clicked.connect(self.crop)
self.ui.lw.itemClicked.connect(self.itemClick)
self.ui.bDel.clicked.connect(self.delete_page)
#self.ui.lw.setDragDropMode(QtWidgets.QAbstractItemView.InternalMove)
self.loadimages()
def loadsets(self):
sets = QtCore.QSettings(os.path.join('RoganovSoft', 'Scan'), "config")
x = int(sets.value("PdfScan/left",0))
y = int(sets.value("PdfScan/top",0))
w = int(sets.value("PdfScan/width",0))
h = int(sets.value("PdfScan/height",0))
if x != 0 and y != 0:
self.move(x,y)
if w != 0 and h != 0:
self.resize(w,h)
def savesets(self):
sets = QtCore.QSettings(os.path.join('RoganovSoft', 'Scan'), "config")
p = self.pos()
sets.setValue("PdfScan/left",p.x())
sets.setValue("PdfScan/top",p.y())
sets.setValue("PdfScan/width",self.width())
sets.setValue("PdfScan/height",self.height())
def loadimages(self):
files = os.listdir(self.cachedir)
for f in files:
if 'image_' in f:
im = Image.open(os.path.join(self.cachedir, f))
im.thumbnail((160,160))
tfn = os.path.join(self.cachedir, 'tumb.jpg')
im.save(tfn)
item = QtWidgets.QListWidgetItem()
item.setText(os.path.join(self.cachedir, f))
item.setIcon(QtGui.QIcon(tfn))
self.ui.lw.addItem(item)
def done(self, r):
clear = True
if r == 0:
if os.path.exists(os.path.join(self.cachedir, 'image_0.jpg')):
res = QtWidgets.QMessageBox.question(self, self.lang.tr('cacheimages'),self.lang.tr("removefiles"), QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if res == QtWidgets.QMessageBox.Yes:
clear = True
else:
clear = False
if clear: self.clearcache()
self.savesets()
super(PdfScan,self).done(r)
def clearcache(self):
files = os.listdir(self.cachedir)
for fn in files:
os.remove(os.path.join(self.cachedir, fn))
def doStart(self):
try:
self.dev.start()
except Exception as e:
print(str(e))
return False
else:
return True
def scan(self):
selectedsource = self.dev.source
if selectedsource == 'Flatbed':
if not self.doStart():
QtWidgets.QMessageBox.information(self,self.lang.tr('error'),self.lang.tr('nodocument'))
return
im = self.dev.snap()
fn = os.path.join(self.cachedir,'image')
i = 0
while os.path.exists(f'{fn}_{i}.jpg'): i += 1
fn = f'{fn}_{i}.jpg'
im.save(fn)
im.thumbnail((160,160))
tfn = os.path.join(self.cachedir, 'tumb.jpg')
im.save(tfn)
item = QtWidgets.QListWidgetItem()
item.setText(fn)
item.setIcon(QtGui.QIcon(tfn))
self.ui.lw.addItem(item)
else:
onescanned = False
while self.doStart():
im = self.dev.snap(True)
onescanned = True
fn = os.path.join(self.cachedir,'image')
i = 0
while os.path.exists(f'{fn}_{i}.jpg'): i += 1
fn = f'{fn}_{i}.jpg'
im.save(fn)
im.thumbnail((160,160))
tfn = os.path.join(self.cachedir, 'tumb.jpg')
im.save(tfn)
item = QtWidgets.QListWidgetItem()
item.setText(fn)
item.setIcon(QtGui.QIcon(tfn))
self.ui.lw.addItem(item)
if not onescanned:
QtWidgets.QMessageBox.information(self,self.lang.tr('error'),self.lang.tr('nodocumentfeeder'))
def save(self):
if self.ui.lw.count() == 0:
QtWidgets.QMessageBox.information(self,self.lang.tr('wtf'),self.lang.tr('emptypdf'))
return
if len(self.savefn) > 1:
i = 0
while os.path.exists(f'{self.savefn}_{i}.pdf'):
i += 1
pfn = f'{self.savefn}_{i}.pdf'
else:
if self.parent != None:
lastdir = self.parent.lastdir
else:
lastdir = str(Path.home())
pfn, ext = QtWidgets.QFileDialog.getSaveFileName(self, self.lang.tr("saveas"), os.path.join(lastdir, "untitled"), ".pdf")
if pfn == None or len(pfn) < 2: return
lastdir = os.path.dirname(pfn)
if self.parent != None:
self.parent.lastdir = lastdir
if pfn[-4:] != ext:
pfn += ext
images = []
for i in range(self.ui.lw.count()):
item = self.ui.lw.item(i)
fn = item.text()
images.append(Image.open(fn))
if len(images) > 0:
images[0].save(
pfn, "PDF" ,resolution=self.dev.resolution, quality=self.jpegquality, save_all=True, append_images=images[1:]
)
self.accept()
def moveup(self):
if self.ui.lw.count() < 2 : return
i = self.ui.lw.currentRow()
if i < 1: return
item = self.ui.lw.takeItem(i)
self.ui.lw.insertItem(i - 1, item)
self.ui.lw.setCurrentRow(i - 1)
def movedown(self):
if self.ui.lw.count() < 2 : return
i = self.ui.lw.currentRow()
if i >= self.ui.lw.count() - 1: return
item = self.ui.lw.takeItem(i)
self.ui.lw.insertItem(i + 1, item)
self.ui.lw.setCurrentRow(i + 1)
def rotate(self):
if self.ui.lw.count() < 1 : return
i = self.ui.lw.currentRow()
if i < 0: return
item = self.ui.lw.item(i)
im = Image.open(item.text())
im = im.rotate(180)
im.save(self.ui.lw.item(i).text())
im.thumbnail((160,160))
tfn = os.path.join(self.cachedir, 'tumb.jpg')
im.save(tfn)
item.setIcon(QtGui.QIcon(tfn))
self.itemClick()
def crop(self):
if self.ui.lw.count() < 1 : return
i = self.ui.lw.currentRow()
if i < 0: return
item = self.ui.lw.item(i)
fn = item.text()
if previewImage(fn, self.lang):
im = Image.open(fn)
im.thumbnail((160,160))
tfn = os.path.join(self.cachedir, 'tumb.jpg')
im.save(tfn)
item.setIcon(QtGui.QIcon(tfn))
self.itemClick()
def delete_page(self):
if self.ui.lw.count() < 1 : return
i = self.ui.lw.currentRow()
if i < 0: return
item = self.ui.lw.takeItem(i)
try:
os.remove(item.text())
except:
pass
def itemClick(self):
if self.ui.lw.count() < 1 : return
i = self.ui.lw.currentRow()
if i < 0: return
fn = self.ui.lw.item(i).text()
self.ui.lPreview.setPixmap(QtGui.QPixmap(fn))
self.ui.lPreview.repaint()