Added more automatic small image generator.

This commit is contained in:
Quantum 2013-10-29 23:57:49 -04:00
parent d1bd66f463
commit e79321c8a9
3 changed files with 124 additions and 12 deletions

View file

@ -4,21 +4,18 @@ import os
import sys import sys
import uuid import uuid
def is_frozen():
import imp
return (hasattr(sys, 'frozen') or # new py2exe
hasattr(sys, 'importers') # old py2exe
or imp.is_frozen('__main__')) # tools/freeze
if __name__ == '__main__': if __name__ == '__main__':
if not is_frozen(): try:
dir = os.path.dirname(sys.executable)
if sys.frozen == 'windows_exe':
sys.stdout = open(os.path.join(dir, 'punyverse.log'), 'a')
except AttributeError:
sys.exit('This is only meant to be ran frozen.') sys.exit('This is only meant to be ran frozen.')
sys.path.insert(0, os.path.dirname(sys.executable)) sys.path.insert(0, dir)
import punyverse._model import punyverse._model
import punyverse._glgeom import punyverse._glgeom
with open('punyverse\__main__.py', 'r') as f: with open('punyverse\__main__.py', 'r') as code:
code = f.read()
exec(code) exec(code)

View file

@ -25,7 +25,7 @@ for res in resources:
data.append((dir, [join(parent, file)])) data.append((dir, [join(parent, file)]))
setup( setup(
console=[{'dest_base': 'punyverse_debug', 'script': 'bootloader.py'}], console=[{'dest_base': 'punyverse_debug', 'script': 'bootloader.py'}, 'small_images.py'],
windows=[{'dest_base': 'punyverse', 'script': 'bootloader.py'}], windows=[{'dest_base': 'punyverse', 'script': 'bootloader.py'}],
data_files=data, data_files=data,
options={'py2exe': { options={'py2exe': {
@ -34,7 +34,7 @@ setup(
'_ssl', 'unittest', 'doctest', 'PIL', 'email', 'distutils', '_ssl', 'unittest', 'doctest', 'PIL', 'email', 'distutils',
'pyglet.window.carbon', 'pyglet.window.xlib', 'pyglet.window.carbon', 'pyglet.window.xlib',
'pyglet.media.drivers.alsa', 'pyglet.media.drivers.alsa',
'win32wnet', 'netbios' 'win32wnet', 'netbios', 'pgmagick'
], ],
'dll_excludes': ['MPR.dll', 'w9xpopen.exe'], 'dll_excludes': ['MPR.dll', 'w9xpopen.exe'],
} }

115
small_images.py Normal file
View file

@ -0,0 +1,115 @@
import sys
import os
from pyglet.gl import GLint, glGetIntegerv, GL_MAX_TEXTURE_SIZE
from ctypes import byref
buf = GLint()
glGetIntegerv(GL_MAX_TEXTURE_SIZE, byref(buf))
max_texture = buf.value
del byref, GLint, glGetIntegerv, GL_MAX_TEXTURE_SIZE, buf
def resize(width, height, target):
factor = (target + .0) / max(width, height)
return int(width * factor), int(height * factor)
def fits(width, height):
return width < max_texture and height < max_texture
def make_name(image, suffix):
name, ext = os.path.splitext(image)
return '%s_%s%s' % (name, suffix, ext)
try:
import pgmagick
def get_image(image):
return pgmagick.Image(image)
def get_size(image):
size = image.size()
return size.width(), size.height()
def scale(image, width, height):
image.filterType(pgmagick.FilterTypes.LanczosFilter)
image.scale(pgmagick.Geometry(width, height))
return image
def save(image, file):
image.write(file)
except ImportError:
import Image
def get_image(image):
return Image.open(image)
def get_size(image):
return image.size
def scale(image, width, height):
return image.resize((width, height), Image.ANTIALIAS)
def save(image, file):
image.save(file)
def shrink(file):
image = get_image(file)
width, height = get_size(image)
if fits(width, height):
print 'no need'
return
width, height = resize(width, height, 2048)
if fits(width, height):
size = 'medium'
else:
width, height = resize(width, height, 1024) # 1024 is minimum
size = 'small'
print 'size %s, %dx%d...' % (size, width, height),
name = make_name(file, size)
if not os.path.exists(name):
image = scale(image, width, height)
print 'saved to:', os.path.basename(name)
save(image, name)
else:
print
textures = [
'earth.jpg',
'moon.jpg',
'mars.jpg',
'jupiter.jpg',
'saturn.jpg',
'moons/io.jpg',
'moons/europa.jpg',
'moons/ganymede.jpg',
'moons/callisto.jpg',
'moons/titan.jpg',
'moons/rhea.jpg',
'moons/iapetus.jpg',
'moons/dione.jpg',
'moons/tethys.jpg',
'moons/enceladus.jpg',
'moons/mimas.jpg',
]
def frozen():
import imp
return (hasattr(sys, "frozen") or # new py2exe
hasattr(sys, "importers") # old py2exe
or imp.is_frozen("__main__")) # tools/freeze
def get_main_dir():
if frozen():
return os.path.dirname(sys.executable)
return os.path.dirname(__file__)
def main():
texture = os.path.join(get_main_dir(), 'punyverse', 'assets', 'textures')
for file in textures:
print 'Resizing %s:' % file,
file = os.path.join(texture, file.replace('/', os.sep))
shrink(file)
if __name__ == '__main__':
main()