diff --git a/format1.py b/format1.py new file mode 100644 index 0000000..493a64a --- /dev/null +++ b/format1.py @@ -0,0 +1,93 @@ +from manipulation import * + +class Format1: + + """ + Type 1 -> Text on the top of the image. + Type 2 -> Text in the bottom of the image. + Type 3 -> Text on top and bottom of the image. + """ + + def __init__( + self, + image_path, + top_text=None, + bottom_text=None, + font_path='impact/impact.ttf', + font_size=9, + ): + self.image_path = image_path + self.top_text = top_text + self.bottom_text = bottom_text + self.font_path = font_path + self.font_size = font_size + + def generate(self): + img = Image.open(self.image_path) + + if self.top_text and self.bottom_text: + + img = text_on_top(self.top_text, img) + image = text_in_bottom(self.bottom_text, img) + + elif self.top_text: + + image = text_on_top(self.top_text, img) + + elif self.bottom_text: + + image = text_in_bottom(self.bottom_text, img) + + #image.save('meme-' + img.filename.split(os.sep)[-1]) + path, imagename = os.path.split(img.filename) + image.save(os.path.join(path,'meme-' + imagename)) + add_logo(image) + return image + +format1type1 = """ +Type 1: __________________ + | Text on top | + | | + | | + | | + |________________| + +""" +format1type2 = """ +Type 2: __________________ + | | + | | + | | + | Text in bottom | + |________________| + +""" +format1type3 = """ +Type 3: __________________ + | Text on top | + | | + | | + | Text in bottom | + |________________| +""" + # def generate(self): + # img = Image.open(self.image_path) + # draw = ImageDraw.Draw(img) + # (image_width, image_height) = img.size + # font = ImageFont.truetype(font=self.font_path, + # size=int(image_height + # * self.font_size) // 100) + # self.top_text = self.top_text.upper() + # (char_width, char_height) = font.getsize('A') + # chars_per_line = image_width // char_width + # top_lines = textwrap.wrap(self.top_text, width=chars_per_line) + # y = 10 + # + # for line in top_lines: + # (line_width, line_height) = font.getsize(line) + # x = (image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + # + # img.save('meme-' + img.filename.split(os.sep)[-1]) + # img.show() diff --git a/format2.py b/format2.py new file mode 100644 index 0000000..81cdd29 --- /dev/null +++ b/format2.py @@ -0,0 +1,90 @@ +from manipulation import * + +class Format2: + + '''Two images concatenated sideways and text on the top and bottom of the image.''' + + def __init__( + self, + image1_path, + image2_path, + top_text, + bottom_text, + font_path='impact/impact.ttf', + font_size=9, + ): + + self.image1_path = image1_path + self.image2_path = image2_path + self.top_text = top_text + self.bottom_text = bottom_text + self.font_path = font_path + self.font_size = font_size + + def generate(self): + img01 = Image.open(self.image1_path) + img02 = Image.open(self.image2_path) + + merge_image = image_join_along_length(img01, img02, (320, 360), (320, 360)) + top_text_image = text_on_top(self.top_text, merge_image) + final_img = text_in_bottom(self.bottom_text, top_text_image) + + + final_img.save('meme-{}{}.jpg'.format(os.path.basename(self.image1_path).split('.')[0], os.path.basename(self.image2_path).split('.')[0])) + add_logo(final_img) + return final_img + + # def generate(self): + # img01 = Image.open(self.image1_path) + # img02 = Image.open(self.image2_path) + # images = map(Image.open, [self.image1_path, self.image2_path]) + # size = (320, 360) + # img1 = img01.resize((320, 360), Image.ANTIALIAS) + # img2 = img02.resize((320, 360), Image.ANTIALIAS) + # img1.save('short1.jpg') + # img2.save('short2.jpg') + # images = map(Image.open, ['short1.jpg', 'short2.jpg']) + # + # # widths, heights = zip(*(i.size for i in images)) + # + # image_width = 640 # sum(widths) + # image_height = 360 # max(heights) + # img = Image.new('RGB', (image_width, image_height)) + # x_offset = 0 + # + # for im in images: + # img.paste(im, (x_offset, 0)) + # x_offset += im.size[0] + # + # draw = ImageDraw.Draw(img) + # font = ImageFont.truetype(font=self.font_path, + # size=int(image_height + # * self.font_size) // 100) + # self.top_text = self.top_text.upper() + # self.bottom_text = self.bottom_text.upper() + # (char_width, char_height) = font.getsize('A') + # chars_per_line = image_width // char_width + # top_lines = textwrap.wrap(self.top_text, width=chars_per_line) + # bottom_lines = textwrap.wrap(self.bottom_text, + # width=chars_per_line) + # y = 10 + # + # for line in top_lines: + # (line_width, line_height) = font.getsize(line) + # x = (image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + # + # y = image_height - char_height * len(bottom_lines) - 15 + # + # for line in bottom_lines: + # (line_width, line_height) = font.getsize(line) + # x = (image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + # + # # img.save("meme3.jpg") + # + # img.save('meme-{}{}.jpg'.format(os.path.basename(self.image1_path).split('.' + # )[0], os.path.basename(self.image2_path).split('.')[0])) + # img.show() diff --git a/format3.py b/format3.py new file mode 100644 index 0000000..116f892 --- /dev/null +++ b/format3.py @@ -0,0 +1,206 @@ +from manipulation import * + +class Format3: + """ + This format helps create memes of comparision as decsribed in the + format_details.md file. + """ + + def __init__( + self, + image1_path, + image2_path, + top_text, + bottom_text, + font_path='impact/impact.ttf', + font_size=9, + ): + self.image1_path = image1_path + self.image2_path = image2_path + self.top_text = top_text + self.bottom_text = bottom_text + self.font_path = font_path + self.font_size = font_size + + def generate(self): + img01 = Image.open(self.image1_path) + img02 = Image.open(self.image2_path) + + size = (320, 360) + if self.top_text.__len__() == 1 and self.bottom_text.__len__() == 1: + + merge_image = image_join_along_breadth(img01, img02, (320, 360), (320, 360)) + top_text_image = text_on_top(self.top_text[0], merge_image) + + final_img = text_in_bottom(self.bottom_text[0], top_text_image) + + elif self.top_text.__len__() == 2 and self.bottom_text.__len__() == 1: + + img1 = text_on_top(self.top_text[0], img01, size) + img2 = text_on_top(self.top_text[1], img02, size) + + final_img = image_join_along_breadth(img1, img2) + final_img = text_in_bottom(self.bottom_text[0], final_img) + + elif self.bottom_text.__len__() == 2 and self.top_text.__len__() == 1: + + img1 = text_in_bottom(self.bottom_text[0], img01, size) + img2 = text_in_bottom(self.bottom_text[1], img02, size) + + final_img = image_join_along_breadth(img1, img2) + final_img = text_on_top(self.top_text[0], final_img) + + elif self.top_text.__len__() == 2 and self.bottom_text.__len__() == 2: + + img1 = text_in_bottom(self.bottom_text[0], img01, size) + img1 = text_on_top(self.top_text[0], img1, size) + + img2 = text_in_bottom(self.bottom_text[1], img02, size) + img2 = text_on_top(self.top_text[1], img2, size) + + final_img = image_join_along_breadth(img1, img2) + + final_img.save('meme-{}{}.jpg'.format(os.path.basename(self.image1_path).split( + '.')[0], os.path.basename(self.image2_path).split('.')[0])) + add_logo(final_image) + return final_img + +format3type1 = """ +Type 1: _______________________ + | Long top text | + | | | + | | | + | Long bottom text | + |__________|__________| +""" +format3type2 = """ +Type 2: _______________________ + | Long top text | + | | | + | | | + | Text | Text | + |__________|__________| +""" + +format3type3 = """ +Type 3: _______________________ + | Text | Text | + | | | + | | | + | Long bottom text | + |_____________________| +""" + +format3type4 = """ +Type 4: _______________________ + | Text | Text | + | | | + | | | + | Text | Text | + |__________|__________| +""" + + # def generate(self): + # img01 = Image.open(self.image1_path) + # img02 = Image.open(self.image2_path) + # images = map(Image.open, [self.image1_path, self.image2_path]) + # + # size = (320, 360) + # img1 = img01.resize((320, 360), Image.ANTIALIAS) + # img2 = img02.resize((320, 360), Image.ANTIALIAS) + # img1.save('short1.jpg') + # img2.save('short2.jpg') + # images = map(Image.open, ['short1.jpg', 'short2.jpg']) + # + # individual_image_width = 320 + # image_width = 640 + # image_height = 360 + # img = Image.new('RGB', (image_width, image_height)) + # x_offset = 0 + # + # for im in images: + # img.paste(im, (x_offset, 0)) + # x_offset += im.size[0] + # + # draw = ImageDraw.Draw(img) + # font = ImageFont.truetype(font=self.font_path, + # size=int(image_height + # * self.font_size) // 100) + # + # self.text_individual1 = self.text_individual1.upper() + # self.text_individual2 = self.text_individual2.upper() + # + # (char_width, char_height) = font.getsize('A') + # individual_char_per_line = individual_image_width // char_width + # char_per_line = image_width // char_width + # + # individual1_lines = textwrap.wrap(self.text_individual1, width=individual_char_per_line) + # individual2_lines = textwrap.wrap(self.text_individual2, width=individual_char_per_line) + # + # + # if self.bottom_text != None: + # self.bottom_text = self.bottom_text.upper() + # bottom_lines = textwrap.wrap(self.bottom_text, width=char_per_line) + # + # y = image_height - char_height * len(bottom_lines) - 15 + # + # for line in bottom_lines: + # (line_width, line_height) = font.getsize(line) + # x = (image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + # + # if self.text_individual1 != None and self.text_individual2 != None: + # y = 10 + # + # for line in individual1_lines: + # (line_width, line_height) = font.getsize(line) + # x = (individual_image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + # + # y = 10 + # + # for line in individual2_lines: + # (line_width, line_height) = font.getsize(line) + # x = 320 + (individual_image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + # + # elif self.top_text != None: + # self.top_text = self.top_text.upper() + # top_lines = textwrap.wrap(self.top_text, width=char_per_line) + # + # y = 10 + # + # for line in top_lines: + # (line_width, line_height) = font.getsize(line) + # x = (image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + # + # if self.text_individual1 != None and self.text_individual2 != None: + # y = image_height - char_height * len(individual1_lines) - 15 + # + # for line in individual1_lines: + # (line_width, line_height) = font.getsize(line) + # x = (individual_image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + # + # y = image_height - char_height * len(individual2_lines) - 15 + # + # for line in individual2_lines: + # (line_width, line_height) = font.getsize(line) + # x = 320 + (individual_image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + + # img.show() + +# ob = Format5(image1_path="data/got_memes/images/got01.jpg", +# image2_path="data/got_memes/images/got02.jpg", +# text_individual1="This is a text for image one", text_individual2="This is a text for thie image two", +# top_text="Hello this it's the top line", +# bottom_text="this a bottom line text wolla") +# ob.generate() diff --git a/formats/__init__.py b/formats/__init__.py index 8b13789..d3f5a12 100644 --- a/formats/__init__.py +++ b/formats/__init__.py @@ -1 +1 @@ - + diff --git a/formats/__pycache__/__init__.cpython-37.pyc b/formats/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..b7dd1df Binary files /dev/null and b/formats/__pycache__/__init__.cpython-37.pyc differ diff --git a/formats/__pycache__/format1.cpython-37.pyc b/formats/__pycache__/format1.cpython-37.pyc new file mode 100644 index 0000000..ccbfed1 Binary files /dev/null and b/formats/__pycache__/format1.cpython-37.pyc differ diff --git a/formats/__pycache__/format2.cpython-37.pyc b/formats/__pycache__/format2.cpython-37.pyc new file mode 100644 index 0000000..51b653d Binary files /dev/null and b/formats/__pycache__/format2.cpython-37.pyc differ diff --git a/formats/__pycache__/format3.cpython-37.pyc b/formats/__pycache__/format3.cpython-37.pyc new file mode 100644 index 0000000..2cb0993 Binary files /dev/null and b/formats/__pycache__/format3.cpython-37.pyc differ diff --git a/formats/format1.py b/formats/format1.py index 418836e..493a64a 100644 --- a/formats/format1.py +++ b/formats/format1.py @@ -41,6 +41,7 @@ def generate(self): #image.save('meme-' + img.filename.split(os.sep)[-1]) path, imagename = os.path.split(img.filename) image.save(os.path.join(path,'meme-' + imagename)) + add_logo(image) return image format1type1 = """ diff --git a/formats/format2.py b/formats/format2.py index 89c799e..81cdd29 100644 --- a/formats/format2.py +++ b/formats/format2.py @@ -31,6 +31,7 @@ def generate(self): final_img.save('meme-{}{}.jpg'.format(os.path.basename(self.image1_path).split('.')[0], os.path.basename(self.image2_path).split('.')[0])) + add_logo(final_img) return final_img # def generate(self): diff --git a/formats/format3.py b/formats/format3.py index c3e3175..116f892 100644 --- a/formats/format3.py +++ b/formats/format3.py @@ -1,205 +1,206 @@ -from manipulation import * - -class Format3: - """ - This format helps create memes of comparision as decsribed in the - format_details.md file. - """ - - def __init__( - self, - image1_path, - image2_path, - top_text, - bottom_text, - font_path='impact/impact.ttf', - font_size=9, - ): - self.image1_path = image1_path - self.image2_path = image2_path - self.top_text = top_text - self.bottom_text = bottom_text - self.font_path = font_path - self.font_size = font_size - - def generate(self): - img01 = Image.open(self.image1_path) - img02 = Image.open(self.image2_path) - - size = (320, 360) - if self.top_text.__len__() == 1 and self.bottom_text.__len__() == 1: - - merge_image = image_join_along_breadth(img01, img02, (320, 360), (320, 360)) - top_text_image = text_on_top(self.top_text[0], merge_image) - - final_img = text_in_bottom(self.bottom_text[0], top_text_image) - - elif self.top_text.__len__() == 2 and self.bottom_text.__len__() == 1: - - img1 = text_on_top(self.top_text[0], img01, size) - img2 = text_on_top(self.top_text[1], img02, size) - - final_img = image_join_along_breadth(img1, img2) - final_img = text_in_bottom(self.bottom_text[0], final_img) - - elif self.bottom_text.__len__() == 2 and self.top_text.__len__() == 1: - - img1 = text_in_bottom(self.bottom_text[0], img01, size) - img2 = text_in_bottom(self.bottom_text[1], img02, size) - - final_img = image_join_along_breadth(img1, img2) - final_img = text_on_top(self.top_text[0], final_img) - - elif self.top_text.__len__() == 2 and self.bottom_text.__len__() == 2: - - img1 = text_in_bottom(self.bottom_text[0], img01, size) - img1 = text_on_top(self.top_text[0], img1, size) - - img2 = text_in_bottom(self.bottom_text[1], img02, size) - img2 = text_on_top(self.top_text[1], img2, size) - - final_img = image_join_along_breadth(img1, img2) - - final_img.save('meme-{}{}.jpg'.format(os.path.basename(self.image1_path).split( - '.')[0], os.path.basename(self.image2_path).split('.')[0])) - return final_img - -format3type1 = """ -Type 1: _______________________ - | Long top text | - | | | - | | | - | Long bottom text | - |__________|__________| -""" -format3type2 = """ -Type 2: _______________________ - | Long top text | - | | | - | | | - | Text | Text | - |__________|__________| -""" - -format3type3 = """ -Type 3: _______________________ - | Text | Text | - | | | - | | | - | Long bottom text | - |_____________________| -""" - -format3type4 = """ -Type 4: _______________________ - | Text | Text | - | | | - | | | - | Text | Text | - |__________|__________| -""" - - # def generate(self): - # img01 = Image.open(self.image1_path) - # img02 = Image.open(self.image2_path) - # images = map(Image.open, [self.image1_path, self.image2_path]) - # - # size = (320, 360) - # img1 = img01.resize((320, 360), Image.ANTIALIAS) - # img2 = img02.resize((320, 360), Image.ANTIALIAS) - # img1.save('short1.jpg') - # img2.save('short2.jpg') - # images = map(Image.open, ['short1.jpg', 'short2.jpg']) - # - # individual_image_width = 320 - # image_width = 640 - # image_height = 360 - # img = Image.new('RGB', (image_width, image_height)) - # x_offset = 0 - # - # for im in images: - # img.paste(im, (x_offset, 0)) - # x_offset += im.size[0] - # - # draw = ImageDraw.Draw(img) - # font = ImageFont.truetype(font=self.font_path, - # size=int(image_height - # * self.font_size) // 100) - # - # self.text_individual1 = self.text_individual1.upper() - # self.text_individual2 = self.text_individual2.upper() - # - # (char_width, char_height) = font.getsize('A') - # individual_char_per_line = individual_image_width // char_width - # char_per_line = image_width // char_width - # - # individual1_lines = textwrap.wrap(self.text_individual1, width=individual_char_per_line) - # individual2_lines = textwrap.wrap(self.text_individual2, width=individual_char_per_line) - # - # - # if self.bottom_text != None: - # self.bottom_text = self.bottom_text.upper() - # bottom_lines = textwrap.wrap(self.bottom_text, width=char_per_line) - # - # y = image_height - char_height * len(bottom_lines) - 15 - # - # for line in bottom_lines: - # (line_width, line_height) = font.getsize(line) - # x = (image_width - line_width) / 2 - # draw.text((x, y), line, fill='white', font=font) - # y += line_height - # - # if self.text_individual1 != None and self.text_individual2 != None: - # y = 10 - # - # for line in individual1_lines: - # (line_width, line_height) = font.getsize(line) - # x = (individual_image_width - line_width) / 2 - # draw.text((x, y), line, fill='white', font=font) - # y += line_height - # - # y = 10 - # - # for line in individual2_lines: - # (line_width, line_height) = font.getsize(line) - # x = 320 + (individual_image_width - line_width) / 2 - # draw.text((x, y), line, fill='white', font=font) - # y += line_height - # - # elif self.top_text != None: - # self.top_text = self.top_text.upper() - # top_lines = textwrap.wrap(self.top_text, width=char_per_line) - # - # y = 10 - # - # for line in top_lines: - # (line_width, line_height) = font.getsize(line) - # x = (image_width - line_width) / 2 - # draw.text((x, y), line, fill='white', font=font) - # y += line_height - # - # if self.text_individual1 != None and self.text_individual2 != None: - # y = image_height - char_height * len(individual1_lines) - 15 - # - # for line in individual1_lines: - # (line_width, line_height) = font.getsize(line) - # x = (individual_image_width - line_width) / 2 - # draw.text((x, y), line, fill='white', font=font) - # y += line_height - # - # y = image_height - char_height * len(individual2_lines) - 15 - # - # for line in individual2_lines: - # (line_width, line_height) = font.getsize(line) - # x = 320 + (individual_image_width - line_width) / 2 - # draw.text((x, y), line, fill='white', font=font) - # y += line_height - - # img.show() - -# ob = Format5(image1_path="data/got_memes/images/got01.jpg", -# image2_path="data/got_memes/images/got02.jpg", -# text_individual1="This is a text for image one", text_individual2="This is a text for thie image two", -# top_text="Hello this it's the top line", -# bottom_text="this a bottom line text wolla") -# ob.generate() +from manipulation import * + +class Format3: + """ + This format helps create memes of comparision as decsribed in the + format_details.md file. + """ + + def __init__( + self, + image1_path, + image2_path, + top_text, + bottom_text, + font_path='impact/impact.ttf', + font_size=9, + ): + self.image1_path = image1_path + self.image2_path = image2_path + self.top_text = top_text + self.bottom_text = bottom_text + self.font_path = font_path + self.font_size = font_size + + def generate(self): + img01 = Image.open(self.image1_path) + img02 = Image.open(self.image2_path) + + size = (320, 360) + if self.top_text.__len__() == 1 and self.bottom_text.__len__() == 1: + + merge_image = image_join_along_breadth(img01, img02, (320, 360), (320, 360)) + top_text_image = text_on_top(self.top_text[0], merge_image) + + final_img = text_in_bottom(self.bottom_text[0], top_text_image) + + elif self.top_text.__len__() == 2 and self.bottom_text.__len__() == 1: + + img1 = text_on_top(self.top_text[0], img01, size) + img2 = text_on_top(self.top_text[1], img02, size) + + final_img = image_join_along_breadth(img1, img2) + final_img = text_in_bottom(self.bottom_text[0], final_img) + + elif self.bottom_text.__len__() == 2 and self.top_text.__len__() == 1: + + img1 = text_in_bottom(self.bottom_text[0], img01, size) + img2 = text_in_bottom(self.bottom_text[1], img02, size) + + final_img = image_join_along_breadth(img1, img2) + final_img = text_on_top(self.top_text[0], final_img) + + elif self.top_text.__len__() == 2 and self.bottom_text.__len__() == 2: + + img1 = text_in_bottom(self.bottom_text[0], img01, size) + img1 = text_on_top(self.top_text[0], img1, size) + + img2 = text_in_bottom(self.bottom_text[1], img02, size) + img2 = text_on_top(self.top_text[1], img2, size) + + final_img = image_join_along_breadth(img1, img2) + + final_img.save('meme-{}{}.jpg'.format(os.path.basename(self.image1_path).split( + '.')[0], os.path.basename(self.image2_path).split('.')[0])) + add_logo(final_image) + return final_img + +format3type1 = """ +Type 1: _______________________ + | Long top text | + | | | + | | | + | Long bottom text | + |__________|__________| +""" +format3type2 = """ +Type 2: _______________________ + | Long top text | + | | | + | | | + | Text | Text | + |__________|__________| +""" + +format3type3 = """ +Type 3: _______________________ + | Text | Text | + | | | + | | | + | Long bottom text | + |_____________________| +""" + +format3type4 = """ +Type 4: _______________________ + | Text | Text | + | | | + | | | + | Text | Text | + |__________|__________| +""" + + # def generate(self): + # img01 = Image.open(self.image1_path) + # img02 = Image.open(self.image2_path) + # images = map(Image.open, [self.image1_path, self.image2_path]) + # + # size = (320, 360) + # img1 = img01.resize((320, 360), Image.ANTIALIAS) + # img2 = img02.resize((320, 360), Image.ANTIALIAS) + # img1.save('short1.jpg') + # img2.save('short2.jpg') + # images = map(Image.open, ['short1.jpg', 'short2.jpg']) + # + # individual_image_width = 320 + # image_width = 640 + # image_height = 360 + # img = Image.new('RGB', (image_width, image_height)) + # x_offset = 0 + # + # for im in images: + # img.paste(im, (x_offset, 0)) + # x_offset += im.size[0] + # + # draw = ImageDraw.Draw(img) + # font = ImageFont.truetype(font=self.font_path, + # size=int(image_height + # * self.font_size) // 100) + # + # self.text_individual1 = self.text_individual1.upper() + # self.text_individual2 = self.text_individual2.upper() + # + # (char_width, char_height) = font.getsize('A') + # individual_char_per_line = individual_image_width // char_width + # char_per_line = image_width // char_width + # + # individual1_lines = textwrap.wrap(self.text_individual1, width=individual_char_per_line) + # individual2_lines = textwrap.wrap(self.text_individual2, width=individual_char_per_line) + # + # + # if self.bottom_text != None: + # self.bottom_text = self.bottom_text.upper() + # bottom_lines = textwrap.wrap(self.bottom_text, width=char_per_line) + # + # y = image_height - char_height * len(bottom_lines) - 15 + # + # for line in bottom_lines: + # (line_width, line_height) = font.getsize(line) + # x = (image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + # + # if self.text_individual1 != None and self.text_individual2 != None: + # y = 10 + # + # for line in individual1_lines: + # (line_width, line_height) = font.getsize(line) + # x = (individual_image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + # + # y = 10 + # + # for line in individual2_lines: + # (line_width, line_height) = font.getsize(line) + # x = 320 + (individual_image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + # + # elif self.top_text != None: + # self.top_text = self.top_text.upper() + # top_lines = textwrap.wrap(self.top_text, width=char_per_line) + # + # y = 10 + # + # for line in top_lines: + # (line_width, line_height) = font.getsize(line) + # x = (image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + # + # if self.text_individual1 != None and self.text_individual2 != None: + # y = image_height - char_height * len(individual1_lines) - 15 + # + # for line in individual1_lines: + # (line_width, line_height) = font.getsize(line) + # x = (individual_image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + # + # y = image_height - char_height * len(individual2_lines) - 15 + # + # for line in individual2_lines: + # (line_width, line_height) = font.getsize(line) + # x = 320 + (individual_image_width - line_width) / 2 + # draw.text((x, y), line, fill='white', font=font) + # y += line_height + + # img.show() + +# ob = Format5(image1_path="data/got_memes/images/got01.jpg", +# image2_path="data/got_memes/images/got02.jpg", +# text_individual1="This is a text for image one", text_individual2="This is a text for thie image two", +# top_text="Hello this it's the top line", +# bottom_text="this a bottom line text wolla") +# ob.generate() diff --git a/manipulation.py b/manipulation.py index 2afcb90..aa9dc80 100644 --- a/manipulation.py +++ b/manipulation.py @@ -1,117 +1,126 @@ -from PIL import Image, ImageFont, ImageDraw -import textwrap -import os - -font_path = "impact/impact.ttf" -font_size = 9 - -def text_on_top (text, image, resize=(None,None)): - ''' - Input PIL Image object - Puts text on the top of the image w/r/t the image height and width - Returns the PIL Image object, its height and width - ''' - if resize != (None, None): - image = image.resize(resize, Image.ANTIALIAS) - - draw = ImageDraw.Draw(image) - (image_width, image_height) = image.size - font = ImageFont.truetype(font=font_path, - size=int(image_height - * font_size) // 100) - text = text.upper() - (char_width, char_height) = font.getsize('A') - chars_per_line = image_width // char_width - top_lines = textwrap.wrap(text, width=chars_per_line) - y = 10 - - for line in top_lines: - (line_width, line_height) = font.getsize(line) - x = (image_width - line_width) / 2 - draw.text((x, y), line, fill='white', font=font) - y += line_height - - return image - -def text_in_bottom (text, image, resize=(None, None)): - ''' - Input PIL image object - Puts Text in the bottom of the image w/r/t the image height and width - Returns the PIL Image object, its height and width - ''' - if resize != (None, None): - image = image.resize(resize, Image.ANTIALIAS) - - draw = ImageDraw.Draw(image) - (image_width, image_height) = image.size - font = ImageFont.truetype(font=font_path, - size=int(image_height - * font_size) // 100) - text = text.upper() - (char_width, char_height) = font.getsize('A') - chars_per_line = image_width // char_width - bottom_lines = textwrap.wrap(text, - width=chars_per_line) - y = image_height - char_height * len(bottom_lines) - 15 - - for line in bottom_lines: - (line_width, line_height) = font.getsize(line) - x = (image_width - line_width) / 2 - draw.text((x, y), line, fill='white', font=font) - y += line_height - - return image - -def image_join_along_breadth(image1, image2, size1=(None, None), size2=(None, None)): - ''' - Concatenates two images side by side - Input PIL Image obejct - Returns the PIL Image object, its height and width - ''' - - if size1 != (None, None): - image1 = image1.resize(size1, Image.ANTIALIAS) - if size2 != (None, None): - image2 = image2.resize(size2, Image.ANTIALIAS) - - image1.save('short1.jpg') - image2.save('short2.jpg') - images = map(Image.open, ['short1.jpg', 'short2.jpg']) - - image_width = image1.size[0] + image2.size[0] - image_height = image1.size[1] - image = Image.new('RGB', (image_width, image_height)) - x_offset = 0 - - for im in images: - image.paste(im, (x_offset, 0)) - x_offset += im.size[0] - - return image - -def image_join_along_length(image1, image2, size1=(None, None), size2=(None, None)): - ''' - Input PIL Image obejct - Concatenates images in a top to bottom fashion - Returns PIL Image object, its height and width - ''' - - if size1 != (None, None): - image1 = image1.resize(size1, Image.ANTIALIAS) - if size2 != (None, None): - image2 = image2.resize(size2, Image.ANTIALIAS) - - image1.save('short1.jpg') - image2.save('short2.jpg') - images = map(Image.open, ['short1.jpg', 'short2.jpg']) - - image_width = image1.size[0] - image_height = image1.size[1] + image2.size[1] - image = Image.new('RGB', (image_width, image_height)) - y_offset = 0 - - for im in images: - image.paste(im, (0, y_offset)) - y_offset += im.size[1] - - return image +from PIL import Image, ImageFont, ImageDraw +import textwrap +import os + +font_path = "impact/impact.ttf" +font_size = 9 + +def text_on_top (text, image, resize=(None,None)): + ''' + Input PIL Image object + Puts text on the top of the image w/r/t the image height and width + Returns the PIL Image object, its height and width + ''' + if resize != (None, None): + image = image.resize(resize, Image.ANTIALIAS) + + draw = ImageDraw.Draw(image) + (image_width, image_height) = image.size + font = ImageFont.truetype(font=font_path, + size=int(image_height + * font_size) // 100) + text = text.upper() + (char_width, char_height) = font.getsize('A') + chars_per_line = image_width // char_width + top_lines = textwrap.wrap(text, width=chars_per_line) + y = 10 + + for line in top_lines: + (line_width, line_height) = font.getsize(line) + x = (image_width - line_width) / 2 + draw.text((x, y), line, fill='white', font=font) + y += line_height + + return image + +def text_in_bottom (text, image, resize=(None, None)): + ''' + Input PIL image object + Puts Text in the bottom of the image w/r/t the image height and width + Returns the PIL Image object, its height and width + ''' + if resize != (None, None): + image = image.resize(resize, Image.ANTIALIAS) + + draw = ImageDraw.Draw(image) + (image_width, image_height) = image.size + font = ImageFont.truetype(font=font_path, + size=int(image_height + * font_size) // 100) + text = text.upper() + (char_width, char_height) = font.getsize('A') + chars_per_line = image_width // char_width + bottom_lines = textwrap.wrap(text, + width=chars_per_line) + y = image_height - char_height * len(bottom_lines) - 15 + + for line in bottom_lines: + (line_width, line_height) = font.getsize(line) + x = (image_width - line_width) / 2 + draw.text((x, y), line, fill='white', font=font) + y += line_height + + return image + +def image_join_along_breadth(image1, image2, size1=(None, None), size2=(None, None)): + ''' + Concatenates two images side by side + Input PIL Image obejct + Returns the PIL Image object, its height and width + ''' + + if size1 != (None, None): + image1 = image1.resize(size1, Image.ANTIALIAS) + if size2 != (None, None): + image2 = image2.resize(size2, Image.ANTIALIAS) + + image1.save('short1.jpg') + image2.save('short2.jpg') + images = map(Image.open, ['short1.jpg', 'short2.jpg']) + + image_width = image1.size[0] + image2.size[0] + image_height = image1.size[1] + image = Image.new('RGB', (image_width, image_height)) + x_offset = 0 + + for im in images: + image.paste(im, (x_offset, 0)) + x_offset += im.size[0] + + return image + +def image_join_along_length(image1, image2, size1=(None, None), size2=(None, None)): + ''' + Input PIL Image obejct + Concatenates images in a top to bottom fashion + Returns PIL Image object, its height and width + ''' + + if size1 != (None, None): + image1 = image1.resize(size1, Image.ANTIALIAS) + if size2 != (None, None): + image2 = image2.resize(size2, Image.ANTIALIAS) + + image1.save('short1.jpg') + image2.save('short2.jpg') + images = map(Image.open, ['short1.jpg', 'short2.jpg']) + + image_width = image1.size[0] + image_height = image1.size[1] + image2.size[1] + image = Image.new('RGB', (image_width, image_height)) + y_offset = 0 + + for im in images: + image.paste(im, (0, y_offset)) + y_offset += im.size[1] + + return image + +def add_logo(image): + logo = Image.open('opengenus_logo.png') + nimg = logo.resize((90,30)) + n=image.size + k=int(n[0] - 90) + g=int(n[1] - 35) + image.paste(nimg, ((k),(g))) + return image diff --git a/opengenus_logo.png b/opengenus_logo.png new file mode 100644 index 0000000..e06b83c Binary files /dev/null and b/opengenus_logo.png differ