-
Notifications
You must be signed in to change notification settings - Fork 5
/
CompressFile.py
67 lines (53 loc) · 2.18 KB
/
CompressFile.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
import os
import compressOnline
# https://askubuntu.com/questions/113544/how-can-i-reduce-the-file-size-of-a-scanned-pdf-file
def main():
# download_file("http://epaper.jagran.com/epaperimages/30012018/muzaffarpur/29smt-pg1-0.pdf","document.pdf")
filestatus = os.path.isfile('aa1.pdf')
if filestatus :
print "file already downloaded"
# compress('aa1.pdf','o.pdf')
# compressOnline.compress('25032018.pdf','co.pdf')
# dir_path = os.path.dirname(os.path.realpath(__file__))
# print dir_path
i =0
while i<1:
print i
i = i+1
byte= os.stat('25032018.pdf').st_size
print byte #29743629
compress('2.pdf','out2.pdf')
percent = 15
# print "Compressed by",(percent), '%'
def compress( input_file_path, out_file_path):
# os.system("ps2pdf -dPDFSETTINGS=/ebook %s %s" % (input_file_path, out_file_path))
temp = out_file_path
temp1 = input_file_path
i = 0
while i<10 :
# print i
# print input_file_path, out_file_path
os.system("gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=%s %s "% ( out_file_path ,input_file_path))
# byte_out= os.stat(out_file_path).st_size
# byte_in = os.stat(input_file_path).st_size
# print byte_out, " ", byte_in - byte_out
# temp = out_file_path
input_file_path = out_file_path
out_file_path = str(i) + 'o.pdf'
filestatus = os.path.isfile(str(i-2) + 'o.pdf')
if filestatus :
os.remove(str(i-2) + 'o.pdf')
# print "file del", str(i-2) + 'o.pdf'
i = i +1
input_file_path = str(i-2) + 'o.pdf'
out_file_path = temp
os.system("gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=%s %s "% ( out_file_path ,input_file_path))
os.remove(input_file_path)
byte_out= os.stat(out_file_path).st_size
byte_in = os.stat(temp1).st_size
percent = ((byte_in - byte_out)*100)/byte_in
# print byte
# compressOnline.compress(input_file_path,out_file_path)
print "-------Compressed by %s" %(percent),'%'
if __name__ == "__main__":
main()