Beispiel #1
0
    def create_image(self, text_file, out_file=None, font=None, bgcolor=(0,0,0),fgcolor=(255,255,255)):
        '''
        Uses text_file to generate an image file saved as out_file
        '''
    
        filename = text_file.split("/")[-1]



        text = []
        with open(text_file) as file:
            for line in file:
                text.append(line)

        
        widest = 0
        for line in text:
            if font.getsize(line)[0] > widest:
                widest = font.getsize(line)[0]
        
        width = widest + 2*self.margin_width
        height = len(text) * (font.size+2) + 2*self.margin_width

        img = PIL.Image.new("RGB",(width, height),bgcolor)
        draw = ImageDraw.Draw(img)

        draw.text((self.margin_width,0),filename,font=font,fill=(0,0,255))
        for i, line in enumerate(text): # I'm not 100% sure of this spacing
            draw.text((self.margin_width, self.margin_width+i*(font.size+2)),line,font=font,fill=fgcolor)

        return img
 def get_height(self):
     #Get height of the graphic in pixels
     font = PIL.ImageFont.truetype(self.font, self.size)
     text_lines = self.text.split("\n")
     longest_line = max(text_lines, key=lambda x: len(x)).strip()
     height = font.getsize(longest_line)[1] * len(text_lines)
     return height
 def get_width(self):
     #Get width of the graphic in pixels
     font = PIL.ImageFont.truetype(self.font, self.size)
     text_lines = self.text.split("\n")
     longest_line = max(text_lines, key=lambda x: len(x)).strip()
     width = font.getsize(longest_line)[0]
     return width
Beispiel #4
0
    def watermark(self):
        # Taken from https://medium.com/analytics-vidhya/some-interesting-tricks-in-python-pillow-8fe5acce6084
        """mark the picture with a watermark"""
        # open image to apply watermark to
        im = Image.open(self.fileName)
        # manipulate file name for save process
        baseFile = self.fileName.split('/')
        length = len(baseFile)
        base = baseFile[len(baseFile) - 1]
        print(baseFile[len(baseFile) - 1])
        im.convert("RGB")
        # get image size
        im_width, im_height = im.size
        # 5 by 4 water mark grid
        wm_size = (int(im_width * 0.20), int(im_height * 0.25))
        wm_txt = Image.new("RGBA", wm_size, (255, 255, 255, 0))
        # set text size, 1:40 of the image width
        font_size = int(im_width / 40)
        # load font e.g. gotham-bold.ttf
        font = ImageFont.truetype('/Library/Fonts/Arial.ttf', 20)

        d = ImageDraw.Draw(wm_txt)
        wm_text = "Scott Bing"
        # centralize text
        left = (wm_size[0] - font.getsize(wm_text)[0]) / 2
        top = (wm_size[1] - font.getsize(wm_text)[1]) / 2
        # RGBA(0, 0, 0, alpha) is black
        # alpha channel specifies the opacity for a colour
        alpha = 75
        # write text on blank wm_text image
        d.text((left, top), wm_text, fill=(0, 0, 0, alpha), font=font)
        # uncomment to rotate watermark text
        # wm_txt = wm_txt.rotate(15,  expand=1)
        # wm_txt = wm_txt.resize(wm_size, Image.ANTIALIAS)
        for i in range(0, im_width, wm_txt.size[0]):
            for j in range(0, im_height, wm_txt.size[1]):
                im.paste(wm_txt, (i, j), wm_txt)

        im.save('watermark-' + base)
        print("file watermark-" + base + " saved")

        self.clearScreen()
Beispiel #5
0
    def find_proportions(self, row_obj: dict, font_size: int) -> (Tuple[ImageFont.truetype, int]):
        """This function calculates the text proportions based on current font size.
        """
        font = ImageFont.truetype('./fonts/cc-wild-words-roman.ttf', font_size)
        font_size_px = font.getsize(text=row_obj.get('translated'))
        words_per_row = self.how_many_words_per_row(
            text=row_obj.get('translated'),
            font_size=font_size_px,
            x_area=row_obj.get('text_box_size')[0]
        )

        return (words_per_row, font, font_size)
        def update_text_loc(mouse_event):
            #Change the location of the selected graphic to the mouse location
            font = PIL.ImageFont.truetype(self.selected_graphic.font,
                                          self.selected_graphic.size)

            #Calculate the size and midpoint of the text:
            text_lines = self.selected_graphic.text.split("\n")
            longest_line = max(text_lines, key=lambda x: len(x)).strip()
            width, height = font.getsize(longest_line)
            height = height * len(text_lines)
            #Move the graphic's centre to the mouse curser
            self.selected_graphic.location = (mouse_event.x - int(0.5 * width),
                                              mouse_event.y -
                                              int(0.5 * height))
            self.update_image_preview(
            )  #my_max(nested_list, key_func=lambda x: x[1])