1.0.0 - Initial release
Merge pull request #1 from VictorWesterlund/develop
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 |
68
pysheeter/PySheeter.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
import json
|
||||
from PIL import Image
|
||||
from pathlib import Path
|
||||
|
||||
# Create new sprite
|
||||
class Sprite:
|
||||
|
||||
def __init__(self,image,size):
|
||||
self.image = Image.open(image).convert("RGBA")
|
||||
|
||||
# Resize image to size[width,height] if nessesary
|
||||
if(self.image.width != size[0] or self.image.height != size[1]):
|
||||
self.resize(size)
|
||||
|
||||
# Resize image without maintaining aspect ratio
|
||||
def resize(self,size,resample=Image.LANCZOS):
|
||||
self.image = self.image.resize((size[0],size[1]),resample)
|
||||
|
||||
# --------------------------------
|
||||
|
||||
# Create new sheet of sprites
|
||||
class Sheet:
|
||||
|
||||
def __init__(self,folder = None):
|
||||
self.sprites = []
|
||||
|
||||
# Auto-import sprite folder
|
||||
if(folder):
|
||||
self.path = Path(folder).glob("**/*.png")
|
||||
self.sprites = [x for x in self.path]
|
||||
|
||||
print(f"Loaded {len(self.sprites)} sprites")
|
||||
|
||||
# Concatinate sprites vertically
|
||||
def concatV(self,size):
|
||||
sheet = Image.new("RGBA",(size[0],size[1] * len(self.sprites)))
|
||||
|
||||
for i, sprite in enumerate(self.sprites):
|
||||
sheet.paste(Sprite(sprite,size).image,(0,size[1] * i))
|
||||
|
||||
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
|
||||
def add(self,path):
|
||||
self.sprites.append(path)
|
||||
|
||||
# Remove sprite by path
|
||||
def remove(self,path):
|
||||
self.sprites.remove(path)
|
||||
|
||||
# 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}'")
|