Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 126 additions & 117 deletions manipulation.py
Original file line number Diff line number Diff line change
@@ -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()
Binary file added opengenus_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.