-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathandroid.py
More file actions
executable file
·249 lines (200 loc) · 7.9 KB
/
android.py
File metadata and controls
executable file
·249 lines (200 loc) · 7.9 KB
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
#!/usr/bin/env python
from array import array
from gimpfu import *
import os.path
ELASTIC_TATTOO = 991
CONTENT_TATTOO = 992
pdb = gimp.pdb
def android_ninepatch_prepare( image, current_layer ):
"""
Prepare an image to be an android 9-patch image.
This creates two layers "9-elastic" and "9-content" on which the
user draws the elastic and content regions of the eventual 9-patch
PNG. Since the regions mask off content and elastic regions, they
are more durable against resizing, and using the function
android_ninepatch_render will perform all the steps to change
these regions into the border annotations of a 9-patch PNG.
"""
width = image.width
height = image.height
# Create the new layers
elastic = gimp.Layer( image, "9-elastic", width, height,
RGBA_IMAGE, 33, 0 )
content = gimp.Layer( image, "9-content", width, height,
RGBA_IMAGE, 33, 0 )
# Add their tatoos
elastic.tattoo = ELASTIC_TATTOO
content.tattoo = CONTENT_TATTOO
# Then place the layers into the image
pdb.gimp_undo_push_group_start( image )
image.add_layer( elastic, -1 )
image.add_layer( content, -1 )
pdb.gimp_undo_push_group_end( image )
def get_alpha_pixels( layer ):
reg = layer.get_pixel_rgn( 0, 0, layer.width, layer.height )
arr = array( 'B', reg[ 0:layer.width, 0:layer.height ] )
return arr[ 3::4 ] # Return only the alpha portions
def android_ninepatch_render( image, current_layer ):
"""
Render a 9-patch border.
The image must first have been prepared with the function
android_ninepatch_prepare, and the content and elastic areas
defined. This will then increase the canvas size by a single
pixel in every dimension, add a layer, and draw the 9-patch PNG
annotations necessary to make the image (after flattening) a true
9-patch PNG.
This may safely be called on unprepared images, but will have no
effect.
"""
if not is_ninepatch( image ):
# Nothing to be done
return
imageWidth = image.width + 2
imageHeight = image.height + 2
elastic = image.get_layer_by_tattoo( ELASTIC_TATTOO )
content = image.get_layer_by_tattoo( CONTENT_TATTOO )
# Bundle everything into a single undo
pdb.gimp_undo_push_group_start( image )
# Create the transparent border layer
image.resize( imageWidth, imageHeight, 1, 1 )
border = gimp.Layer( image, "9-patch border",
imageWidth, imageHeight,
RGBA_IMAGE, 100, 0 )
border.tattoo = 990
image.add_layer( border, -1 )
# Select the brush useful for drawing the border
pdb.gimp_brushes_set_brush( "1. Pixel" )
pdb.gimp_context_set_brush_size( 1 )
pdb.gimp_context_set_foreground( (0,0,0) )
pdb.gimp_context_set_opacity( 100 )
# Use pixel regions to brute-force opacities
target = border.get_pixel_rgn( 0, 0, image.width, image.height )
targetArray = array( 'B', target[ 0:target.width, 0:target.height ] )
eArray = get_alpha_pixels( elastic )
cArray = get_alpha_pixels( content )
# Coordinate parts for the border
(et, el) = elastic.offsets;
(ct, cl) = content.offsets;
top = 0;
left = 0;
bottom = imageHeight -1;
right = imageWidth -1;
# Mark left (elastic) side
for row in xrange( 0, elastic.height ):
if any( a > 0 for a in eArray[ (row * elastic.width) :
((row + 1) * elastic.width) ] ):
pdb.gimp_pencil( border, 2, (left, row + et) )
# Mark right (content) side
for row in xrange( 0, content.height ):
if any( a > 0 for a in cArray[ (row * content.width) :
((row+1) * content.width) ] ):
pdb.gimp_pencil( border, 2, (right, row + ct) )
# Mark top (elastic) side
for col in xrange( 0, elastic.width ):
if any( a > 0 for a in eArray[ col::elastic.width ] ):
pdb.gimp_pencil( border, 2, (col + el, top) )
# Mark bottom (content) side
for col in xrange( 0, content.width ):
if any( a > 0 for a in cArray[ col::elastic.width ] ):
pdb.gimp_pencil( border, 2, (col + cl, bottom ) )
elastic.visible = False
content.visible = False
pdb.gimp_undo_push_group_end( image )
def is_ninepatch( image ):
elastic = image.get_layer_by_tattoo( ELASTIC_TATTOO )
content = image.get_layer_by_tattoo( CONTENT_TATTOO )
return (elastic is not None) and (content is not None)
def mk9filename( image ):
filename = pdb.gimp_image_get_filename( image )
filename = os.path.basename( filename )
if( is_ninepatch( image ) ):
ext = ".9.png"
else:
ext = ".png"
return os.path.splitext( filename )[0] + ext
def android_ninepatch_save( image, layer, directory, scaleFactor ):
"""
Render and save a 9-patch PNG.
"""
# Figure out what our filenames should be (temporary, and later)
newFile = pdb.gimp_temp_name( "xcf" )
pngFile = os.path.join( directory, mk9filename( image ) )
#pngFile.mkdirs()
# Save (and then load) the temporary file
pdb.gimp_xcf_save( 0, image, layer, newFile, newFile )
newImage = pdb.gimp_xcf_load( 0, newFile, newFile )
# Scale the image then render the 9-patch
pdb.gimp_image_scale_full( newImage,
scaleFactor * image.width,
scaleFactor * image.height,
INTERPOLATION_CUBIC )
android_ninepatch_render( newImage, None )
# Create a new layer with all layers merged
resultLayer = pdb.gimp_image_merge_visible_layers( newImage, EXPAND_AS_NECESSARY )
# Now export to PNG, and then delete the image
pdb.file_png_save2( newImage,
resultLayer, pngFile, pngFile,
0, 9, 0, 0, 0, 0, 0, 0, 0 )
pdb.gimp_image_delete( newImage )
def mkdirs( path ):
try:
os.makedirs( path )
except: pass
def android_save_resolutions( image, layer, directory ):
"""
Save an image in many resolutions to a project.
This takes a project directory as a parameter, and saves the image
as low, medium, and high densities (scaled appropriately, of
course) to the appropriate drawable directories within the project.
"""
resolution = image.resolution[0]
print "Saving all resolutions to %s" % directory
res = os.path.join( directory, 'res' )
mdpi = os.path.join( res, 'drawable-mdpi' )
hdpi = os.path.join( res, 'drawable-hdpi' )
xhdpi = os.path.join( res, 'drawable-xhdpi' )
mkdirs( mdpi )
mkdirs( hdpi )
mkdirs( xhdpi )
android_ninepatch_save( image, layer, mdpi, 160 / resolution )
android_ninepatch_save( image, layer, hdpi, 240 / resolution )
android_ninepatch_save( image, layer, xhdpi, 320 / resolution )
register(
"android-ninepatch-prepare",
"Prepare image as 9-patch",
android_ninepatch_prepare.__doc__.strip(),
"Greyson Fischer",
"Copyright 2011-2014, Greyson Fischer",
"October 16, 2014",
"<Image>/Image/Android/Prepare 9-patch",
"RGBA",
[],
[],
android_ninepatch_prepare )
register(
"android-ninepatch-render",
"Render image as 9-patch",
android_ninepatch_render.__doc__.strip(),
"Greyson Fischer",
"Copyright 2011-2014, Greyson Fischer",
"October 16, 2014",
"<Image>/Image/Android/Render 9-patch",
"RGBA",
[],
[],
android_ninepatch_render )
register(
"android-save-resolutions",
"Save an image in {m,h,xh}dpi resolutions to a project",
android_save_resolutions.__doc__.strip(),
"Greyson Fischer",
"Copyright 2011-2014, Greyson Fischer",
"October 16, 2014",
"<Image>/Image/Android/Save to project",
"RGBA",
[ (PF_DIRNAME, "directory", "Project directory", "/tmp") ],
[],
android_save_resolutions )
main()
__author__ = "Greyson Fischer <gfischer@foosoft.us>"
__copyright__ = "Copyright 2012 Greyson Fischer"