当前位置: 首页 > news >正文

Python-将多张图片合并成一张图片调整指定区域的颜色选框工具

在 Python 中,可以使用 Pillow(PIL)库将多张图片拼接(合并)成一张大图片。

下面以将多张图片水平或者垂直拼接为例,给出代码示例:

from PIL import Image
# 图片列表
image_files = ['img1.jpg', 'img2.jpg', 'img3.jpg']
# 打开图片,并获取尺寸
images = [Image.open(img) for img in image_files]
widths, heights = zip(*(img.size for img in images))
# 拼接方向:水平(axis=0)或垂直(axis=1)
axis = 0  # 0表示水平拼接,1表示垂直拼接
if axis == 0:  # 水平拼接total_width = sum(widths)max_height = max(heights)new_im = Image.new('RGB', (total_width, max_height))x_offset = 0for img in images:new_im.paste(img, (x_offset, 0))x_offset += img.width
else:  # 垂直拼接max_width = max(widths)total_height = sum(heights)new_im = Image.new('RGB', (max_width, total_height))y_offset = 0for img in images:new_im.paste(img, (0, y_offset))y_offset += img.height# 保存合并后的图片
new_im.save('merged.jpg')

可以使用 Pillow 库结合像素处理,来调整图片指定区域的色号。通常包括如下几个步骤:
1、打开图片
2、确定需要调整的区域(可以用矩形框 [left, upper, right, lower] 表示)
3、遍历区域内的像素,并按照你的需求改变色号(颜色值)
4、保存修改后的图片

示例一:将指定区域变为特定颜色

from PIL import Image
# 读取图片
img = Image.open('input.jpg')
# 指定区域 (左, 上, 右, 下)
box = (50, 50, 150, 150)
# 新建一个纯色区域
color = (255, 0, 0)  # RGB红色
region = Image.new('RGB', (box[2] - box[0], box[3] - box[1]), color)
# 粘贴到原图指定区域
img.paste(region, box)
img.save('output.jpg')

示例二:调整指定区域的每个像素色号(如变亮、变暗、调整色调)

from PIL import Imageimg = Image.open('input.jpg').convert('RGB')
pixels = img.load()  # 像素访问对象box = (50, 50, 150, 150)
for x in range(box[0], box[2]):for y in range(box[1], box[3]):r, g, b = pixels[x, y]# 举例:全都变亮50(不超过255)r = min(r + 50, 255)g = min(g + 50, 255)b = min(b + 50, 255)pixels[x, y] = (r, g, b)
img.save('brighter.jpg')

示例三:替换某个色号为另一个色号

from PIL import Image
from itertools import productimg = Image.open('input.jpg').convert('RGB')
pixels = img.load()
box = (50, 50, 150, 150)
_r = range(88, 176)
_g = range(90, 175)
_b = range(98, 182)
old_colors = [tuple(x) for x in product(_r, _g, _b)]  # 三个数组 递归组成一个新数组
new_color = (255, 0, 0)
for x in range(box[0], box[2]):for y in range(box[1], box[3]):if pixels[x, y] == old_color:pixels[x, y] = new_color
img.save('replace_color.jpg')

Tkinter 实现矩形和椭圆选框
主要思路
1、用 Canvas 显示图片;
2、监听鼠标按下、拖动、松开事件,实时绘制选框;
3、支持选择“矩形”或“椭圆”模式。

import tkinter as tk
from PIL import Image, ImageTkclass SelectBoxApp:def __init__(self, root, image_path):self.root = rootself.image = Image.open(image_path)self.tkimg = ImageTk.PhotoImage(self.image)self.mode = tk.StringVar(value='rect')  # 'rect' or 'ellipse'tk.Radiobutton(root, text='矩形', variable=self.mode, value='rect').pack(side='left')tk.Radiobutton(root, text='椭圆', variable=self.mode, value='ellipse').pack(side='left')self.canvas = tk.Canvas(root, width=self.image.width, height=self.image.height)self.canvas.pack()self.canvas.create_image(0, 0, anchor='nw', image=self.tkimg)self.start_x = self.start_y = 0self.rect = self.ellipse = Noneself.canvas.bind('<Button-1>', self.on_mouse_down)self.canvas.bind('<B1-Motion>', self.on_mouse_drag)self.canvas.bind('<ButtonRelease-1>', self.on_mouse_up)def on_mouse_down(self, event):self.start_x, self.start_y = event.x, event.y# 清除旧框if self.rect: self.canvas.delete(self.rect)if self.ellipse: self.canvas.delete(self.ellipse)self.rect = self.ellipse = Nonedef on_mouse_drag(self, event):# 动态画矩形或椭圆if self.rect: self.canvas.delete(self.rect)if self.ellipse: self.canvas.delete(self.ellipse)x1, y1 = self.start_x, self.start_yx2, y2 = event.x, event.yif self.mode.get() == 'rect':self.rect = self.canvas.create_rectangle(x1, y1, x2, y2, outline='red', width=2)else:self.ellipse = self.canvas.create_oval(x1, y1, x2, y2, outline='blue', width=2)def on_mouse_up(self, event):# 最终区域可以在这里获取x1, y1 = self.start_x, self.start_yx2, y2 = event.x, event.yx1, x2 = sorted([x1, x2])y1, y2 = sorted([y1, y2])print(f'选区: ({x1}, {y1}, {x2}, {y2}), 形状: {self.mode.get()}')if __name__ == '__main__':root = tk.Tk()app = SelectBoxApp(root, 'your_image.jpg')  # 换成你的图片路径root.mainloop()
http://www.lryc.cn/news/580073.html

相关文章:

  • 应急响应靶场——web3 ——知攻善防实验室
  • 【Unity开发】Unity实现glb模型上传到场景中使用功能
  • 秘塔AI搜索的通过Prompt生成互动式网页探索(二)
  • python脚本编程:使用BeautifulSoup爬虫库获取热门单机游戏排行榜
  • Android发展历程
  • 面试版-前端开发核心知识
  • Oracle如何使用序列 Oracle序列使用教程
  • Java 大视界 -- Java 大数据实战:智能安防入侵检测的特征工程与模型融合全解析
  • 硬件嵌入式学习路线大总结(一):C语言与linux。内功心法——从入门到精通,彻底打通你的任督二脉!
  • Java教程——线程池和future
  • Spring Boot 应用启动时,端口 8080 已被其他进程占用,怎么办
  • 批量PDF转换工具,一键转换Word Excel
  • Jenkins 介绍
  • 后端密码加密:守护用户数据的钢铁长城
  • [尚庭公寓]06-Redis快速入门
  • 通过 Ansys Discovery CFD 仿真探索电池冷板概念
  • Excel 如何进行多条件查找或求和?
  • WPF 右键菜单 MenuItem 绑定图片时只显示最后一个 Icon
  • 深度分析:Microsoft .NET Framework System.Random 的 C++ 复刻实现
  • c# 使用GADL: Can‘t load requested DLL错误处理
  • PixiJS教程(004):点击事件交互
  • gic 中断触发类型
  • Python 中线程和进程在实际项目使用中的区别和联系
  • FastAPI 小白教程:从入门级到实战(源码教程)
  • 基于Docker构建OrangePi5 SDK环境
  • 使用mindie:2.0.RC2-800I-A2-py311-openeuler24.03-lts制作一个通用的模型推理性能测试的镜像
  • Windows 10/11 PC平台关闭禁用系统自动上传相关隐私数据手册
  • TDengine STMT2 API 使用指南
  • HarmonyOS-ArkUI 手势系列4--多层级手势
  • Spring Boot 中常用的工具类库及其使用示例(完整版)