Skip to content

Instantly share code, notes, and snippets.

@universax
Created February 10, 2019 11:19
Show Gist options
  • Save universax/d11377f4675fbc8f56a506e22696f5f1 to your computer and use it in GitHub Desktop.
Save universax/d11377f4675fbc8f56a506e22696f5f1 to your computer and use it in GitHub Desktop.
from PIL import Image, ImageDraw, ImageFilter
from pathlib import Path
import random
def getImage(path):
img = resizeImage(Image.open(path))
return img
def resizeImage(image):
return image.resize((640, 640))
def getAllFilePath(dirPath):
p = Path(dirPath)
flist = list(p.glob('*.jpg'))
all_path = []
for f in flist:
all_path.append(str(f.resolve()))
return all_path
def getTargetImages(dirPath):
p = Path(dirPath)
flist = list(p.glob('*.png'))
imgs = []
for i, f in enumerate(flist):
# f.rename('squirrel_' + str(i))
img = Image.open(f)
imgs.append(img.resize((400, 400)))
return imgs
def run():
# get target obj img
objs = getTargetImages('./squirrel_base')
# list files
files = getAllFilePath('./challenge2018')
for i, fpath in enumerate(files):
if i > 1000:
return
bg_img = getImage(fpath)
# add alpha channel
bg_img.putalpha(255)
# choise target img random
obj = objs[random.randrange(0, len(objs) - 1)]
# paste obj to bg image
bg_img.paste(obj, (120, 120), obj)
# jpg_img = bg_img.convert('RGB')
bg_img.save('treat/img_' + str(i) + '.png')
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment