forked from SebastianJarsve/Pythonista-Pixel-Editor
/
pixel_editor.py
212 lines (182 loc) · 6.05 KB
/
pixel_editor.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
import ui, photos, console, numpy, Image
from scene import Rect, Point
class Pixel (Rect):
def __init__(self, x, y, w, h):
Rect.__init__(self, x, y, w, h)
self.reset()
def reset(self):
self.color = tuple((0, 0, 0, 0))
self.used = False
class PixelEditor (ui.View):
def did_load(self):
self.row = 16
self.column = 16
self.pixels = []
self.used_pixels = []
self.image_view = self.create_image_view()
self.grid_layout = self.create_grid_layout()
self.current_color = (1, 1, 1, 1)
self.mode = 'pencil'
def undo(self):
if len(self.used_pixels) == 0:
return
pixel = self.used_pixels.pop()
pixel.reset()
img = self.create_image_from_history()
self.image_view.image = img
self.superview['preview'].image = img
def reset(self, row=None, column=None):
if row is not None:
self.row = row
if column is not None:
self.column = column
self.pixels = []
self.used_pixels = []
self.grid_layout.image = self.create_grid_image()
self.image_view.image = self.create_new_image()
self.superview['preview'].image = self.image_view.image
def create_grid_image(self):
s = self.width/self.row if self.row > self.column else self.height/self.column
w, h = self.frame[2:]
path = ui.Path.rect(0, 0, w, h)
with ui.ImageContext(*self.frame[2:]) as ctx:
ui.set_color((0, 0, 0, 0))
path.fill()
path.line_width = 2
for y in xrange(self.column):
for x in xrange(self.row):
pixel = Pixel(x*s, y*s, s, s)
path.append_path(ui.Path.rect(*pixel))
self.pixels.append(pixel)
ui.set_color('gray')
path.stroke()
return ctx.get_image()
def create_grid_layout(self):
image_view = ui.ImageView(frame=self.bounds)
image_view.image = self.create_grid_image()
self.add_subview(image_view)
return image_view
def create_image_view(self):
image_view = ui.ImageView(frame=self.bounds)
image_view.image = self.create_new_image()
self.add_subview(image_view)
return image_view
def create_new_image(self):
path = ui.Path.rect(*self.frame)
with ui.ImageContext(self.width, self.width) as ctx:
ui.set_color((0, 0, 0, 0))
path.fill()
return ctx.get_image()
def create_image_from_history(self):
path = ui.Path.rect(*self.frame)
with ui.ImageContext(self.width, self.height) as ctx:
for pixel in self.used_pixels:
ui.set_color(pixel.color)
pixel_path = ui.Path.rect(*pixel)
pixel_path.line_width = 0.5
pixel_path.fill()
pixel_path.stroke()
img = ctx.get_image()
return img
def pencil(self, pixel):
if not pixel.used or pixel.color != self.current_color:
pixel.used = True
pixel.color = self.current_color
old_img = self.image_view.image
path = ui.Path.rect(*pixel)
with ui.ImageContext(self.width, self.height) as ctx:
if old_img:
old_img.draw()
ui.set_color(self.current_color)
pixel_path = ui.Path.rect(*pixel)
pixel_path.line_width = 0.5
pixel_path.fill()
pixel_path.stroke()
self.used_pixels.append(pixel)
img = ctx.get_image()
self.image_view.image = img
self.superview['preview'].image = img
def eraser(self, pixel):
if pixel.used:
self.used_pixels.remove(pixel)
pixel.reset()
img = self.create_image_from_history()
self.image_view.image = img
self.superview['preview'].image = img
def color_picker(self, pixel):
self.current_color = pixel.color
self.superview['colors'].set_color(pixel.color)
def action(self, touch):
for pixel in self.pixels:
p = Point(*touch.location)
if p in pixel:
eval('self.{}(pixel)'.format(self.mode))
def touch_began(self, touch):
self.action(touch)
def touch_moved(self, touch):
self.action(touch)
class ColorView (ui.View):
def did_load(self):
self.color = {'r':1, 'g':1, 'b':1, 'a':1}
for subview in self.subviews:
self.init_action(subview)
def init_action(self, subview):
if hasattr(subview, 'action'):
subview.action = self.choose_color
if hasattr(subview, 'subviews'):
for sv in subview.subviews:
self.init_action(sv)
def get_color(self):
return tuple(self.color[i] for i in 'rgba')
def set_color(self, color=None):
if color is None:
color = self.get_color()
self['r'].value, self['g'].value, self['b'].value, self['a'].value = color
self.color['r'], self.color['g'], self.color['b'], self.color['a'] = color
self['current_color'].background_color = color
self.superview['editor'].current_color = color
def choose_color(self, sender):
if sender.name in self.color.keys():
self.color[sender.name] = sender.value
self.set_color()
elif sender in self['palette'].subviews:
self.set_color(sender.background_color)
color = {k:self.get_color()[i] for i, k in enumerate('rgba')}
for i in 'rgba':
self[i].value = color[i]
class ToolbarView (ui.View):
def did_load(self):
for subview in self.subviews:
self.init_actions(subview)
def init_actions(self, subview):
if hasattr(subview, 'action'):
if isinstance(subview, ui.Button):
subview.action = self.button_tapped
elif isinstance(subview, ui.TextField):
subview.action = self.textfield_action
if hasattr(subview, 'subviews'):
for sv in subview.subviews:
self.init_actions(sv)
def textfield_action(self, sender):
if sender.name == 'bits':
row, column = eval(sender.text)
self.superview['editor'].reset(row, column)
@ui.in_background
def button_tapped(self, sender):
pixel_editor = self.superview['editor']
if sender.name == 'trash':
if console.alert('Trash', 'Are you sure you want to clear the pixel editor? Image will not be saved.', 'Yes'):
pixel_editor.image_view.image = pixel_editor.reset()
elif sender.name == 'save':
if console.alert('Save Image', 'Save image to cameraroll?', 'Yes'):
photos.save_image(pixel_editor.image_view.image)
console.hud_alert('Saved to cameraroll')
elif sender.name == 'undo':
pixel_editor.undo()
else:
pixel_editor.mode = sender.name
for b in self['tools'].subviews:
b.background_color = tuple((0, 0, 0, 0))
sender.background_color = '#4C4C4C'
root_view = ui.load_view('pixel_editor')
root_view.present()