1.0.0
Moved lib into 'pysheeter/' and an import reference with '__init__.py'. Added example files
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
__pycache__/
|
||||||
|
example_*
|
1
__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
from lib import PySheeter
|
10
example.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
from pysheeter import PySheeter
|
||||||
|
|
||||||
|
# Load sprites from 'example/'
|
||||||
|
spritesheet = PySheeter.Sheet("example")
|
||||||
|
|
||||||
|
# Create a vertical spritesheet with the dimensions 16x16
|
||||||
|
spritesheet.put("example_v1616.png",(16,16))
|
||||||
|
|
||||||
|
# Create a horizontal spritesheet with the dimensions 16x32
|
||||||
|
spritesheet.put("example_h1632.png",(16,32),False)
|
BIN
example/1.png
Normal file
After Width: | Height: | Size: 373 B |
BIN
example/2.png
Normal file
After Width: | Height: | Size: 406 B |
BIN
example/3.png
Normal file
After Width: | Height: | Size: 471 B |
BIN
example/4.png
Normal file
After Width: | Height: | Size: 291 B |
BIN
example/5.png
Normal file
After Width: | Height: | Size: 333 B |
BIN
example/6.png
Normal file
After Width: | Height: | Size: 333 B |
BIN
example/7.png
Normal file
After Width: | Height: | Size: 280 B |
BIN
example/8.png
Normal file
After Width: | Height: | Size: 333 B |
BIN
example/9.png
Normal file
After Width: | Height: | Size: 450 B |
|
@ -10,22 +10,11 @@ class Sprite:
|
||||||
|
|
||||||
# Resize image to size[width,height] if nessesary
|
# Resize image to size[width,height] if nessesary
|
||||||
if(self.image.width != size[0] or self.image.height != size[1]):
|
if(self.image.width != size[0] or self.image.height != size[1]):
|
||||||
self.resize()
|
self.resize(size)
|
||||||
|
|
||||||
# Resize image without maintaining aspect ratio
|
# Resize image without maintaining aspect ratio
|
||||||
def resize(self,resample=Image.BICUBIC):
|
def resize(self,size,resample=Image.LANCZOS):
|
||||||
rw = self.image.width
|
self.image = self.image.resize((size[0],size[1]),resample)
|
||||||
rh = self.image.height
|
|
||||||
|
|
||||||
# Scale image width
|
|
||||||
if(rw != size[0]):
|
|
||||||
rw = int(self.image.height * self.image.width / size[0])
|
|
||||||
|
|
||||||
# Scale image height
|
|
||||||
if(rh != size[1]):
|
|
||||||
rh = int(self.image.width * size[1] / self.image.height)
|
|
||||||
|
|
||||||
self.image = self.image.resize((rw,rh),resample)
|
|
||||||
|
|
||||||
# --------------------------------
|
# --------------------------------
|
||||||
|
|
||||||
|
@ -40,8 +29,10 @@ class Sheet:
|
||||||
self.path = Path(folder).glob("**/*.png")
|
self.path = Path(folder).glob("**/*.png")
|
||||||
self.sprites = [x for x in self.path]
|
self.sprites = [x for x in self.path]
|
||||||
|
|
||||||
|
print(f"Loaded {len(self.sprites)} sprites")
|
||||||
|
|
||||||
# Concatinate sprites vertically
|
# Concatinate sprites vertically
|
||||||
def concat(self,size):
|
def concatV(self,size):
|
||||||
sheet = Image.new("RGBA",(size[0],size[1] * len(self.sprites)))
|
sheet = Image.new("RGBA",(size[0],size[1] * len(self.sprites)))
|
||||||
|
|
||||||
for i, sprite in enumerate(self.sprites):
|
for i, sprite in enumerate(self.sprites):
|
||||||
|
@ -49,13 +40,29 @@ class Sheet:
|
||||||
|
|
||||||
return sheet
|
return sheet
|
||||||
|
|
||||||
|
# Concatinate sprites horizontally
|
||||||
|
def concatH(self,size):
|
||||||
|
sheet = Image.new("RGBA",(size[0] * len(self.sprites),size[1]))
|
||||||
|
|
||||||
|
for i, sprite in enumerate(self.sprites):
|
||||||
|
sheet.paste(Sprite(sprite,size).image,(size[0] * i,0))
|
||||||
|
|
||||||
|
return sheet
|
||||||
|
|
||||||
# Add sprite by path
|
# Add sprite by path
|
||||||
def add(self,path):
|
def add(self,path):
|
||||||
self.sprites.append(path)
|
self.sprites.append(path)
|
||||||
|
|
||||||
# Create and save spritesheet
|
# Remove sprite by path
|
||||||
def put(self,dest,size):
|
def remove(self,path):
|
||||||
sheet = self.concat(size)
|
self.sprites.remove(path)
|
||||||
sheet.save(dest)
|
|
||||||
|
|
||||||
print(len(self.sprites))
|
# Create and save spritesheet
|
||||||
|
def put(self,dest,size,vertical = True):
|
||||||
|
if(vertical):
|
||||||
|
sheet = self.concatV(size)
|
||||||
|
else:
|
||||||
|
sheet = self.concatH(size)
|
||||||
|
|
||||||
|
sheet.save(dest)
|
||||||
|
print(f"Saved spritesheet to '{dest}'")
|