繁体   English   中英

结合图像轮廓和另一幅图​​像

[英]Combine image outlines with another image

假设我有此图像作为numpy数组。 所有白色像素均为1,黑色为零。 我也有这个图像作为numpy数组。 我想从没有白色背景的第一张图片中提取snqke,然后使用python将其粘贴到森林图片上。 这里有人知道怎么做吗?

我认为使蛇变红会更具有启发性和乐趣,因为您拥有的东西一定可以随心所欲地四处闲逛。

#!/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')

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM