合并图像轮廓与另一张图像

2024-09-27 21:33:19 发布

您现在位置:Python中文网/ 问答频道 /正文

假设这个image是numpy数组。所有白色像素为1,黑色像素为0。我还有这个image作为numpy数组。我想从第一个没有白色背景的图像中提取snqke,并使用python将其粘贴到森林图像上。有人知道怎么做吗?你知道吗


Tags: 图像imagenumpy粘贴森林像素数组背景
1条回答
网友
1楼 · 发布于 2024-09-27 21:33:19

我觉得把蛇染成红色会更有教育意义,也会很有趣——很明显,你可以随心所欲地到处游荡,现在你有了一些有用的东西。你知道吗

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

enter image description here

相关问题 更多 >

    热门问题