假设我有这个image作为numpy数组。所有白色像素均为1,黑色为零。我也有这个image作为numpy数组。我想从没有白色背景的第一张图片中提取snqke,然后使用python将其粘贴到森林图片上。这里有人知道怎么做吗?
答案 0 :(得分:0)
我认为使蛇变红会更具启发性和乐趣,因为您显然可以随心所欲地四处闲逛。
#!/usr/bin/env python3
from PIL import Image
import numpy as np
snake = Image.open('snake.jpg').convert('L')
forest = Image.open('forest.jpg').convert('RGB')
# Make sensible sizes
snakedims = (100,150)
snake = snake.resize(snakedims)
forest = forest.resize((600,800))
# Make into numpy arrays
snakenp = np.array(snake)
forestnp = np.array(forest)
# Top-left corner of where we want snake to appear
topleft = 300,500
# Create an ROI - Region of Interest - same size as snake
roi = forestnp[topleft[1]:topleft[1]+snakedims[1], topleft[0]:topleft[0]+snakedims[0], :]
# Everywhere in the ROI where the snake is less than 128, put red (255,0,0)
roi[snakenp<128] = 255,0,0
# Save result to disk
Image.fromarray(forestnp).save('result.png')