def image_from_text(string, font_path, font_size, color=None, image_bg=None): font_path = font_path or share.GLOBAL_FONT_PATH font_size = font_size or share.GLOBAL_FONT_SIZE color = color or "#000" if image_bg: # from Button.__init__ font_size = int(font_size * (share.WIDTH_ZOOM_SCALE + share.HEIGHT_ZOOM_SCALE) / 2) string = convert.get_unicode(string) font = ImageFont.truetype(font_path, font_size) space = 1 #guess size i = w = h = space * 3 for line in string.replace("\r\n", "\n").split("\n"): for word in line: i += font_size * (0.5 if ord(word) < 255 else 1) w = max(i, w) i = space * 3 h += font_size * 1.3 + space #set bg if not image_bg: img = Image.new("RGBA", (int(w), int(h))) #, "#FFF") x, y = space, 0 else: img = image_bg x, y = int((img.size[0] - w) / 2), int((img.size[1] - h) / 2) #draw text draw = ImageDraw.Draw(img) for line in string.replace("\r\n", "\n").split("\n"): for word in line: draw.text((x, y), word, font=font, fill=color) x += font_size * (0.5 if ord(word) < 255 else 1) x = space y += font_size + space return img
def play(self, path, loop=False): self.stop() self.loop_count = 0 if loop else 1 if convert.get_unicode(path).find(">") != -1: if share.KEY: self.spawn( "http://127.0.0.1:%d/req=%s"%(share.PORT, encrypt_path(path)) ) else: self.spawn( convert.get_str(convert.get_unicode(path).replace(">", "/")) ) elif os.path.isfile(path): self.spawn(path) else: raise IOError("%s not exist"%path)
def image_from_text(string, font_path, font_size, color=None, image_bg=None): font_path = font_path or share.GLOBAL_FONT_PATH font_size = font_size or share.GLOBAL_FONT_SIZE color = color or "#000" if image_bg: # from Button.__init__ font_size = int(font_size*(share.WIDTH_ZOOM_SCALE+share.HEIGHT_ZOOM_SCALE)/2) string = convert.get_unicode(string) font = ImageFont.truetype(font_path, font_size) space = 1 #guess size i = w = h = space*3 for line in string.replace("\r\n", "\n").split("\n"): for word in line: i += font_size*(0.5 if ord(word)<255 else 1) w = max(i, w) i = space*3 h += font_size*1.3+space #set bg if not image_bg: img = Image.new("RGBA", (int(w), int(h)))#, "#FFF") x, y = space, 0 else: img = image_bg x, y = int((img.size[0]-w)/2), int((img.size[1]-h)/2) #draw text draw = ImageDraw.Draw(img) for line in string.replace("\r\n", "\n").split("\n"): for word in line: draw.text((x, y), word, font=font, fill=color) x += font_size*(0.5 if ord(word)<255 else 1) x = space y += font_size+space return img
def image_list_from_text(string, length_once, width, height, font_path, font_size, color=None): font_path = font_path or share.GLOBAL_FONT_PATH font_size = font_size or share.GLOBAL_FONT_SIZE color = color or "#000" string = convert.get_unicode(string).replace("\r\n", "\n") font = ImageFont.truetype(font_path, font_size) space = 1 x, y = space, 0 length_buffer = 0 img_list = [] draw_next = None img = Image.new("RGBA", (int(width), int(height))) draw = ImageDraw.Draw(img) for word in string: if word == "\n": x = space y += font_size + space continue if draw_next: set_text_with_alpha(draw_next, (x, y), word, font=font, fill=color, alpha=50) draw_next = None if length_buffer >= length_once: set_text_with_alpha(draw, (x, y), word, font=font, fill=color, alpha=100) length_buffer = 0 draw_next = draw img_list.append(img) img = Image.new("RGBA", (int(width), int(height))) draw = ImageDraw.Draw(img) set_text_with_alpha(draw, (x, y), word, font=font, fill=color) if ord(word) < 255: x += font_size * 0.5 length_buffer += 0.5 else: x += font_size * 1 length_buffer += 1 if (x + font_size + space) > width: x = space y += font_size + space if length_buffer: img_list.append(img) return img_list
def read_from_path(path, key): path_split = map(convert.get_str, convert.get_unicode(path).split(">")) if len(path_split) > 1: arc_path = path_split[0] zip_path = path_split[1] if exist(share.PATCH_ARCHIVE, zip_path, key): arc_path = PATCH_ARCHIVE if os.path.isdir(arc_path): data = open(os.path.join(arc_path, zip_path), "rb").read() elif os.path.isfile(arc_path): data = read_from_archive(arc_path, zip_path, key) else: raise IOError("archive %s not exist"%arc_path) else: data = open(path, "rb").read() return data
def image_list_from_text(string, length_once, width, height, font_path, font_size, color=None): font_path = font_path or share.GLOBAL_FONT_PATH font_size = font_size or share.GLOBAL_FONT_SIZE color = color or "#000" string = convert.get_unicode(string).replace("\r\n", "\n") font = ImageFont.truetype(font_path, font_size) space = 1 x, y = space, 0 length_buffer = 0 img_list = [] draw_next = None img = Image.new("RGBA", (int(width), int(height))) draw = ImageDraw.Draw(img) for word in string: if word == "\n": x = space y += font_size+space continue if draw_next: set_text_with_alpha( draw_next, (x, y), word, font=font, fill=color, alpha=50 ) draw_next = None if length_buffer >= length_once: set_text_with_alpha(draw, (x, y), word, font=font, fill=color, alpha=100) length_buffer = 0 draw_next = draw img_list.append(img) img = Image.new("RGBA", (int(width), int(height))) draw = ImageDraw.Draw(img) set_text_with_alpha(draw, (x, y), word, font=font, fill=color) if ord(word) < 255: x += font_size*0.5 length_buffer += 0.5 else: x += font_size*1 length_buffer += 1 if (x+font_size+space) > width: x = space y += font_size+space if length_buffer: img_list.append(img) return img_list
def encrypt_path(path): ext = os.path.splitext(convert.get_unicode(path))[1] or ".none" return path.encode("hex")+convert.get_str(ext)
def posix_path(path): return convert.get_str(convert.get_unicode(path).replace("\\", "/"))