forked from thunil/mantaflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweak_images.py
102 lines (81 loc) · 3.52 KB
/
tweak_images.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
# There's a break in the end of the loop to prevent it from recurse
import sys, os, re, shutil
import colorama
# I use backslash, since I get files with backslashes from the os
path = '.\\'
bRecursive = 0
colorama.init()
lf = []
for root, dirs, files in os.walk(path):
for file in files:
m = re.match('^(.+)\.(png|jpg|bmp)$', file)
if m:
ext = m.group(2)
fl = os.path.join(root, file)
print(colorama.Fore.GREEN + colorama.Style.BRIGHT + fl + colorama.Style.RESET_ALL)
# replace color
# --opaque, -fill: replace color of opaque with fill
# -fuzz: acceptable distance from opaque color
if 0:
cmd = 'convert "%s" -fuzz 5%% -opaque "rgb(170,170,170)" -fill white "%s"' % (fl,fl)
#print cmd
os.system(cmd)
# convert bmp to png (png is lossless, jpg isn't unless it's .jp2 -- jpg2000)
if 0 and ext.lower() == 'bmp':
dest = os.path.join(root, m.group(1) + '.png')
print(colorama.Fore.GREEN + colorama.Style.BRIGHT + fl + " => " + dest + colorama.Style.RESET_ALL)
cmd = "convert %s %s" % (fl, dest)
os.system(cmd)
fl = dest
# convert images to low res .pdf
if 0:
dest = os.path.join(root, m.group(1) + '.pdf')
print(colorama.Fore.GREEN + colorama.Style.BRIGHT + fl + " => " + dest + colorama.Style.RESET_ALL)
#cmd = "convert %s %s" % (fl, dest)
cmd = "convert %s -resample 16 %s" % (fl, dest)
os.system(cmd)
# convert to black and white
if 0:
cmd = 'convert "%s" -threshold 70%% "%s"' % (fl,fl)
#print cmd
os.system(cmd)
# fill background in white
if 0:
# fuzz was 20
cmd = 'convert "%s" -fuzz 2%% -fill white -draw "color 0,0 floodfill" "%s"' % (fl,fl)
#print cmd
os.system(cmd)
# trim borders
if 1:
cmd = 'convert "%s" -trim "%s"' % (fl,fl)
#print cmd
os.system(cmd)
# add white border on left and right
if 0:
cmd = 'convert "%s" -bordercolor white -border 10x0 "%s"' % (fl,fl)
#print cmd
os.system(cmd)
# add white border on all sides
if 0:
cmd = 'convert "%s" -bordercolor white -border 10 "%s"' % (fl,fl)
#print cmd
os.system(cmd)
# set transparent background
if 0:
cmd = 'convert "%s" -transparent white "%s"' % (fl,fl)
os.system(cmd)
# density
if 0:
cmd = 'convert "%s" -units PixelsPerInch -density 72 "%s"' % (fl,fl)
#print cmd
os.system(cmd)
# resample to low resolution (e.g. to create draft for latex)
if 0:
cmd = 'convert "%s" -resample 8 "%s"' % (fl,fl)
#print cmd
os.system(cmd)
if not bRecursive:
break #prevent descending into subfolders
if 0:
print('\nPress Enter...')
input()