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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
|
#!/usr/bin/env python3
# Photo Crop - photo crop to size app to print in the most popular photo paper sizes
# author: Przemysław R. Pietraszczyk
# license: GPL v.2
# date 21-01-2022
# editor: Geany
import sys
import cairo
import gi, os
import time
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GdkPixbuf, Gdk
from gi.repository.GdkPixbuf import Pixbuf
UI_INFO = """
<ui>
<menubar name='MenuBar'>
<menu action='FileMenu'>
<menuitem action='FileLoad' />
<menuitem action='FileSave' />
<menuitem action='FilePrint' />
<separator />
<menuitem action='FileQuit' />
</menu>
<menu action='EditMenu'>
<menuitem action='Rotate'/>
</menu>
<menu action='InfoMenu'>
<menuitem action='Help'/>
<menuitem action='About'/>
<separator />
<menuitem action='License'/>
</menu>
</menubar>
</ui>
"""
"""
# szkielet z podmenu
<ui>
<menubar name='MenuBar'>
<menu action='FileMenu'>
<menu action='FileLoad'>
<menuitem action='LoadImg' />
</menu>
<menuitem action='FilePrint' />
<separator />
<menuitem action='FileQuit' />
</menu>
<menu action='InfoMenu'>
<menuitem action='Help'/>
<menuitem action='About'/>
</menu>
</menubar>
</ui>
"""
grid = Gtk.Grid()
rotate = False
file_img_selected = ""
def get_resource_path(rel_path):
dir_of_py_file = os.path.dirname(__file__)
rel_path_to_resource = os.path.join(dir_of_py_file, rel_path)
abs_path_to_resource = os.path.abspath(rel_path_to_resource)
return abs_path_to_resource
class FileChooserIMGLoad(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Selection of graphic files")
global file_img_selected
dialog = Gtk.FileChooserDialog(title="Selection of graphic files", 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:
print("Open clicked")
print("File selected: " + dialog.get_filename())
file_img_selected=dialog.get_filename()
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()
def add_filters(self, dialog):
filter_jpeg = Gtk.FileFilter()
filter_jpeg.set_name("Files JPEG")
filter_jpeg.add_mime_type("image/jpeg")
dialog.add_filter(filter_jpeg)
filter_png = Gtk.FileFilter()
filter_png.set_name("Files PNG")
filter_png.add_mime_type("image/png")
dialog.add_filter(filter_png)
"""
filter_png = Gtk.FileFilter()
filter_png.set_name("Files TIFF")
filter_png.add_mime_type("image/tiff")
dialog.add_filter(filter_png)
"""
class FileChooserIMGSave(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Save the cropped photo")
global file_img_selected
dialog = Gtk.FileChooserDialog(title="Save the cropped photo", parent=self, action=Gtk.FileChooserAction.SAVE)
dialog.add_buttons(
Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
Gtk.STOCK_SAVE,
Gtk.ResponseType.OK,
)
self.add_filters(dialog)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("Open clicked")
print("File selected: " + dialog.get_filename())
file_img_selected=dialog.get_filename()
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()
def add_filters(self, dialog):
filter_jpeg = Gtk.FileFilter()
filter_jpeg.set_name("Files JPEG")
filter_jpeg.add_mime_type("image/jpeg")
dialog.add_filter(filter_jpeg)
filter_png = Gtk.FileFilter()
filter_png.set_name("Files PNG")
filter_png.add_mime_type("image/png")
dialog.add_filter(filter_png)
"""
filter_png = Gtk.FileFilter()
filter_png.set_name("Files TIFF")
filter_png.add_mime_type("image/tiff")
dialog.add_filter(filter_png)
"""
class DialogWarning(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, title="Attention!", transient_for=parent, flags=0)
self.props.border_width = 20
self.add_buttons(
Gtk.STOCK_OK, Gtk.ResponseType.OK
)
self.set_default_size(150, 100)
label1 = Gtk.Label(label="The frame is outside the canvas!\n")
box = self.get_content_area()
box.add(label1)
self.show_all()
class DialogCropWarning(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, title="Attention!", transient_for=parent, flags=0)
self.props.border_width = 20
self.add_buttons(
Gtk.STOCK_OK, Gtk.ResponseType.OK
)
self.set_default_size(150, 100)
label1 = Gtk.Label(label="Crop the photo first!\n")
box = self.get_content_area()
box.add(label1)
self.show_all()
class DialogHelp(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, title="Help", transient_for=parent, flags=0)
self.props.border_width = 20
self.add_buttons(
Gtk.STOCK_OK, Gtk.ResponseType.OK
)
self.set_default_size(150, 100)
label1 = Gtk.Label(label="An application for cropping photos to the most popular\nformats [13x18, 10x15, A4]\n\nScaling the size of the frame - mouse wheel with the CTRL key pressed\nMoving the frame - pressed LMB and moving the mouse\nin the desired direction.")
#label2 = Gtk.Label(lanel="")
box = self.get_content_area()
box.add(label1)
#box.add(label2)
self.show_all()
class DialogAbout(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, title="About", transient_for=parent, flags=0)
self.props.border_width = 20
self.add_buttons(
Gtk.STOCK_OK, Gtk.ResponseType.OK
)
self.set_default_size(150, 100)
label = Gtk.Label(label="\tThe application is based on:")
box = self.get_content_area()
box.add(label)
button = Gtk.LinkButton("https://python-gtk-3-tutorial.readthedocs.io/", label="https://python-gtk-3-tutorial.readthedocs.io/")
box.add(button)
label2 = Gtk.Label(label="\n\tVersion: 0.230112-1~alpha\n\n\tPrzemysław R. Pietraszczyk\n\n\t\t January 2022\n\n\n")
box.add(label2)
button = Gtk.LinkButton("https://prymula.ct8.pl", label="Site")
box.add(button)
self.show_all()
class DialogLicense(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, title="License", transient_for=parent, flags=0)
self.props.border_width = 20
self.add_buttons(
Gtk.STOCK_OK, Gtk.ResponseType.OK
)
self.set_default_size(150, 100)
label = Gtk.Label(label="This program is distributed without any warranty. More information:\n")
box = self.get_content_area()
box.add(label)
button = Gtk.LinkButton("https://www.gnu.org/licenses/old-licenses/gpl-2.0.html", label="GNU General License version => 2")
box.add(button)
self.show_all()
class Brush(object):
default_rgba_color = (0, 0, 0, 1)
def __init__(self, width=None, rgba_color=None):
if rgba_color is None:
rgba_color = self.default_rgba_color
if width is None:
width = 3
self.__width = width
self.__rgba_color = rgba_color
self.__stroke = []
self.__current_line = []
def _line_ended(self):
self.__stroke.append(self.__current_line.copy())
self.__current_line = []
def _add_point(self, point):
self.__current_line.append(point)
def _draw(self, cairo_context):
cairo_context.set_source_rgba(*self.__rgba_color)
cairo_context.set_line_width(self.__width)
cairo_context.set_line_cap(cairo.LINE_CAP_ROUND)
cairo_context.new_path()
for line in self.__stroke:
for x, y in line:
cairo_context.line_to(x, y)
cairo_context.new_sub_path()
for x, y in self.__current_line:
cairo_context.line_to(x, y)
cairo_context.stroke()
# ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ #
# ~ Getters & Setters ~ #
# ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ #
def _get_width(self):
return self.__width
def _set_width(self, width):
self.__width = width
def _get_rgba_color(self):
return self.__rgba_color
def _set_rgba_color(self, rgba_color):
self.__rgba_color = rgba_color
def _get_stroke(self):
return self.__stroke
def _get_current_line(self):
return self.__current_line
class MyWindow(Gtk.Window):
def __init__(self):
super().__init__()
self.init_ui()
def on_drawing_area_draw(self, drawable, cairo_context):
global rotate
start = time.time()
self.brush = Brush()
# DrawingArea size depends on Pixbuf size
#self.drawing_area.get_window().resize(self.displayed_pixbuf.get_width(),
# self.displayed_pixbuf .get_height())
#self.drawing_area.set_size_request(self.displayed_pixbuf.get_width(),
# self.displayed_pixbuf.get_height())
# (x, y) offsets
pixbuf_x = 0 #int(self.viewport.get_hadjustment().get_value())
pixbuf_y = 0 # int(self.viewport.get_vadjustment().get_value())
# Width and height of the image's clip
width = cairo_context.get_target().get_width()
height = cairo_context.get_target().get_height()
if width > 0 and height > 0:
# Draw created area of the Sample's Pixbuf
Gdk.cairo_set_source_pixbuf(cairo_context, self.pixbuf_view, pixbuf_x, pixbuf_y)
cairo_context.paint()
if self.border_visible == True:
# Draw brush strokes
self.brush._add_point((self.border_x, self.border_y))
self.brush._add_point((self.border_width, self.border_y))
self.brush._add_point((self.border_width, self.border_y+self.border_height))
self.brush._add_point((self.border_x, self.border_y+self.border_height))
self.brush._add_point((self.border_x, self.border_y)) # nie działa ?
self.brush._draw(cairo_context)
end = time.time()
#print(f"Runtime of the program is {end - start}")
def set_ratio_border_size(self):
self.border_x = 0
self.border_y = 0
if self.format_size == "10x15":
if self.horizontal == True and self.vertical == False:
single = self.picture_view_height*0.5
self.border_height = single
self.border_width = single*1.4
#self.look = "horizontal"
#self.border_type = "horizontal"
elif self.horizontal == False and self.vertical == True:
single = self.picture_view_width*0.5
self.border_width = single
self.border_height = single*1.4
#self.look = "vertical"
#self.border_type = "vertical"
elif self.horizontal == False and self.vertical == False:
self.border_height = self.picture_view_height
self.border_width = self.picture_view_width
elif self.format_size == "13x18":
if self.horizontal == True and self.vertical == False:
single = self.picture_view_height*0.5
self.border_height = single
self.border_width = single*1.42
elif self.horizontal == False and self.vertical == True:
single = self.picture_view_width*0.5
self.border_width = single
self.border_height = single*1.42
elif self.horizontal == False and self.vertical == False:
self.border_height = self.picture_view_height
self.border_width = self.picture_view_width
elif self.format_size == "A4":
if self.horizontal == True and self.vertical == False:
single = self.picture_view_height*0.5
self.border_height = single
self.border_width = single*1.4
elif self.horizontal == False and self.vertical == True:
single = self.picture_view_width*0.5
self.border_width = single
self.border_height = single*1.4
elif self.horizontal == False and self.vertical == False:
self.border_height = self.picture_view_height
self.border_width = self.picture_view_width
# ustaw proporcje dla obrazu w kontrolce
def set_ratio_picture_view(self):
self.picture_orig_width = float(self.pixbuf_orig.get_width())
self.picture_orig_height = float(self.pixbuf_orig.get_height())
#if self.format_size == "10x15":
#self.picture_orig_x = 0
#self.picture_orig_y = 0
if self.picture_orig_width > self.picture_orig_height:
self.picture_view_width = 586.66 #600
m = self.picture_orig_width / 586.66 #600
self.picture_view_height = self.picture_orig_height/m
if self.picture_orig_height > self.picture_orig_width:
self.picture_view_height = 586.66 #600
m = self.picture_orig_height / 586.66 #600
self.picture_view_width = self.picture_orig_width/m
# Zapamiętanie rozmiaru obrazka dla kontrolki
self.orig_ratio_width = self.picture_view_width
self.orig_ratio_height = self.picture_view_height
# ustaw proporcje dla obrazu w kontrolce ppo rotacji
def set_ratio_picture_view_after_rotation(self):
self.picture_tmp_width = self.border_width - self.border_x
self.picture_tmp_height = self.border_height - self.border_y
if self.border_x != 0 :
bx = self.picture_orig_width / self.border_x
crop_x = self.picture_tmp_width / bx
else:
crop_x = 0
if self.border_y != 0:
by = self.picture_orig_height / self.border_y
crop_y = self.picture_tmp_height / by
else:
crop_y = 0
self.picture_orig_x = crop_x
self.picture_orig_y = crop_y
"""
W zależności jaka była pierwotnie orientacja zdjęcia, po obrocie
jest ono odpowiedio przycinane
"""
print ("ORIG: "+self.look)
if self.look == "vertical":
if self.border_type == "horizontal":
bw = self.picture_orig_width / (self.border_width - self.border_x)
self.picture_tmp_width = self.picture_orig_width / bw - crop_x
# w tym wypadku 'y' bez odejmowania gdyż skróciło by to fotografie w pione
bh = self.picture_orig_height / self.border_height
self.picture_tmp_height = self.picture_orig_height / bh
elif self.border_type == "vertical":
bw = self.picture_orig_width / (self.border_width - self.border_x)
self.picture_tmp_width = self.picture_orig_width / bw - crop_x
bh = self.picture_orig_height / (self.border_height - self.border_y)
self.picture_tmp_height = self.picture_orig_height / bh
elif self.look == "horizontal":
if self.border_type == "horizontal":
bw = self.picture_orig_width / (self.border_width - self.border_x)
self.picture_tmp_width = self.picture_orig_width / bw - crop_x
bh = self.picture_orig_height / (self.border_height - self.border_y)
self.picture_tmp_height = self.picture_orig_height / bh
elif self.border_type == "vertical":
bw = self.picture_orig_width / (self.border_width - self.border_x)
self.picture_tmp_width = self.picture_orig_width / bw - crop_x
bh = self.picture_orig_height / (self.border_height - self.border_y)
self.picture_tmp_height = self.picture_orig_height / bh
if self.picture_tmp_width > self.picture_tmp_height:
self.picture_view_width = 586.66
m = self.picture_tmp_width / 586.66
self.picture_view_height = self.picture_tmp_height/m
if self.picture_tmp_height > self.picture_tmp_width:
self.picture_view_height = 586.66
m = self.picture_tmp_height / 586.66
self.picture_view_width = self.picture_tmp_width/m
# Zapamiętanie rozmiaru obrazka dla kontrolki
self.orig_ratio_width = self.picture_view_width
self.orig_ratio_height = self.picture_view_height
def on_menu_file_load_img_generic(self, widget):
global grid, file_img_selected
filename=FileChooserIMGLoad()
if len(file_img_selected) != 0:
self.pixbuf_orig = GdkPixbuf.Pixbuf.new_from_file(filename=file_img_selected)
self.set_ratio_picture_view()
self.pixbuf_view = self.pixbuf_orig.scale_simple(self.picture_view_width, self.picture_view_height, GdkPixbuf.InterpType.HYPER)
if self.picture_view_width > self.picture_view_height:
self.horizontal = True
self.vertical = False
self.resize(582.66,413.34)
self.look = "horizontal"
self.border_type = "horizontal"
elif self.picture_view_height > self.picture_view_width:
self.vertical = True
self.horizontal = False
self.resize(413.34,582.66)
self.look = "vertical"
self.border_type = "vertical"
else:
self.horizontal = False
self.vertical = False
self.set_ratio_border_size()
self.border_visible = True
# nie rysujemy na orginale ale na kopii z okna
self.drawing_area.set_size_request(self.pixbuf_view.get_width(), self.pixbuf_view.get_height())
self.drawing_area.set_events(Gdk.EventMask.ALL_EVENTS_MASK)
self.show_all()
def add_edit_menu_actions(self, action_group):
action_info_menu = Gtk.Action(name="EditMenu", label="Edit")
action_group.add_action(action_info_menu)
action_new = Gtk.Action(
name="Rotate",
label="Frame rotation",
tooltip="Rotate border",
)
action_new.connect("activate", self.on_menu_rotate)
action_group.add_action_with_accel(action_new, None)
def add_info_menu_actions(self, action_group):
action_info_menu = Gtk.Action(name="InfoMenu", label="Info")
action_group.add_action(action_info_menu)
action_new = Gtk.Action(
name="Help",
label="Help",
tooltip="Help",
)
action_new.connect("activate", self.on_menu_help)
action_group.add_action_with_accel(action_new, None)
action_new = Gtk.Action(
name="About",
label="About",
tooltip="About",
)
action_new.connect("activate", self.on_menu_about)
action_group.add_action_with_accel(action_new, None)
action_new = Gtk.Action(
name="License",
label="License",
tooltip="License",
)
action_new.connect("activate", self.on_menu_license)
action_group.add_action_with_accel(action_new, None)
def add_file_menu_actions(self, action_group):
action_filemenu = Gtk.Action(name="FileMenu", label="File")
action_group.add_action(action_filemenu)
"""
# sposób dodawania podmenu
action_fileloadmenu = Gtk.Action(name="FileLoad", stock_id=Gtk.STOCK_OPEN)
action_group.add_action(action_fileloadmenu)
action_new = Gtk.Action(
name="LoadImg",
label="Wczytaj Obrazek",
tooltip="Wczytuje obrazek",
)
action_new.connect("activate", self.on_menu_file_load_img_generic)
action_group.add_action_with_accel(action_new, None)
"""
action_fileload = Gtk.Action(name="FileLoad", stock_id=Gtk.STOCK_OPEN)
action_fileload.connect("activate", self.on_menu_file_load_img_generic)
action_group.add_action(action_fileload)
action_filesave = Gtk.Action(name="FileSave", stock_id=Gtk.STOCK_SAVE)
action_filesave.connect("activate", self.on_menu_file_save_img)
action_group.add_action(action_filesave)
action_print = Gtk.Action(name="FilePrint", stock_id=Gtk.STOCK_PRINT)
action_print.connect("activate", self.print_image)
action_group.add_action(action_print)
action_filequit = Gtk.Action(name="FileQuit", stock_id=Gtk.STOCK_QUIT)
action_filequit.connect("activate", self.on_menu_file_quit)
action_group.add_action(action_filequit)
# tworzy menu bar
def create_ui_manager(self):
uimanager = Gtk.UIManager()
# Throws exception if something went wrong
uimanager.add_ui_from_string(UI_INFO)
# Add the accelerator group to the toplevel window
accelgroup = uimanager.get_accel_group()
self.add_accel_group(accelgroup)
return uimanager
def on_menu_about(self, widget):
dialog = DialogAbout(self)
response = dialog.run()
dialog.destroy()
def on_menu_license(self, widget):
dialog = DialogLicense(self)
response = dialog.run()
dialog.destroy()
def on_menu_help(self, widget):
dialog = DialogHelp(self)
response = dialog.run()
dialog.destroy()
def on_menu_rotate(self, widget):
global rotate
if self.border_type == "horizontal":
self.border_type = "vertical"
self.horizontal = False
self.vertical = True
single = self.picture_view_width*0.3
self.border_width = single
self.border_height = single*1.4
self.border_x = 0
self.border_y = 0
rotate = True
elif self.border_type == "vertical":
self.border_type = "horizontal"
self.horizontal = True
self.vertical = False
single = self.picture_view_height*0.3
self.border_height = single
self.border_width = single*1.4
self.border_x = 0
self.border_y = 0
rotate = True
else:
pass
print ("ASPECT: "+self.border_type)
print ("BORDER-WIDTH :"+str(self.border_width))
print ("BORDER-HEIGHT:"+str(self.border_height))
self.drawing_area.queue_draw()
def on_menu_file_save_img(self, widget):
global file_img_selected
if self.pixbuf_tmp != None:
filename=FileChooserIMGSave()
if len(file_img_selected) != 0:
self.pixbuf_view.savev(file_img_selected, "jpeg", ["quality"], ["100"])
else:
dialog = DialogCropWarning(self)
response = dialog.run()
dialog.destroy()
def on_menu_file_quit(self, widget):
Gtk.main_quit()
def print_page(self, operation=None, context=None, page_nr=None):
ctx = context.get_cairo_context()
# make cairo ImageSurface from the png file
surface = cairo.ImageSurface.create_from_png('/tmp/photocrop.png')
#ctx.rectangle(50,50,100,100)
ctx.set_source_surface(surface)
ctx.paint ()
os.remove("/tmp/photocrop.png");
def print_image(self, widget):
if self.pixbuf_tmp == None:
self.pixbuf_tmp = self.pixbuf_orig
# źle obraca
#if self.border_type == "horizontal":
# self.pixbuf_tmp.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE)
# #pixbuf2.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE)
# self.border_type = "vertical"
FACTOR_MM_TO_PIXEL = 2.834645669
if self.format_size == "10x15":
#if self.horizontal == True and self.vertical == False:
if self.border_type == "horizontal":
page_width = 148
page_height = 104.99
img_height =104.99 * FACTOR_MM_TO_PIXEL
img_width = 148 * FACTOR_MM_TO_PIXEL
#elif self.horizontal == False and self.vertical == True:
elif self.border_type == "vertical":
page_width = 104.99
page_height = 148
img_width =104.99 * FACTOR_MM_TO_PIXEL
img_height = 148 * FACTOR_MM_TO_PIXEL
size = "10x15"
elif self.format_size == "13x18":
#if self.horizontal == True and self.vertical == False:
if self.border_type == "horizontal":
page_width = 178
page_height = 127
img_height = 127 * FACTOR_MM_TO_PIXEL
img_width = 178 * FACTOR_MM_TO_PIXEL
#elif self.horizontal == False and self.vertical == True:
elif self.border_type == "vertical":
page_width = 127
page_height = 178
img_width = 127 * FACTOR_MM_TO_PIXEL
img_height = 178 * FACTOR_MM_TO_PIXEL
size = "5x7"
elif self.format_size == "A4":
#if self.horizontal == True and self.vertical == False:
if self.border_type == "horizontal":
page_width = 297
page_height = 207
img_height = 207 * FACTOR_MM_TO_PIXEL
img_width = 297 * FACTOR_MM_TO_PIXEL
#elif self.horizontal == False and self.vertical == True:
elif self.border_type == "vertical":
page_width = 207
page_height = 297
img_width = 207 * FACTOR_MM_TO_PIXEL
img_height = 297 * FACTOR_MM_TO_PIXEL
size = "A4"
dpi = 600
# z orginalnymi wielkościami nie chce drukowac
pixbuf2 = self.pixbuf_tmp.scale_simple(img_width, img_height, GdkPixbuf.InterpType.HYPER)
pixbuf2.savev("/tmp/photocrop.png","png", ["quailty"], ["100"])
#ps = Gtk.PaperSize.new_custom(size, size, self.pixbuf_tmp.get_width(), self.pixbuf_tmp.get_height(), Gtk.Unit.POINTS)
ps = Gtk.PaperSize.new_custom(size, size, page_width, page_height, Gtk.Unit.MM)
print_settings = Gtk.PrintSettings()
print_settings.set_resolution(dpi)
page_setup = Gtk.PageSetup()
page_setup.set_paper_size(ps)
page_setup.set_bottom_margin(0.0, Gtk.Unit.MM)
page_setup.set_left_margin(0.0, Gtk.Unit.MM)
page_setup.set_right_margin(0.0, Gtk.Unit.MM)
page_setup.set_top_margin(0.0, Gtk.Unit.MM)
#if self.border_type == "horizontal":
# page_setup.set_orientation(Gtk.PageOrientation.LANDSCAPE)
#elif self.border_type == "vertical":
# page_setup.set_orientation(Gtk.PageOrientation.PORTRAIT)
print_operation = Gtk.PrintOperation()
print_operation.set_unit(Gtk.Unit.POINTS)
print_operation.set_n_pages(1)
print_operation.set_default_page_setup(page_setup)
print_operation.set_print_settings(print_settings)
print_operation.connect("draw_page", self.print_page)
#print_operation.set_export_filename("example.pdf")
result = print_operation.run(Gtk.PrintOperationAction.PRINT_DIALOG, None) # window zamisat None
#result = print_operation.run(Gtk.PrintOperationAction.PREVIEW, None)
print(result)
# przycinamy !
def photo_crop(self, button):
if self.border_x < 0:
dialog = DialogWarning(self)
response = dialog.run()
dialog.destroy()
return
if self.border_y < 0:
dialog = DialogWarning(self)
response = dialog.run()
dialog.destroy()
return
if self.border_width > self.picture_view_width:
dialog = DialogWarning(self)
response = dialog.run()
dialog.destroy()
return
# FIXME - w sumie to jest zastanawiające !?
if self.border_height + self.border_y > self.picture_view_height:
dialog = DialogWarning(self)
response = dialog.run()
dialog.destroy()
return
if self.border_x != 0 :
bx = self.picture_view_width / self.border_x
crop_x = self.picture_orig_width / bx
else:
crop_x = 0
if self.border_y != 0:
by = self.picture_view_height / self.border_y
crop_y = self.picture_orig_height / by
else:
crop_y = 0
bw = self.picture_view_width / self.border_width
crop_width = self.picture_orig_width / bw - crop_x
bh = self.picture_view_height / self.border_height
crop_height = self.picture_orig_height / bh
# False - kanał Alpha
self.pixbuf_tmp = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, False, 8, crop_width, crop_height)
# zera na koncu to dest_x i dest_y
self.pixbuf_orig.copy_area(crop_x, crop_y, crop_width, crop_height, self.pixbuf_tmp, 0, 0)
if rotate == False:
self.picture_tmp_width = float(self.pixbuf_tmp.get_width())
self.picture_tmp_height = float(self.pixbuf_tmp.get_height())
# tym razem przeliczamy z uwzględnieniem bufora tymczasowego
if self.picture_tmp_width > self.picture_tmp_height:
self.picture_view_width = 586.66
m = self.picture_tmp_width / 586.66
self.picture_view_height = self.picture_tmp_height/m
if self.picture_tmp_height > self.picture_tmp_width:
self.picture_view_height = 586.66
m = self.picture_tmp_height / 586.66
self.picture_view_width = self.picture_tmp_width/m
else:
self.set_ratio_picture_view_after_rotation()
self.drawing_area.set_size_request(self.picture_view_width, self.picture_view_height)
self.drawing_area.set_events(Gdk.EventMask.ALL_EVENTS_MASK)
self.pixbuf_view = self.pixbuf_tmp.scale_simple(self.picture_view_width, self.picture_view_height, GdkPixbuf.InterpType.HYPER)
self.picture_view_x = 0
self.picture_view_y = 0
self.border_visible = False
self.drawing_area.queue_draw()
# przywracamy
def photo_restore(self, button):
self.border_visible = True
self.set_ratio_picture_view()
self.pixbuf_view = self.pixbuf_orig.scale_simple(self.orig_ratio_width, self.orig_ratio_height, GdkPixbuf.InterpType.BILINEAR)
self.drawing_area.set_size_request(self.orig_ratio_width, self.orig_ratio_height)
self.drawing_area.set_events(Gdk.EventMask.ALL_EVENTS_MASK)
self.pixbuf_tmp = None # uniemożliwiamy zapis
self.show_all()
self.drawing_area.queue_draw()
def on_format_combo_changed(self, combo):
tree_iter = combo.get_active_iter()
if tree_iter is not None:
model = combo.get_model()
self.format_size = model[tree_iter][0]
print("Selected: format=%s" % self.format_size)
self.set_ratio_border_size()
self.drawing_area.queue_draw()
def on_scroll(self, widget, event):
""" handles on scroll event"""
# Handles zoom in / zoom out on Ctrl+mouse wheel
accel_mask = Gtk.accelerator_get_default_mod_mask()
if event.state & accel_mask == Gdk.ModifierType.CONTROL_MASK:
direction = event.get_scroll_deltas()[2]
if direction > 0:
scrolling = "zoom_out"
else:
scrolling = "zoom_in"
self.border_height += self.border_properties[self.format_size][self.border_type][scrolling]["height"]
self.border_width += self.border_properties[self.format_size][self.border_type][scrolling]["width"]
"""
# tu jest jakiś błąd
if self.border_width > self.picture_view_width:
self.border_width -= 1
if self.border_height+self.border_y > self.picture_view_height:
self.border_height -= 1
"""
self.drawing_area.queue_draw()
def unclick_in_drawing_area (self, box, event):
self.button_press = False
print ("Przycisk myszki puszczony")
def onclick_in_drawing_area (self, box, event):
if event.button == 1:
self.button_press = True
print ("Lewy przyciski myszki naciśnięty")
def on_mouse_move_in_drawing_area(self, box, event):
if self.button_press == True:
if self.border_type == "vertical":
#print ("VERTICAL %%")
if self.last_x < event.x:
self.border_x += 1
self.border_width += 1
if event.x < self.last_x:
self.border_x -= 1
self.border_width -= 1
if self.last_y < event.y:
self.border_y += 1
self.border_height += 0.0
if event.y < self.last_y:
self.border_y -= 1
self.border_height -= 0.0
elif self.border_type == "horizontal":
#print ("Horizontal %%")
if self.last_x < event.x:
self.border_x += 1
self.border_width += 1
if event.x < self.last_x:
self.border_x -= 1.
self.border_width -= 1
if self.last_y < event.y:
self.border_y += 1
self.border_height += 0.0
if event.y < self.last_y:
self.border_y -= 1
self.border_height -= 0.0
self.last_y = event.y
self.last_x = event.x
"""
# jeśli będzie się napierać na skraj krawędzi wówczas powiększa ramkę
if self.border_x < 0:
self.border_x += 1
self.border_width += 1
if self.border_y < 0:
self.border_y += 1
self.border_height +=1
# tu jest jakiś błąd
# powoduje błędne zachowanie ramki
if self.border_type == "vertical":
if self.border_width > self.picture_view_width:
self.border_width -= 1
self.border_x -= 1
if self.border_height+self.border_y > self.picture_view_height:
self.border_height -= 1
self.border_y -= 1
elif self.border_type == "horizontal":
if self.border_height+self.border_y> self.picture_view_width:
self.border_width -= 1
self.border_x -= 1
if self.border_width > self.picture_view_height:
self.border_height -= 1
self.border_y -= 1
"""
self.drawing_area.queue_draw()
def init_ui(self):
# JPG akceptuje jedynie z GIMPa
self.border_properties = { "10x15" : { "horizontal" : { "zoom_out" : {"width" : -1.48, "height" : -0.92}, "zoom_in" : {"width" : 1.48, "height" : 0.92}},
"vertical" : { "zoom_out": { "width" : -0.92, "height" : -1.48}, "zoom_in": { "width" : 0.92, "height" : 1.48}},
"square" : { "zoom_out" : { "width" : -1, "height" : -1}, "zoom_out" : { "width" : 1, "height" : 1}}},
"13x18" : { "horizontal" : { "zoom_out" : {"width" : -1.82, "height" : -1.30}, "zoom_in" : {"width" : 1.82, "height" : 1.30}},
"vertical" : { "zoom_out": { "width" : -1.30, "height" : -1.78}, "zoom_in": { "width" : 1.30, "height" : 1.78}},
"square" : { "zoom_out" : { "width" : -1, "height" : -1}, "zoom_out" : { "width" : 1, "height" : 1}}},
"A4" : { "horizontal" : { "zoom_out" : {"width" : -2.97, "height" : -1.84}, "zoom_in" : {"width" : 2.97, "height" : 1.84}},
"vertical" : { "zoom_out": { "width" : -1.84, "height" : -2.97}, "zoom_in": { "width" : 1.84, "height" : 2.97}},
"square" : { "zoom_out" : { "width" : -1, "height" : -1}, "zoom_out" : { "width" : 1, "height" : 1}}}}
#self.props.border_width = 20
self.add(grid)
self.pixbuf_tmp = None
grid.set_row_spacing(10)
grid.set_column_spacing(10)
grid.set_column_homogeneous(True) # rozszerza kontrolki na resztę okna
action_group = Gtk.ActionGroup(name="my_actions")
self.add_file_menu_actions(action_group)
self.add_edit_menu_actions(action_group)
self.add_info_menu_actions(action_group)
uimanager = self.create_ui_manager()
uimanager.insert_action_group(action_group)
menubar = uimanager.get_widget("/MenuBar")
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
box.pack_start(menubar, False, False, 0)
grid.attach(box, 0, 0, 3, 1)
file_img_selected=get_resource_path("/usr/share/photocrop/IMG_6854.JPG") #img/blank.png
self.pixbuf_orig = GdkPixbuf.Pixbuf.new_from_file(filename=file_img_selected)
self.format_size = "10x15"
self.set_ratio_picture_view()
self.pixbuf_view = self.pixbuf_orig.scale_simple(self.picture_view_width, self.picture_view_height, GdkPixbuf.InterpType.HYPER)
self.border_visible = True
if self.picture_view_width > self.picture_view_height:
self.horizontal = True
self.vertical = False
self.look = "horizontal"
self.border_type = "horizontal"
elif self.picture_view_height > self.picture_view_width:
self.vertical = True
self.horizontal = False
self.look = "vertical"
self.border_type = "vertical"
else:
self.horizontal = False
self.vertical = False
self.look = "square"
self.drawing_area = Gtk.DrawingArea()
self.drawing_area.set_size_request(self.pixbuf_view.get_width(), self.pixbuf_view.get_height())
self.drawing_area.set_events(Gdk.EventMask.ALL_EVENTS_MASK)
self.drawing_area.connect("draw", self.on_drawing_area_draw)
frame = Gtk.Frame()
event_box = Gtk.EventBox ()
self.last_x = 1
self.last_y = 1
self.border_x = 0
self.border_y = 0
self.button_press = False
self.pixbuf_tmp = None
event_box.connect ('button-press-event', self.onclick_in_drawing_area)
event_box.connect ('button-release-event', self.unclick_in_drawing_area)
event_box.connect("motion-notify-event", self.on_mouse_move_in_drawing_area)
event_box.add_events(Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.BUTTON_PRESS_MASK)
event_box.add(self.drawing_area)
frame.add(event_box)
grid.attach(frame,0,1,3,1)
button1 = Gtk.Button.new_with_label("Crop")
button1.connect("clicked", self.photo_crop)
grid.attach(button1,0,4,1,1)
button2 = Gtk.Button.new_with_label("Restore")
button2.connect("clicked", self.photo_restore)
grid.attach(button2,1,4,1,1)
format_store = Gtk.ListStore(str)
format_photo = [
"10x15",
"13x18",
"A4",
]
for fp in format_photo:
format_store.append([fp])
format_combo = Gtk.ComboBox.new_with_model(format_store)
format_combo.connect("changed", self.on_format_combo_changed)
renderer_text = Gtk.CellRendererText()
format_combo.pack_start(renderer_text, True)
format_combo.add_attribute(renderer_text, "text", 0)
format_combo.set_active(0)
grid.attach(format_combo,2,4,1,1)
self.drawing_area.connect('scroll-event', self.on_scroll)
self.set_border_width(10)
self.set_title("Photo Crop (alpha)")
#self.set_default_size(700, 600)
#self.resize(700, 600)
self.set_resizable(False)
self.connect("destroy", Gtk.main_quit)
win = MyWindow()
win.show_all()
Gtk.main()
|