diff --git a/manipulation.py b/manipulation.py index 2afcb90..e2ca8d7 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(): + logo = Image.open('opengenus_logo.png') + nimg = logo.resize((90,30)) + n=img.size + k=int(n[0] - 90) + g=int(n[1] - 35) + img.paste(nimg, ((k),(g))) + img.show() 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