-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
350 lines (290 loc) · 12.2 KB
/
main.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import gi
import img_ascii as ia
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository import GdkPixbuf
from gi.repository import GLib
from gi.repository import Gio
import sys
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="Pic to Ascii-Art", application=app)
### VARIABLES ###
self.path = ''
self.resultado = 0
### CONTAINER ###
self.grid = Gtk.Grid()
self.add(self.grid)
### ELEMENTS ###
self.all_in_screen()
### ACTIONS ###
# action without a state created
new_action = Gio.SimpleAction.new("new", None)
# action connected to the callback function
new_action.connect("activate", self.new_callback)
# action added to the application
self.add_action(new_action)
# action without a state created
save_action = Gio.SimpleAction.new("save", None)
# action connected to the callback function
save_action.connect("activate", self.save_callback)
# action added to the application
self.add_action(save_action)
# action without a state created (name, parameter type)
login_action = Gio.SimpleAction.new("login", None)
# connected with the callback function
login_action.connect("activate", self.login_callback)
# added to the window
self.add_action(login_action)
# action without a state created (name, parameter type)
logout_action = Gio.SimpleAction.new("logout", None)
# connected with the callback function
logout_action.connect("activate", self.logout_callback)
# added to the window
self.add_action(logout_action)
# action with a state created (name, parameter type, initial state)
shape_action = Gio.SimpleAction.new_stateful(
"shape", GLib.VariantType.new('s'), GLib.Variant.new_string('line'))
# connected to the callback function
shape_action.connect("activate", self.shape_callback)
# added to the window
self.add_action(shape_action)
# action with a state created
about_action = Gio.SimpleAction.new("about", None)
# action connected to the callback function
about_action.connect("activate", self.about_callback)
# action added to the application
self.add_action(about_action)
# action with a state created
source_action = Gio.SimpleAction.new("source", None)
# action connected to the callback function
source_action.connect("activate", self.source_callback)
# action added to the application
self.add_action(source_action)
def all_in_screen(self):
scale_lbl = Gtk.Label(label='Scale (percentage)')
adjustment = Gtk.Adjustment(upper=200,
step_increment=1, page_increment=10)
self.scale_btn = Gtk.SpinButton()
self.scale_btn.set_adjustment(adjustment)
self.scale_btn.set_value(50)
self.grid.attach(scale_lbl,1,6,2,1)
self.grid.attach(self.scale_btn,1,7,2,1)
generate_btn = Gtk.Button(label='Generate\nimage')
generate_btn.connect("clicked", self.gen)
self.grid.attach(generate_btn,5,6,2,2)
tuit_btn = Gtk.Button(label='Share with\nTwitter')
tuit_btn.connect("clicked", self.tuitear)
self.grid.attach(tuit_btn,5,9,2,1)
invertcolors = Gtk.Button(label='invert image\ncolors')
invertcolors.connect("clicked", self.inversion)
self.grid.attach(invertcolors,5,8,2,1)
quality_lbl = Gtk.Label(label='Quality')
quality_btn = Gtk.ListStore(str)
opciones = [''.join(i[1]) for i in ia.chars.items()]
for opcion in opciones:
quality_btn.append([opcion])
self.quality_combo = Gtk.ComboBox.new_with_model(quality_btn)
renderer_text = Gtk.CellRendererText()
self.quality_combo.pack_start(renderer_text, True)
self.quality_combo.add_attribute(renderer_text, "text", 0)
self.grid.attach(quality_lbl,9,6,2,1)
self.grid.attach(self.quality_combo,9,7,2,1)
def inversion(self, widget):
if self.resultado != 0:
self.resultado.invertc()
else:
print('no generaste la imagen')
def tuitear(self, widget):
if self.resultado != 0:
try:
self.resultado.tweet()
print('done')
except:
print('Imagen muy pesada')
else:
print('no generaste la imagen')
def gen(self, widget):
if self.path != '':
tree_iter = self.quality_combo.get_active_iter()
if tree_iter is not None:
model = self.quality_combo.get_model()
posibilidad = model[tree_iter][0]
for i in ia.chars.items():
if list(i[1]) == posibilidad:
calidad = i[0]
else:
calidad = '16ch'
if self.scale_btn.get_value_as_int() == 0:
escala = 50
else:
escala = self.scale_btn.get_value_as_int()
self.resultado = ia.ImgToAscii(self.path,
escala/100,
calidad)
self.resultado.to_blackwhite()
self.resultado.rescale()
self.resultado.imgmatrix()
self.resultado.to_pic()
else:
print('no se seleccionó imagen')
# callback function for new
def new_callback(self, action, widget):
print("You clicked \"New\"")
dialog = Gtk.FileChooserDialog(
title="Please choose a file",
parent=self,
action=Gtk.FileChooserAction.OPEN
)
dialog.add_buttons(
Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN,
Gtk.ResponseType.OK,
)
self.add_filters(dialog)
response = dialog.run()
if response == Gtk.ResponseType.OK:
self.path = dialog.get_filename()
elif response == Gtk.ResponseType.CANCEL:
pass
dialog.destroy()
# filters for new function
def add_filters(self, dialog):
filter_img = Gtk.FileFilter()
filter_img.set_name("Images")
filter_img.add_pattern('*.jpeg')
filter_img.add_pattern('*.jpg')
filter_img.add_pattern('*.png')
filter_img.add_pattern('*.eps')
dialog.add_filter(filter_img)
# callback function for save
def save_callback(self, action, parameter):
print("You clicked \"Save\"")
# callback function for copy_action
def login_callback(self, action, parameter):
print("\"Login\" activated")
if not ia.is_logged():
dialog = TwitterDialog(self)
response = dialog.run()
if response == Gtk.ResponseType.OK:
pin = dialog.entry.get_text()
ia.get_logged(dialog.auth, pin)
elif response == Gtk.ResponseType.CANCEL:
pass
dialog.destroy()
else:
print("You are already logged")
# callback function for paste_action
def logout_callback(self, action, parameter):
print("\"Logout\" activated")
ia.logout()
# callback function for shape_action
def shape_callback(self, action, parameter):
print("Shape is set to", parameter.get_string())
# Note that we set the state of the action!
action.set_state(parameter)
def source_callback(self, action, parameter):
ia.webbrowser.open_new("https://github.com/AmadoCab/Proyecto3-progra")
# callback function for about (see the AboutDialog example)
def about_callback(self, action, parameter):
# Instance of Gtk.AboutDialog
aboutdialog = Gtk.AboutDialog()
# Varibles of the aboutdialog
image = GdkPixbuf.Pixbuf.new_from_file_at_scale('icono.png',4*12,5*12,True)
authors = ["Amado Alberto Cabrera Estrada"]
comments = "Implementación de un conversor de imágenes a Ascii-Art que permite compartir imágenes hechas de caracteres a través de Twitter."
version = "1.0"
# Fill the aboutdialog
aboutdialog.set_logo(image)
aboutdialog.set_program_name("Pic to Ascii-Art")
aboutdialog.set_copyright("Copyright \xc2\xa9 2020 Amado C.")
aboutdialog.set_authors(authors)
aboutdialog.set_comments(comments)
aboutdialog.set_website("https://github.com/AmadoCab/Proyecto3-progra")
aboutdialog.set_website_label("Github Source Code")
aboutdialog.set_version(version)
# to close the aboutdialog when "close" is clicked we connect the
# "response" signal to on_close
aboutdialog.connect("response", self.on_close)
# show the aboutdialog
aboutdialog.show()
# a callback function to destroy the aboutdialog
def on_close(self, action, parameter):
action.destroy()
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
# FIRST THING TO DO: do_startup()
Gtk.Application.do_startup(self)
# action without a state created
quit_action = Gio.SimpleAction.new("quit", None)
# action connected to the callback function
quit_action.connect("activate", self.quit_callback)
# action added to the application
self.add_action(quit_action)
# action with a state created
state_action = Gio.SimpleAction.new_stateful(
"state", GLib.VariantType.new('s'), GLib.Variant.new_string('off'))
# action connected to the callback function
state_action.connect("activate", self.state_callback)
# action added to the application
self.add_action(state_action)
# action with a state created
awesome_action = Gio.SimpleAction.new_stateful(
"awesome", None, GLib.Variant.new_boolean(False))
# action connected to the callback function
awesome_action.connect("activate", self.awesome_callback)
# action added to the application
self.add_action(awesome_action)
# a builder to add the UI designed with Glade to the grid:
builder = Gtk.Builder()
# get the file (if it is there)
try:
builder.add_from_file("menubar.xml")
except:
print("file not found")
sys.exit()
# we use the method Gtk.Application.set_menubar(menubar) to add the menubar
# and the menu to the application (Note: NOT the window!)
self.set_menubar(builder.get_object("menubar"))
self.set_app_menu(builder.get_object("appmenu"))
# callback function for quit
def quit_callback(self, action, parameter):
print("You clicked \"Quit\"")
sys.exit()
# callback function for state
def state_callback(self, action, parameter):
print("State is set to", parameter.get_string())
action.set_state(parameter)
# callback function for awesome
def awesome_callback(self, action, parameter):
action.set_state(GLib.Variant.new_boolean(not action.get_state()))
if action.get_state().get_boolean() is True:
print("You checked \"Awesome\"")
else:
print("You unchecked \"Awesome\"")
class TwitterDialog(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, title="Twitter Login", transient_for=parent, flags=0)
self.add_buttons(
Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OK, Gtk.ResponseType.OK
)
self.set_default_size(150, 100)
vbox = self.get_content_area()
label = Gtk.Label(label="Insert your pin")
self.en = Gtk.Entry()
vbox.pack_start(label, True, True, 0)
vbox.pack_end(self.entry, True, True, 0)
self.show_all()
consumer_key = ia.consumer_key
consumer_secret = ia.consumer_secret
self.auth = ia.tweepy.OAuthHandler(consumer_key, consumer_secret)
ia.webbrowser.open(self.auth.get_authorization_url())
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)