質問編集履歴
2
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
|
19
|
-
```p
|
19
|
+
```clip
|
20
20
|
|
21
21
|
import numpy as np
|
22
22
|
|
@@ -80,15 +80,381 @@
|
|
80
80
|
|
81
81
|
#関数定義
|
82
82
|
|
83
|
-
|
84
|
-
|
85
|
-
mask_and_alpha(base_img, mask_img, basename)
|
86
|
-
|
87
83
|
```
|
88
84
|
|
85
|
+
```main
|
86
|
+
|
87
|
+
# -*- coding: utf-8 -*-
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
from random import random
|
92
|
+
|
93
|
+
from kivy.app import App
|
94
|
+
|
95
|
+
from kivy.config import Config
|
96
|
+
|
97
|
+
import clip
|
98
|
+
|
99
|
+
import cv2
|
100
|
+
|
101
|
+
import os
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
# 起動時の解像度の設定
|
106
|
+
|
107
|
+
Config.set('graphics', 'width', '1024')
|
108
|
+
|
109
|
+
Config.set('graphics', 'height', '768') # 16:9
|
110
|
+
|
111
|
+
Config.set('graphics', 'resizable', False) # ウインドウリサイズ禁止
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
from kivy.uix.widget import Widget
|
116
|
+
|
117
|
+
from kivy.uix.button import Button
|
118
|
+
|
119
|
+
from kivy.graphics import Color, Ellipse, Line
|
120
|
+
|
121
|
+
from kivy.properties import ObjectProperty
|
122
|
+
|
123
|
+
from kivy.uix.behaviors import ToggleButtonBehavior
|
124
|
+
|
125
|
+
from kivy.uix.togglebutton import ToggleButton
|
126
|
+
|
127
|
+
from kivy.utils import get_color_from_hex # 色の16進数表示を可能にする
|
128
|
+
|
129
|
+
from kivy.core.window import Window
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
class MyPaintWidget(Widget):
|
134
|
+
|
135
|
+
#pass
|
136
|
+
|
137
|
+
last_color = '' # 画面クリアを押された場合の最後の色
|
138
|
+
|
139
|
+
line_width = 3 # 線の太さ
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
def on_touch_down(self, touch):
|
144
|
+
|
145
|
+
if Widget.on_touch_down(self, touch):
|
146
|
+
|
147
|
+
return
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
color = (random(), 1, 1)
|
154
|
+
|
155
|
+
with self.canvas:
|
156
|
+
|
157
|
+
touch.ud['line'] = Line(points=(touch.x, touch.y), width=self.line_width)
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
def set_line_width(self, line_width=3):
|
162
|
+
|
163
|
+
self.line_width = line_width
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
def on_touch_move(self, touch):
|
168
|
+
|
169
|
+
if touch.ud: # スライダーを動かす際のエラーを解除するため
|
170
|
+
|
171
|
+
touch.ud['line'].points += [touch.x, touch.y]
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
def set_color(self, new_color):
|
176
|
+
|
177
|
+
self.last_color = new_color
|
178
|
+
|
179
|
+
self.canvas.add(Color(*new_color))
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
class MyCanvasWidget(Widget):
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
def clear_canvas(self):
|
188
|
+
|
189
|
+
MyPaintWidget.clear_canvas(self)
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
class MyPaintApp(App):
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
def __init__(self, **kwargs):
|
200
|
+
|
201
|
+
super(MyPaintApp, self).__init__(**kwargs)
|
202
|
+
|
203
|
+
self.title = '画像表示'
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
def build(self):
|
208
|
+
|
209
|
+
parent = Widget()
|
210
|
+
|
211
|
+
self.painter = MyCanvasWidget()
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
# 起動時の色の設定を行う
|
216
|
+
|
217
|
+
self.painter.ids['paint_area'].set_color(get_color_from_hex('#000000')) #黒色を設定
|
218
|
+
|
219
|
+
return self.painter
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
def clear_canvas(self):
|
224
|
+
|
225
|
+
self.painter.ids['paint_area'].canvas.clear()
|
226
|
+
|
227
|
+
self.painter.ids['paint_area'].set_color(self.painter.ids['paint_area'].last_color)
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
def save_canvas(self):
|
232
|
+
|
233
|
+
filename_base = 'sample.png'
|
234
|
+
|
235
|
+
img_path = Window.screenshot("base\" + filename_base) # スクリーンショットを保存する
|
236
|
+
|
89
|
-
|
237
|
+
basename = os.path.basename(img_path)
|
238
|
+
|
90
|
-
|
239
|
+
img = cv2.imread("base\" + basename)
|
240
|
+
|
91
|
-
|
241
|
+
cv2.rectangle(img, (0, 0), (78, 39), (255, 255, 255), thickness=-1)
|
242
|
+
|
243
|
+
img = img[0:637, 0:1024]
|
244
|
+
|
245
|
+
clip.mask_and_alpha(img, clip.maskgenerate(img, basename), basename)
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
class ColorButton(ToggleButton):
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
def _do_press(self):
|
254
|
+
|
255
|
+
if self.state == 'normal':
|
256
|
+
|
257
|
+
ToggleButtonBehavior._do_press(self)# ボタンを押されてない場合は状態を変更する
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
if __name__ == '__main__':
|
264
|
+
|
265
|
+
Window.clearcolor = get_color_from_hex('#ffffff') # ウィンドウの色を白色に変更する
|
266
|
+
|
267
|
+
MyPaintApp().run()
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
```
|
272
|
+
|
273
|
+
```mypaint
|
274
|
+
|
275
|
+
#:import hex_color kivy.utils.get_color_from_hex
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
<ColorButton>:
|
280
|
+
|
281
|
+
background_normal: 'color_button_normal.png'
|
282
|
+
|
283
|
+
background_down: 'color_button_down.png'
|
284
|
+
|
285
|
+
group: 'color'
|
286
|
+
|
287
|
+
border: (5, 5, 5, 5)
|
288
|
+
|
289
|
+
on_release: app.painter.ids['paint_area'].set_color(self.background_color)
|
290
|
+
|
291
|
+
#on_release: app.painter.paint_id.set_color(self.background_color) # こ�?�方法でもset_corlorにアクセス可能
|
292
|
+
|
293
|
+
<MyCanvasWidget>:
|
294
|
+
|
295
|
+
paint_id:paint_area
|
296
|
+
|
297
|
+
id: canvas_area
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
test:button1
|
302
|
+
|
303
|
+
Button:
|
304
|
+
|
305
|
+
text: 'save'
|
306
|
+
|
307
|
+
color: 1, 1, 1 , 1
|
308
|
+
|
309
|
+
font_size: 20
|
310
|
+
|
311
|
+
on_release: app.save_canvas()
|
312
|
+
|
313
|
+
border: (2, 2, 2, 2)
|
314
|
+
|
315
|
+
x: 0
|
316
|
+
|
317
|
+
top: root.top
|
318
|
+
|
319
|
+
width: 80
|
320
|
+
|
321
|
+
height: 40
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
BoxLayout:
|
326
|
+
|
327
|
+
orientation: 'vertical'
|
328
|
+
|
329
|
+
height: root.height
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
width: root.width
|
334
|
+
|
335
|
+
MyPaintWidget:
|
336
|
+
|
337
|
+
id: paint_area
|
338
|
+
|
339
|
+
size_hint_y: 0.8
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
BoxLayout:
|
344
|
+
|
345
|
+
orientation: 'horizontal'
|
346
|
+
|
347
|
+
size_hint_y: 0.1
|
348
|
+
|
349
|
+
Label:
|
350
|
+
|
351
|
+
size_hint_x: 0.1
|
352
|
+
|
353
|
+
text: 'Line width %s' % int(s1.value) if s1.value else 'Line width not set'
|
354
|
+
|
355
|
+
color: 0,0,0,1
|
356
|
+
|
357
|
+
Slider:
|
358
|
+
|
359
|
+
id: s1
|
360
|
+
|
361
|
+
size_hint_x: 0.9
|
362
|
+
|
363
|
+
value: 3
|
364
|
+
|
365
|
+
range: (1,100)
|
366
|
+
|
367
|
+
step: 1
|
368
|
+
|
369
|
+
on_touch_down:app.painter.ids['paint_area'].set_line_width(self.value)
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
BoxLayout:
|
374
|
+
|
375
|
+
orientation: 'horizontal'
|
376
|
+
|
377
|
+
size_hint_y: 0.1
|
378
|
+
|
379
|
+
clear_btn:button1
|
380
|
+
|
381
|
+
Button:
|
382
|
+
|
383
|
+
id: button1
|
384
|
+
|
385
|
+
text: "Clear"
|
386
|
+
|
387
|
+
ont_size: 30
|
388
|
+
|
389
|
+
on_release: app.clear_canvas()
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
ColorButton:
|
396
|
+
|
397
|
+
text: "white "
|
398
|
+
|
399
|
+
color: 0, 0, 0 , 1
|
400
|
+
|
401
|
+
background_color: hex_color('#ffffff')
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
ColorButton:
|
406
|
+
|
407
|
+
text: "black "
|
408
|
+
|
409
|
+
state: 'down'
|
410
|
+
|
411
|
+
background_color: hex_color('#000000')
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
ColorButton:
|
416
|
+
|
417
|
+
text: "red "
|
418
|
+
|
419
|
+
background_color: hex_color('#ff0000')
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
ColorButton:
|
424
|
+
|
425
|
+
text: "biue "
|
426
|
+
|
427
|
+
background_color: hex_color('#0000ff')
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
ColorButton:
|
432
|
+
|
433
|
+
text: "green "
|
434
|
+
|
435
|
+
background_color: hex_color('#008000')
|
436
|
+
|
437
|
+
|
438
|
+
|
439
|
+
ColorButton:
|
440
|
+
|
441
|
+
text: "orange"
|
442
|
+
|
443
|
+
background_color: hex_color('#ff4500')
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
ColorButton:
|
448
|
+
|
449
|
+
text: "purple"
|
450
|
+
|
451
|
+
background_color: hex_color('#800080')
|
452
|
+
|
453
|
+
```
|
454
|
+
|
455
|
+
clip,mainにはそれぞれ拡張子として.py、mypaintには.kvが付きます![イメージ説明](027834afadb964c124192c606415d7dc.png)
|
456
|
+
|
457
|
+
↑失敗例の切り抜き画像
|
92
458
|
|
93
459
|
|
94
460
|
|
1
プログラムの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -76,9 +76,13 @@
|
|
76
76
|
|
77
77
|
return clip
|
78
78
|
|
79
|
+
|
80
|
+
|
79
81
|
#関数定義
|
80
82
|
|
81
83
|
|
84
|
+
|
85
|
+
mask_and_alpha(base_img, mask_img, basename)
|
82
86
|
|
83
87
|
```
|
84
88
|
|