【Python 常用脚本及命令系列 12.1 -- OpenCV 设置图片区域为某个颜色】

文章目录

cv2 设置图片区域颜色

你可以使用numpy的切片操作来选择图像的一部分,并使用赋值操作来设置颜色。以下是一个简单的Python脚本示例:

python 复制代码
import cv2 
import numpy as np 

# 读取图像 
img = cv2.imread('input.jpg') 

# 设置图像的一部分颜色,例如设置左上角100x100区域为红色 
img[0:100, 0:100] = [0, 0, 255] 

# 保存修改后的图像 
cv2.imwrite('modified.jpg', img)

在这个脚本中,我们首先读取了一张名为 'input.jpg' 的图像,然后使用 numpy 的切片操作选取了图像左上角的 100x100 像素区域,然后使用赋值操作将这一部分的颜色设置为红色(在OpenCV中,颜色的表示顺序为BGR,所以红色表示为[0, 0, 255])。最后,我们将修改后的图像保存为'modified.jpg'。

请注意 ,numpy的切片操作中,第一个索引是y坐标(垂直方向),第二个索引是x坐标(水平方向)。所以img[0:100, 0:100]选取的是图像左上角的区域。如果你想选取其他区域,可以调整切片操作的参数。例如,img[50:150, 50:150]会选取图像从(50, 50)开始的100x100像素区域。

以设置图片右下角100x100 区域为白色为例

要将图像的右下角 100x100 的区域设置为白色,你需要知道图像的宽度和高度,以便从正确的位置开始切片。

以下是一个Python脚本示例:

python 复制代码
import cv2 
import numpy as np 

# 读取图像 
img = cv2.imread('input.jpg') 

# 获取图像的高度和宽度 
height, width = img.shape[:2] 

# 设置图像右下角的100x100区域为白色 
img[height-100:height, width-100:width] = [255, 255, 255] 

# 保存修改后的图像 
cv2.imwrite('modified.jpg', img)
  • 我们首先读取了一张名为'input.jpg'的图像,然后获取了图像的高度和宽度。
  • 然后,我们使用numpy的切片操作选取了图像右下角的100x100像素区域,然后使用赋值操作将这一部分的颜色设置为白色(在OpenCV中,颜色的表示顺序为BGR,所以白色表示为[255, 255, 255])。
  • 最后,我们将修改后的图像保存为'modified.jpg'。

请注意,如果图像的高度或宽度小于100 像素,你需要相应地调整切片操作的参数,否则会出现索引错误。

动态输入高和宽

python 复制代码
import sys
import cv2 # 导入包
import numpy as np
from PIL import Image

num_args = len(sys.argv)
print("The input length is:", num_args)
if num_args != 3:
    print("Plese input the height and width")
    exit(1)

print("type:", type(sys.argv))
print("function name:", sys.argv[0])

try:
    print("hight:", sys.argv[1])
    print("width:", sys.argv[2])
except Exception as e:
    print("Input Error:", e)

img = cv2.imread(r'test.jpg')#
height, width = img.shape[:2]
img[height - int(sys.argv[1]) : height, width - int(sys.argv[2]) : width] = [255, 255, 255]
cv2.imwrite('modified.jpg', img)

#read image
img_grey = cv2.imread('modified.jpg', cv2.IMREAD_GRAYSCALE)


# define a threshold, 128 is the middle of black and white in grey scale
#thresh = 128
thresh = 210

# assign blue channel to zeros
img_binary = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY)[1]


# 在窗口中显示图像
cv2.imshow(r'Image', img_binary)

# 最后还要写一句代码,这样就可以使窗口始终保持住
cv2.waitKey(0)

#最后别忘了释放窗口,养成良好习惯。
cv2.destroyAllWindows()

#save image
cv2.imwrite('black-and-white.png',img_binary)
相关推荐
前端付豪3 小时前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽4 小时前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战4 小时前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋10 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者1 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者1 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅1 天前
Python函数入门详解(定义+调用+参数)
python
曲幽1 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时1 天前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构