Path: blob/master/src/framedata.py
254 views
from PIL import Image1from utils import g_settings, imghashes, spritesheet_split_cache23class FrameData:4def __init__(self, impath, from_single_png, pose_name, **xmlinfo):5# get imgpath6# get img from imgpath and ?xmlinfo7# set appropriate frame stuff: depending on from_single_png8# hash img9# other stuff...10# needed in XML table window11self.imgpath = impath12self.tx = None13self.ty = None1415# w, h needed to crop img16self.tw = None17self.th = None18self.from_single_png = from_single_png1920img = Image.open(impath).convert('RGBA')21self.framex = 022self.framey = 023self.framew = img.width24self.frameh = img.height25should_clip = g_settings['isclip'] != 026cached_hash = None27if not self.from_single_png:28self.tx = xmlinfo.get("tx", 0)29self.ty = xmlinfo.get("ty", 0)30self.tw = xmlinfo.get("tw", 0)31self.th = xmlinfo.get("th", 0)32# impath, tex_coords (, clip) -> img33# if clip == True here, then skip clip step ("if should_clip:...")34# check if this img is in cache35cached_hash = spritesheet_split_cache[impath].get((self.tx, self.ty, self.tw, self.th, should_clip))36if not cached_hash:37# crop the image38img = img.crop((self.tx, self.ty, self.tx + self.tw, self.ty + self.th))39else:40# print("[DEBUG] Img found in cache!")41img = imghashes.get(cached_hash)42# set frame properties from xml43self.framex = xmlinfo.get("framex", 0)44self.framey = xmlinfo.get("framey", 0)45self.framew = xmlinfo.get("framew", 0)46self.frameh = xmlinfo.get("frameh", 0)4748# clipping the image if i didn't already find it in the cache49if not cached_hash and should_clip:50imbbox = img.getbbox()51if imbbox:52# crop img53img = img.crop(imbbox)54# adjust frame properties such that image can be reconstructed from them55x1, y1, _, _ = imbbox56self.framex -= x157self.framey -= y158# Note: frame width and height stay the same59else:60print("Unable to crop image!")6162# storing some img properties here for efficiency(kinda)63self.img_width = img.width64self.img_height = img.height6566# get hash67self.img_hash = cached_hash if cached_hash else hash(img.tobytes())68# if hash isnt in imghashes then add it69if self.img_hash not in imghashes:70imghashes[self.img_hash] = img71# cache image for re-use (only imgs from xmls are cached this way)72if not self.from_single_png:73spritesheet_split_cache[impath][(self.tx, self.ty, self.tw, self.th, should_clip)] = self.img_hash74elif cached_hash:75pass76else:77img.close()7879self.pose_name = pose_name80self.xml_pose_name = ""8182def change_img(self, newimg):83# this method will mostly only be called when flipping the images84# so frame data will be unaltered85self.img_width = newimg.width86self.img_height = newimg.height8788self.img_hash = hash(newimg.tobytes())89if self.img_hash not in imghashes:90imghashes[self.img_hash] = newimg91else:92newimg.close()9394# not used as of now95class FrameImgData:96def __init__(self, imgpath, from_single_png, **texinfo):97self.imgpath = imgpath98self.from_single_png = from_single_png99if self.from_single_png:100self.img = Image.open(imgpath)101self.img_width = self.img.width102self.img_height = self.img.height103else:104im = Image.open(imgpath)105self.tx = int(texinfo.get("tx", 0))106self.ty = int(texinfo.get("ty", 0))107self.tw = int(texinfo.get("tw", 0))108self.th = int(texinfo.get("th", 0))109self.img_width = self.tw110self.img_height = self.th111self.img = im.crop((self.tx, self.ty, self.tx + self.tw, self.ty + self.th))112im.close()113114self.img_hash = hash(self.img.tobytes())115self.img.close()116self.is_flip_x = False117self.is_flip_y = False118119def __str__(self):120return f"""Frame Image data:121Image path: {repr(self.imgpath)}122From single png: {repr(self.from_single_png)}123Width: {repr(self.img_width)}124Height: {repr(self.img_height)}125Flip-X: {repr(self.is_flip_x)}126Flip-Y: {repr(self.is_flip_y)}127"""128129def modify_image_to(self, im):130# modifies PIL image object itself (does not change imgpath though)131self.img = im132self.img_width = im.width133self.img_height = im.height134135# not used as of now136# class FrameXMLData:137# def __init__(self, pose_name, x, y, w, h, framex, framey, framew, frameh, flipx=False, flipy=False):138# self.pose_name = pose_name139# self.x = x140# self.y = y141# self.w = w142# self.h = h143# self.framex = framex144# self.framey = framey145# self.framew = framew146# self.frameh = frameh147# self.is_flip_x = flipx148# self.is_flip_y = flipy149# self.xml_posename = None150# # not exactly relevant to the xml but still151# self.from_single_png = False152153# def convert_to_dict(self):154# attribs = {155# "name": self.pose_name,156# "x": self.x,157# "y": self.y,158# "width": self.w,159# "height": self.h160# }161162# if self.framex:163# attribs.update({164# "frameX": self.framex,165# "frameY": self.framey,166# "frameWidth": self.framew,167# "frameHeight": self.frameh,168# })169170# return attribs171172# def __str__(self):173# return f"""Frame XML data:174# FrameX: {repr(self.framex)}175# FrameY: {repr(self.framey)}176# FrameWidth: {repr(self.framew)}177# FrameHeight: {repr(self.frameh)}178# """179180