def __init__(self, img, ismask=False, transparent=True, fromalpha=False, duration=None, dpi=96, width=None, height=None): self.svg = svgutils.transform.fromfile(img) svg_bytestring = self.svg.to_str() png_file = io.BytesIO() if width is not None and height is not None: current_width = float(self.svg.width.replace("px", "")) current_height = float(self.svg.height.replace("px", "")) scale = max(height / current_height, width / current_width) cairosvg.svg2png(bytestring=svg_bytestring, write_to=png_file, parent_width=width, parent_height=height, scale=scale) else: #Converts to png and saves to bytestring cairosvg.svg2png(bytestring=svg_bytestring, write_to=png_file) #np_img = svg_to_npim(svg_bytestring, dpi) np_img = imread(png_file.getvalue()) ImageClip.__init__(self, np_img, ismask=ismask, transparent=transparent, fromalpha=fromalpha, duration=duration)
def __init__(self, txt=None, filename=None, size=None, color='black', bg_color='transparent', fontsize=None, font=None, stroke_color=None, stroke_width=1, method='label', kerning=None, align='center', interline=None, tempfilename=None, temptxt=None, transparent=True, remove_temp=True, print_cmd=False): #For nicer transparent compositing: https://nedbatchelder.com/blog/200801/truly_transparent_text_with_pil.html #https://www.quora.com/Which-graphics-module-for-Python-is-best-for-antialiased-drawing self.txt = txt self.color = color if fontsize is None and size is not None and font is not None: fontsize = self._find_fontsize(font, size) elif fontsize is None: fontsize = 10 #First part is writting txt to tempfile mode = "RGBA" if transparent else "RGB" if font is None: font = ImageFont.load_default() else: #Font cache fs = (font, fontsize) if fs in TextClipPIL.text_cache: font = TextClipPIL.text_cache[fs] else: font = ImageFont.truetype(font, fontsize) TextClipPIL.text_cache[fs] = font if size is None: size = font.getsize(txt) size = tuple(math.ceil(x) for x in size) if print_cmd: print(txt, mode, fontsize, size) if transparent and bg_color == "transparent": #Setting background color to fully transparent font color makes much nicer #antialias fonts rgb_color = list(ImageColor.getcolor(color, mode)) rgb_color[-1] = 0 rgb_trans = tuple(rgb_color) im = Image.new(mode, size, rgb_trans) else: im = Image.new(mode, size) d = ImageDraw.Draw(im) #Stroke color: #https://mail.python.org/pipermail/image-sig/2009-May/005681.html #d.fontmode = "l" #d.text((0-1,0), txt, fill=stroke_color, font=font) #d.text((0+1,0), txt, fill=stroke_color, font=font) #d.text((0,0-1), txt, fill=stroke_color, font=font) #d.text((0,0+1), txt, fill=stroke_color, font=font) d.text((0, 0), txt, fill=color, font=font) ImageClip.__init__(self, np.asarray(im), transparent=transparent)
def __init__(self, txt, size=None, color='black', bg_color='transparent', fontsize=None, font='Courier', stroke_color=None, stroke_width=1, method='label', kerning=None, align='center', interline=None, tempfilename=None, temptxt=None, transparent=True, remove_temp=True, print_cmd=False): if size is not None: size = ('' if size[0] is None else str(size[0]), '' if size[1] is None else str(size[1])) cmd = ([get_setting("IMAGEMAGICK_BINARY"), "-background", bg_color, "-fill", color, "-font", font]) if fontsize is not None: cmd += ["-pointsize", "%d" % fontsize] if kerning is not None: cmd += ["-kerning", "%0.1f" % kerning] if stroke_color is not None: cmd += ["-stroke", stroke_color, "-strokewidth", "%.01f" % stroke_width] if size is not None: cmd += ["-size", "%sx%s" % (size[0], size[1])] if align is not None: cmd += ["-gravity", align] if interline is not None: cmd += ["-interline-spacing", "%d" % interline] if tempfilename is None: tempfile_fd, tempfilename = tempfile.mkstemp(suffix='.png') os.close(tempfile_fd) print(txt) cmd += ["%s:'%s'" % (method, txt), "-type", "truecolormatte", "PNG32:%s" % tempfilename] print(cmd) if print_cmd: print(" ".join(cmd)) try: subprocess_call(cmd, verbose=False) except (IOError, OSError) as err: error = ("MoviePy Error: creation of %s failed because of the " "following error:\n\n%s.\n\n." % (tempfilename, str(err)) + ("This error can be due to the fact that ImageMagick " "is not installed on your computer, or (for Windows " "users) that you didn't specify the path to the " "ImageMagick binary in file conf.py, or that the path " "you specified is incorrect")) raise IOError(error) ImageClip.__init__(self, tempfilename, transparent=transparent) self.txt = txt self.color = color self.stroke_color = stroke_color if remove_temp: if os.path.exists(tempfilename): os.remove(tempfilename) if os.path.exists(temptxt): os.remove(temptxt)