質問するログイン新規登録

質問編集履歴

2

追記

2019/07/17 09:45

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -7,7 +7,7 @@
7
7
 
8
8
  ### 該当のソースコード
9
9
 
10
- ```python
10
+ ```clip
11
11
  import numpy as np
12
12
  import cv2
13
13
 
@@ -39,12 +39,195 @@
39
39
  return clip
40
40
 
41
41
  #関数定義
42
+ ```
43
+ ```main
44
+ # -*- coding: utf-8 -*-
42
45
 
46
+ from random import random
47
+ from kivy.app import App
48
+ from kivy.config import Config
49
+ import clip
50
+ import cv2
51
+ import os
52
+
53
+ # 起動時の解像度の設定
54
+ Config.set('graphics', 'width', '1024')
55
+ Config.set('graphics', 'height', '768') # 16:9
56
+ Config.set('graphics', 'resizable', False) # ウインドウリサイズ禁止
57
+
58
+ from kivy.uix.widget import Widget
59
+ from kivy.uix.button import Button
60
+ from kivy.graphics import Color, Ellipse, Line
61
+ from kivy.properties import ObjectProperty
62
+ from kivy.uix.behaviors import ToggleButtonBehavior
63
+ from kivy.uix.togglebutton import ToggleButton
64
+ from kivy.utils import get_color_from_hex # 色の16進数表示を可能にする
65
+ from kivy.core.window import Window
66
+
67
+ class MyPaintWidget(Widget):
68
+ #pass
69
+ last_color = '' # 画面クリアを押された場合の最後の色
70
+ line_width = 3 # 線の太さ
71
+
72
+ def on_touch_down(self, touch):
73
+ if Widget.on_touch_down(self, touch):
74
+ return
75
+
76
+
77
+ color = (random(), 1, 1)
78
+ with self.canvas:
79
+ touch.ud['line'] = Line(points=(touch.x, touch.y), width=self.line_width)
80
+
81
+ def set_line_width(self, line_width=3):
82
+ self.line_width = line_width
83
+
84
+ def on_touch_move(self, touch):
85
+ if touch.ud: # スライダーを動かす際のエラーを解除するため
86
+ touch.ud['line'].points += [touch.x, touch.y]
87
+
88
+ def set_color(self, new_color):
89
+ self.last_color = new_color
90
+ self.canvas.add(Color(*new_color))
91
+
92
+ class MyCanvasWidget(Widget):
93
+
94
+ def clear_canvas(self):
95
+ MyPaintWidget.clear_canvas(self)
96
+
97
+
98
+ class MyPaintApp(App):
99
+
100
+ def __init__(self, **kwargs):
101
+ super(MyPaintApp, self).__init__(**kwargs)
102
+ self.title = '画像表示'
103
+
104
+ def build(self):
105
+ parent = Widget()
106
+ self.painter = MyCanvasWidget()
107
+
108
+ # 起動時の色の設定を行う
109
+ self.painter.ids['paint_area'].set_color(get_color_from_hex('#000000')) #黒色を設定
110
+ return self.painter
111
+
112
+ def clear_canvas(self):
113
+ self.painter.ids['paint_area'].canvas.clear()
114
+ self.painter.ids['paint_area'].set_color(self.painter.ids['paint_area'].last_color)
115
+
116
+ def save_canvas(self):
117
+ filename_base = 'sample.png'
118
+ img_path = Window.screenshot("base\" + filename_base) # スクリーンショットを保存する
119
+ basename = os.path.basename(img_path)
120
+ img = cv2.imread("base\" + basename)
121
+ cv2.rectangle(img, (0, 0), (78, 39), (255, 255, 255), thickness=-1)
122
+ img = img[0:637, 0:1024]
43
- mask_and_alpha(base_img, mask_img, basename)
123
+ clip.mask_and_alpha(img, clip.maskgenerate(img, basename), basename)
124
+
125
+ class ColorButton(ToggleButton):
126
+
127
+ def _do_press(self):
128
+ if self.state == 'normal':
129
+ ToggleButtonBehavior._do_press(self)# ボタンを押されてない場合は状態を変更する
130
+
131
+
132
+ if __name__ == '__main__':
133
+ Window.clearcolor = get_color_from_hex('#ffffff') # ウィンドウの色を白色に変更する
134
+ MyPaintApp().run()
135
+
44
136
  ```
137
+ ```mypaint
45
- 変数imgには読み込んだ画像がbasenameにはファイル名が入ります。
138
+ #:import hex_color kivy.utils.get_color_from_hex
46
139
 
140
+ <ColorButton>:
141
+ background_normal: 'color_button_normal.png'
142
+ background_down: 'color_button_down.png'
143
+ group: 'color'
144
+ border: (5, 5, 5, 5)
145
+ on_release: app.painter.ids['paint_area'].set_color(self.background_color)
146
+ #on_release: app.painter.paint_id.set_color(self.background_color) # こ�?�方法でもset_corlorにアクセス可能
147
+ <MyCanvasWidget>:
148
+ paint_id:paint_area
149
+ id: canvas_area
47
150
 
151
+ test:button1
152
+ Button:
153
+ text: 'save'
154
+ color: 1, 1, 1 , 1
155
+ font_size: 20
156
+ on_release: app.save_canvas()
157
+ border: (2, 2, 2, 2)
158
+ x: 0
159
+ top: root.top
160
+ width: 80
161
+ height: 40
162
+
163
+ BoxLayout:
164
+ orientation: 'vertical'
165
+ height: root.height
166
+
167
+ width: root.width
168
+ MyPaintWidget:
169
+ id: paint_area
170
+ size_hint_y: 0.8
171
+
172
+ BoxLayout:
173
+ orientation: 'horizontal'
174
+ size_hint_y: 0.1
175
+ Label:
176
+ size_hint_x: 0.1
177
+ text: 'Line width %s' % int(s1.value) if s1.value else 'Line width not set'
178
+ color: 0,0,0,1
179
+ Slider:
180
+ id: s1
181
+ size_hint_x: 0.9
182
+ value: 3
183
+ range: (1,100)
184
+ step: 1
185
+ on_touch_down:app.painter.ids['paint_area'].set_line_width(self.value)
186
+
187
+ BoxLayout:
188
+ orientation: 'horizontal'
189
+ size_hint_y: 0.1
190
+ clear_btn:button1
191
+ Button:
192
+ id: button1
193
+ text: "Clear"
194
+ ont_size: 30
195
+ on_release: app.clear_canvas()
196
+
197
+
198
+ ColorButton:
199
+ text: "white "
200
+ color: 0, 0, 0 , 1
201
+ background_color: hex_color('#ffffff')
202
+
203
+ ColorButton:
204
+ text: "black "
205
+ state: 'down'
206
+ background_color: hex_color('#000000')
207
+
208
+ ColorButton:
209
+ text: "red "
210
+ background_color: hex_color('#ff0000')
211
+
212
+ ColorButton:
213
+ text: "biue "
214
+ background_color: hex_color('#0000ff')
215
+
216
+ ColorButton:
217
+ text: "green "
218
+ background_color: hex_color('#008000')
219
+
220
+ ColorButton:
221
+ text: "orange"
222
+ background_color: hex_color('#ff4500')
223
+
224
+ ColorButton:
225
+ text: "purple"
226
+ background_color: hex_color('#800080')
227
+ ```
228
+ clip,mainにはそれぞれ拡張子として.py、mypaintには.kvが付きます![イメージ説明](027834afadb964c124192c606415d7dc.png)
229
+ ↑失敗例の切り抜き画像
230
+
48
231
  ### 補足情報(FW/ツールのバージョンなど)
49
232
  python 3.6.8
50
233
  opencv 3.4.2

1

プログラムの追記

2019/07/17 09:45

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -37,8 +37,10 @@
37
37
  clip = cv2.merge(bgr_img + [mask_img])
38
38
  cv2.imwrite("clip\" + basename,clip)
39
39
  return clip
40
+
40
41
  #関数定義
41
42
 
43
+ mask_and_alpha(base_img, mask_img, basename)
42
44
  ```
43
45
  変数imgには読み込んだ画像がbasenameにはファイル名が入ります。
44
46