Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial release #1

Merged
merged 2 commits into from
Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
example_*
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from lib import PySheeter
10 changes: 10 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -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)
Binary file added example/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions pysheeter/PySheeter.py
Original file line number Diff line number Diff line change
@@ -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}'")