| 1 |
""" |
| 2 |
Copyright (C) 2013 Mayank Jha <[email protected]> |
| 3 |
|
| 4 |
This library is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU Lesser General Public License as published |
| 6 |
by the Free Software Foundation; either version 2.1 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU Lesser General Public License |
| 15 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
""" |
| 17 |
""" |
| 18 |
Copyright (C) 2013 Mayank Jha <[email protected]> |
| 19 |
|
| 20 |
This library is free software; you can redistribute it and/or modify |
| 21 |
it under the terms of the GNU Lesser General Public License as published |
| 22 |
by the Free Software Foundation; either version 2.1 of the License, or |
| 23 |
(at your option) any later version. |
| 24 |
|
| 25 |
This program is distributed in the hope that it will be useful, |
| 26 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 28 |
GNU General Public License for more details. |
| 29 |
|
| 30 |
You should have received a copy of the GNU Lesser General Public License |
| 31 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 32 |
""" |
| 33 |
from PIL import Image |
| 34 |
def white_bg_square(img): |
| 35 |
"return a white-background-color image having the img in exact center" |
| 36 |
size = (max(img.size),)*2 |
| 37 |
layer = Image.new('1', size, 1) |
| 38 |
layer.paste(img, tuple(map(lambda x:(x[0]-x[1])/2, zip(size, img.size)))) |
| 39 |
return layer |
| 40 |
|
| 41 |
class BitmapCompare: |
| 42 |
def basicCompare(self,testbmp,standardsprite,size,xoffset,equal=False): |
| 43 |
master = Image.new('1',(size,size),0) |
| 44 |
img = Image.open(testbmp) |
| 45 |
square_one = white_bg_square(img) |
| 46 |
square_one.resize((size,size)) |
| 47 |
master.paste(square_one,(0,0)) |
| 48 |
master.save(testbmp) |
| 49 |
if equal: |
| 50 |
master = Image.new('1',(size,size),0) |
| 51 |
img = Image.open(standardsprite) |
| 52 |
square_one = white_bg_square(img) |
| 53 |
square_one.resize((size,size)) |
| 54 |
master.paste(square_one,(0,0)) |
| 55 |
master.save(standardsprite) |
| 56 |
sprite = Image.open(standardsprite) |
| 57 |
test = Image.open(testbmp) |
| 58 |
count = 0 |
| 59 |
i=0 |
| 60 |
while i<size: |
| 61 |
j=0 |
| 62 |
while j<size: |
| 63 |
if sprite.getpixel((j+xoffset,i)) == test.getpixel((j,i)): |
| 64 |
count+=1 |
| 65 |
j=j+1 |
| 66 |
i=i+1 |
| 67 |
return float(count)/(size*size) |
| 68 |
|
| 69 |
|
| 70 |
class CreateSpriteSheet: |
| 71 |
def __init__(self,pixelsize,font,glyphrange,effects): |
| 72 |
master_width = (pixelsize * (glyphrange[1]-glyphrange[0]+1) ) |
| 73 |
|
| 74 |
master_height = pixelsize |
| 75 |
oldfont = font |
| 76 |
print "the master image will by %d by %d" % (master_width, master_height) |
| 77 |
print "creating image..." |
| 78 |
master = Image.new( |
| 79 |
mode='1', |
| 80 |
size=(master_width, master_height), |
| 81 |
color=0) |
| 82 |
print "created." |
| 83 |
if effects == "italic": |
| 84 |
font.selection.all() |
| 85 |
font = font.italicize(-13) |
| 86 |
count=0 |
| 87 |
for i in range (glyphrange[0],glyphrange[1]): |
| 88 |
location = pixelsize*count |
| 89 |
try: |
| 90 |
t=font[i] |
| 91 |
if effects == "bold": |
| 92 |
font[i].changeWeight(50,"auto",0,0,"auto") |
| 93 |
font[i].export("temp.bmp",pixelsize,1) |
| 94 |
img = Image.open("temp.bmp") |
| 95 |
print "adding %s at %d..." % (str(i)+".bmp", location), |
| 96 |
square_one = white_bg_square(img) |
| 97 |
square_one.resize((pixelsize, pixelsize)) |
| 98 |
master.paste(square_one,(location,0)) |
| 99 |
print "added." |
| 100 |
except: |
| 101 |
print "ooopsy" |
| 102 |
count+=1 |
| 103 |
print "done adding pics." |
| 104 |
print "saving mastersprite.bmp..." |
| 105 |
master.save('data/mastersprite'+effects+'.bmp' ) |
| 106 |
print "saved!" |
| 107 |
font.close() |