-
Notifications
You must be signed in to change notification settings - Fork 27
/
txt2vectorgfx.py
221 lines (175 loc) · 9.43 KB
/
txt2vectorgfx.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
"""
using POTRACE as backend cmd line tool for vectorizing SD output
This script will download from
https://potrace.sourceforge.net/#downloading
the windows exetuable (todo: mac, linux support)
Potrace is under GPL, you can download the source from the url above.
If you dont want to download that, please install POTRACE to your
system manually and assign it to your PATH env variable properly.
"""
PO_URL = "https://potrace.sourceforge.net/download/1.16/potrace-1.16.win64.zip"
PO_ZIP = "potrace.zip"
PO_ZIP_EXE = "potrace-1.16.win64/potrace.exe"
PO_EXE = "scripts/potrace.exe"
# not yet
BASE_PROMPT=",(((lineart))),((low detail)),(simple),high contrast,sharp,2 bit"
BASE_NEGPROMPT="(((text))),((color)),(shading),background,noise,dithering,gradient,detailed,out of frame,ugly,error,Illustration, watermark"
BASE_STEPS=40
BASE_SCALE=10
StyleDict = {
"Illustration":BASE_PROMPT+",(((vector graphic))),medium detail",
"Logo":BASE_PROMPT+",(((centered vector graphic logo))),negative space,stencil,trending on dribbble",
"Drawing":BASE_PROMPT+",(((cartoon graphic))),childrens book,lineart,negative space",
"Artistic":BASE_PROMPT+",(((artistic monochrome painting))),precise lineart,negative space",
"Tattoo":BASE_PROMPT+",(((tattoo template, ink on paper))),uniform lighting,lineart,negative space",
"Gothic":BASE_PROMPT+",(((gothic ink on paper))),H.P. Lovecraft,Arthur Rackham",
"Anime":BASE_PROMPT+",(((clean ink anime illustration))),Studio Ghibli,Makoto Shinkai,Hayao Miyazaki,Audrey Kawasaki",
"Cartoon":BASE_PROMPT+",(((clean ink funny comic cartoon illustration)))",
"Sticker":",(Die-cut sticker, kawaii sticker,contrasting background, illustration minimalism, vector, pastel colors)",
"Gold Pendant": ",gold dia de los muertos pendant, intricate 2d vector geometric, cutout shape pendant, blueprint frame lines sharp edges, svg vector style, product studio shoot",
"None - prompt only":""
}
##########################################################################
import os
import pathlib
import subprocess
from PIL import Image
from tkinter import Image, image_types
from zipfile import ZipFile
import requests
import glob
import os.path
from sys import platform
import time
import modules.scripts as scripts
import modules.images as Images
import gradio as gr
from modules.processing import Processed, process_images
from modules.shared import opts
class Script(scripts.Script):
def title(self):
return "Text to Vector Graphics"
def ui(self, is_img2img):
with gr.Row():
poUseColor = gr.Radio(list(StyleDict.keys()), label="Visual style", value="Illustration")
with gr.Row():
with gr.Column():
with gr.Box():
with gr.Group():
with gr.Row():
poDoVector = gr.Checkbox(label="Enable Vectorizing", value=True)
poFormat = gr.Dropdown(["svg","pdf"], label="Output format", value="svg")
poOpaque = gr.Checkbox(label="White is Opaque", value=True)
poTight = gr.Checkbox(label="Cut white margin from input", value=True)
with gr.Row():
poKeepPnm = gr.Checkbox(label="Keep temp images", value=False)
poThreshold = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, step=0.05, value=0.5)
with gr.Column():
with gr.Box():
with gr.Group():
poTransPNG = gr.Checkbox(label="Transparent PNG",value=False)
poTransPNGEps = gr.Slider(label="Noise Tolerance",minimum=0,maximum=128,value=16)
poTransPNGQuant = gr.Slider(label="Quantize",minimum=2,maximum=255,value=16)
return [poUseColor,poFormat, poOpaque, poTight, poKeepPnm, poThreshold, poTransPNG, poTransPNGEps,poDoVector,poTransPNGQuant]
def run(self, p, poUseColor,poFormat, poOpaque, poTight, poKeepPnm, poThreshold, poTransPNG, poTransPNGEps,poDoVector, poTransPNGQuant):
PO_TO_CALL = self.check_Potrace_install()
p.do_not_save_grid = True
# Add the prompt from above
p.prompt += StyleDict[poUseColor]
proc = process_images(p)
mixedImages = []
try:
# vectorize
for i,img in enumerate(proc.images[::-1]):
if (not hasattr(img,"already_saved_as")) : continue
fullfn = img.already_saved_as
fullfnPath = pathlib.Path(fullfn)
fullofpnm = fullfnPath.with_suffix('.pnm') #for vectorizing
fullofTPNG = fullfnPath.with_stem(fullfnPath.stem+ "_T")
fullofTPNG = fullofTPNG.with_suffix('.png')
fullof = pathlib.Path(fullfn).with_suffix('.'+poFormat)
mixedImages.append([img,"PNG"])
# set transparency to PNG, actually not vector feature, but people need it
if poTransPNG:
self.doTransPNG(poTransPNGEps, mixedImages, img, fullofTPNG, poTransPNGQuant)
if poDoVector:
self.doVector(poFormat, poOpaque, poTight, poKeepPnm, poThreshold, PO_TO_CALL, img, fullofpnm, fullof, mixedImages)
except (Exception):
raise Exception("TXT2Vectorgraphics: Execution of Potrace failed, check filesystem, permissions, installation or settings (is image saving on?)")
return Processed(p, mixedImages, p.seed, proc.info)
def doVector(self, poFormat, poOpaque, poTight, poKeepPnm, poThreshold, PO_TO_CALL, img, fullofpnm, fullof, mixedImages):
# for vectorizing
img.save(fullofpnm)
args = [PO_TO_CALL, "-b", poFormat, "-o", fullof, "--blacklevel", format(poThreshold, 'f')]
if poOpaque: args.append("--opaque")
if poTight: args.append("--tight")
args.append(fullofpnm)
p2 = subprocess.Popen(args)
if not poKeepPnm:
p2.wait()
os.remove(fullofpnm)
abspathsvg = os.path.abspath(fullof)
mixedImages.append([abspathsvg,"SVG"]) # img, caption
def doTransPNG(self, poTransPNGEps, mixedImages, img, fullofTPNG, poTransPNGQuant):
#Image.quantize(colors=256, method=None, kmeans=0, palette=None)
imgQ = img.quantize(colors=poTransPNGQuant, kmeans=0, palette=None)
histo = imgQ.histogram()
# get first pixel and assume it is background, best with Sticker style
if (imgQ):
bgI = imgQ.getpixel((0,0)) # return pal index
bg = list(imgQ.palette.colors.keys())[bgI]
E = poTransPNGEps # tolerance range if noisy
imgT=imgQ.convert('RGBA')
datas = imgT.getdata()
newData = []
for item in datas:
if (item[0] > bg[0]-E and item[0] < bg[0]+E) and (item[1] > bg[1]-E and item[1] < bg[1]+E) and (item[2] > bg[2]-E and item[1] < bg[2]+E):
newData.append((255, 255, 255, 0))
else:
newData.append(item)
imgT.putdata(newData)
imgT.save(fullofTPNG)
mixedImages.append([imgQ,"PNG-quantized"])
mixedImages.append([imgT,"PNG-transparent"])
def check_Potrace_install(self) -> str:
# For Linux, run potrace from installed binary
if platform == "darwin":
try:
# check whether already in PATH
checkPath = subprocess.Popen(["potrace","-v"])
checkPath.wait()
return "potrace"
except (Exception):
raise Exception("Cannot find installed Protrace on Mac. Please run `brew install potrace`")
elif platform == "linux"or platform == "linux2":
try:
# check whether already in PATH
checkPath = subprocess.Popen(["potrace","-v"])
checkPath.wait()
return "potrace"
except (Exception):
raise Exception("Cannot find installed Potrace. Please run `sudo apt install potrace`")
# prefer local potrace over that from PATH
elif platform == "win32":
if not os.path.exists(PO_EXE):
try:
# check whether already in PATH
checkPath = subprocess.Popen(["potrace","-v"])
checkPath.wait()
return "potrace"
except (Exception):
try:
# try to download Potrace and unzip locally into "scripts"
if not os.path.exists(PO_ZIP):
r = requests.get(PO_URL)
with open(PO_ZIP, 'wb') as f:
f.write(r.content)
with ZipFile(PO_ZIP, 'r') as zipObj:
exe = zipObj.read(PO_ZIP_EXE)
with open(PO_EXE,"wb") as e:
e.write(exe)
zipObj.close()
os.remove(PO_ZIP)
except:
raise Exception("Cannot find and or download/extract Potrace. Provide Potrace in script folder. ")
return PO_EXE