-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-svg-editor.py
More file actions
227 lines (185 loc) · 6.19 KB
/
python-svg-editor.py
File metadata and controls
227 lines (185 loc) · 6.19 KB
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
import sys
import tkinter
import cairosvg
from PIL import Image, ImageTk
from io import BytesIO
from pathlib import Path
import tkinter.filedialog
from tkinterdnd2 import DND_FILES, TkinterDnD
filepath = ""
svgText = ""
def on_drop(event):
"""
Support for drag-and-drop of files onto the UI.
"""
global svgText
global filepath
dropPath = event.data
print(dropPath)
if dropPath[:1] == "{":
dropPath = dropPath[1:-1]
print(dropPath)
if dropPath != '' and dropPath[-4:] == ".svg":
filepath = dropPath
svgText = ReadSvgFile(filepath)
filename = Path(filepath).name
main.title(f"SVG Editor - {filename}")
DisplayImage(svgText)
def WriteSvgFile(filename, text):
svgFile = open(filename, 'w')
svgFile.write(text)
svgFile.close()
def ReadSvgFile(filename):
svgFile = open(filename, 'r')
text = svgFile.read()
svgFile.close()
return text
def CreateDisplayImage(svgText, scale, dpi):
svg = cairosvg.svg2svg(svgText, dpi=(dpi / scale))
bytes = cairosvg.svg2png(svg)
img = Image.open(BytesIO(bytes))
return img
def setFilePathGlobal(path):
global filepath
filepath = path
def setSvgTextGlobal(text):
global svgText
svgText = text
def OpenCommand():
global filepath
global svgText
filepath = openFile()
svgText = ReadSvgFile(filepath)
filename = Path(filepath).name
main.title(f"SVG Editor - {filename}")
DisplayImage(svgText)
def DisplayImage(svgText):
sourceText.delete("1.0", tkinter.END)
sourceText.insert(tkinter.END, svgText)
img = CreateDisplayImage(svgText, 1, 96)
tkimg = ImageTk.PhotoImage(img)
imageLabel.config(image=tkimg)
imageLabel.image = tkimg
def SaveCommand():
global filepath
global svgText
svgText = sourceText.get("1.0", "end-1c")
WriteSvgFile(filepath, svgText)
svgText = ReadSvgFile(filepath)
filename = Path(filepath).name
main.title(f"SVG Editor - {filename}")
DisplayImage(svgText)
def SaveAsCommand():
global filepath
global svgText
svgText = sourceText.get("1.0", "end-1c")
newFilepath = tkinter.filedialog.asksaveasfilename(
title="Save As",
defaultextension=".svg",
filetypes=[("SVG files", "*.svg"), ("All files", "*.*")]
)
filepath = newFilepath
WriteSvgFile(newFilepath, svgText)
filename = Path(newFilepath).name
main.title(f"SVG Editor - {filename}")
def NewCommand():
global filepath
global svgText
svgText = '''<svg height="100" width="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
</svg>
'''
newFilepath = tkinter.filedialog.asksaveasfilename(
title="New SVG",
defaultextension=".svg",
filetypes=[("SVG files", "*.svg"), ("All files", "*.*")]
)
filepath = newFilepath
WriteSvgFile(newFilepath, svgText)
filename = Path(newFilepath).name
main.title(f"SVG Editor - {filename}")
DisplayImage(svgText)
def openFile():
global filepath
filepath = tkinter.filedialog.askopenfilename(
filetypes=[("SVG Files", "*.svg"), ("All Files", "*.*")]
)
return filepath
def SavePNGCommand():
global svgText
if svgText != "":
newFilepath = tkinter.filedialog.asksaveasfilename(
title="PNG",
defaultextension=".png",
filetypes=[("SVG files", "*.png"), ("All files", "*.*")]
)
img = CreateDisplayImage(svgText, 1, 96)
img.save(newFilepath)
def SaveICOCommand():
global svgText
if svgText != "":
newFilepath = tkinter.filedialog.asksaveasfilename(
title="ICO",
defaultextension=".ico",
filetypes=[("SVG files", "*.ico"), ("All files", "*.*")]
)
img = CreateDisplayImage(svgText, 1, 96)
img.save(newFilepath,
format='ICO',
sizes=[
(32, 32),
(48, 48),
(64, 64),
(128, 128),
(256, 256),
(512, 512)
])
main = TkinterDnD.Tk()
# main = tkinter.Tk()
main.title("SVG Editor")
main.geometry("1000x400")
main.grid_columnconfigure(0, weight=1, uniform="equal")
main.grid_columnconfigure(1, weight=1, uniform="equal")
main.grid_rowconfigure(0, weight=1)
menubar = tkinter.Menu(main)
main.config(menu=menubar)
filemenu = tkinter.Menu(menubar, tearoff=False)
exportMenu = tkinter.Menu(filemenu, tearoff=False)
menubar.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=NewCommand)
filemenu.add_command(label="Open", command=OpenCommand)
filemenu.add_command(label="Save As", command=SaveAsCommand)
filemenu.add_cascade(label="Export", menu=exportMenu)
exportMenu.add_command(label="Export to PNG", command=SavePNGCommand)
exportMenu.add_command(label="Export to ICO", command=SaveICOCommand)
filemenu.add_command(label='Exit', command=main.destroy)
menubar.add_command(label="Save", command=SaveCommand)
sourceFrame = tkinter.Frame(master=main, bg="orange")
sourceFrame.grid(row=0, column=0, sticky='nsew')
vScrollbar = tkinter.Scrollbar(sourceFrame, orient="horizontal")
vScrollbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
hScrollbar = tkinter.Scrollbar(sourceFrame)
hScrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
sourceText = tkinter.Text(
sourceFrame,
yscrollcommand=hScrollbar.set,
xscrollcommand=vScrollbar.set,
wrap="none")
hScrollbar.config(command=sourceText.yview)
vScrollbar.config(command=sourceText.xview)
sourceText.pack(fill=tkinter.BOTH, expand=True)
sourceText.insert(tkinter.END, svgText)
imageFrame = tkinter.Frame(master=main, bg="pink")
imageFrame.grid(row=0, column=1, sticky='nsew')
imageLabel = tkinter.Label(imageFrame) # , image=tkimg)
imageLabel.pack(fill=tkinter.BOTH, expand=True)
startingPath = sys.argv[1] if len(sys.argv) >= 2 else ''
if startingPath != '' and startingPath[-4:] == ".svg":
setFilePathGlobal(startingPath)
svgText = ReadSvgFile(startingPath)
setSvgTextGlobal(svgText)
filename = Path(startingPath).name
main.title(f"SVG Editor - {filename}")
DisplayImage(svgText)
main.drop_target_register(DND_FILES)
main.dnd_bind("<<Drop>>", on_drop)
main.mainloop()