Skip to content

Instantly share code, notes, and snippets.

@kubinka0505
Last active October 25, 2020 09:22
Show Gist options
  • Save kubinka0505/702d1c4d7523dabab60b6a4cb1cd8f1b to your computer and use it in GitHub Desktop.
Save kubinka0505/702d1c4d7523dabab60b6a4cb1cd8f1b to your computer and use it in GitHub Desktop.
from PIL import Image
from requests import get
BG = Image.new("RGBA", (325, 250), (255,) * 3)
FG = get("https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Circle_-_black_simple.svg/100px-Circle_-_black_simple.svg.png", stream = True).raw
FG = Image.open(FG).convert("RGBA")
class Paste_Image(object):
def __init__(self, Background: Image, Foreground: Image, Crop = True):
self.FG = Foreground.crop(Foreground.getbbox()) if Crop == True else Foreground
self.BG = Background
#---#
def on_top_left(self):
self.BG.paste(self.FG, self.FG)
return self.BG
#---#
def on_top_center(self):
self.BG.paste(self.FG, ((self.BG.size[0] - self.FG.size[0]) // 2, 0), self.FG)
return self.BG
#---#
def on_top_right(self):
self.BG.paste(self.FG, (self.BG.size[0] - self.FG.size[0], 0), self.FG)
return self.BG
#---#
def on_center_left(self):
self.BG.paste(self.FG, (0, (self.BG.size[1] - self.FG.size[1]) // 2), self.FG)
return self.BG
#---#
def on_center(self):
self.BG.paste(self.FG, ((self.BG.size[0] - self.FG.size[0]) // 2, (self.BG.size[1] - self.FG.size[1]) // 2), self.FG)
return self.BG
#---#
def on_center_right(self):
self.BG.paste(self.FG, (self.BG.size[0] - self.FG.size[0], (self.BG.size[1] - self.FG.size[1]) // 2), self.FG)
return self.BG
#---#
def on_bottom_left(self):
self.BG.paste(self.FG, (0, self.BG.size[1] - self.FG.size[1]), self.FG)
return self.BG
#---#
def on_bottom_center(self):
self.BG.paste(self.FG, ((self.BG.size[0] - self.FG.size[0]) // 2, self.BG.size[1] - self.FG.size[1]), self.FG)
return self.BG
#---#
def on_bottom_right(self):
self.BG.paste(self.FG, (self.BG.size[0] - self.FG.size[0], self.BG.size[1] - self.FG.size[1]), self.FG)
return self.BG
# Reference: https://puu.sh/GGpUx/69fbe8704d.png
# 1 - Paste_Image.on_top_left
# 2 - Paste_Image.on_top_center
# 3 - Paste_Image.on_top_right
# 4 - Paste_Image.on_center_left
# 5 - Paste_Image.on_center
# 6 - Paste_Image.on_center_right
# 7 - Paste_Image.on_bottom_left
# 8 - Paste_Image.on_bottom_center
# 9 - Paste_Image.on_bottom_right
# Example
FG = Paste_Image(BG, FG).on_top_left()
FG.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment