#!/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')
我觉得把蛇染成红色会更有教育意义,也会很有趣——很明显,你可以随心所欲地到处游荡,现在你有了一些有用的东西。你知道吗
相关问题 更多 >
编程相关推荐