# ipa2 **Repository Path**: chewlll/ipa2 ## Basic Information - **Project Name**: ipa2 - **Description**: IPA项目02图像处理 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-03-15 - **Last Updated**: 2025-03-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 以下是为你整理的上述代码的 `README.md` 文件,主要介绍了代码的功能、依赖、使用方法等内容: # 图像处理代码库 README ## 一、简介 本代码库包含了一系列使用 Python 语言编写的图像处理脚本,利用 `numpy` 和 `PIL`(Python Imaging Library,通过 `Pillow` 库使用)等库实现了多种图像操作,如颜色变换、通道分离、添加条纹效果等。 ## 二、依赖环境 确保你已经安装了以下 Python 库: - `numpy`:用于数值计算和数组操作。 - `Pillow`:提供了广泛的文件格式支持以及强大的图像处理能力。 - `matplotlib`:用于显示和可视化图像。 可以通过以下命令安装这些库: ```bash pip install numpy pillow matplotlib ``` ## 三、代码功能及使用方法 ### 1. 图像上半部分接近黑色像素转红色 ```python import numpy as np from PIL import Image import matplotlib.pyplot as plt # 打开图像 img = Image.open("x.jpg") # 将图像转换为 numpy 数组 img_array = np.array(img) # 获取图像的高度和宽度 height, width = img_array.shape[:2] # 创建一个副本,避免修改原始图像数组 new_img_array = img_array.copy() # 设置阈值 threshold = 10 # 遍历图像上半部分 for y in range(height // 2): for x in range(width): # 获取当前像素的 RGB 值 r, g, b = new_img_array[y, x] # 如果像素接近黑色,则将其改为红色 if r < threshold and g < threshold and b < threshold: new_img_array[y, x] = [255, 0, 0] # 使用 matplotlib 显示处理后的图像 plt.imshow(new_img_array) # 去除坐标轴 plt.axis('off') # 显示图像 plt.show() # 将处理后的 numpy 数组转换回 PIL 图像 new_img = Image.fromarray(new_img_array) # 保存处理后的图像 new_img.save("x_upper_black_to_red_result.jpg") ``` **使用方法**:将 `x.jpg` 替换为你要处理的图像路径,运行脚本,处理后的图像将保存为 `x_upper_black_to_red_result.jpg`。 ### 2. 图像左半部分接近黑色像素转红色 ```python from PIL import Image import matplotlib.pyplot as plt img = Image.open("x.jpg") width, height = img.size def process_left_black_to_red(): new_img = img.copy() # 设置阈值 threshold = 10 for y in range(height): for x in range(width // 2): r, g, b = new_img.getpixel((x, y)) # 判断像素是否接近黑色 if r < threshold and g < threshold and b < threshold: new_img.putpixel((x, y), (255, 0, 0)) new_img.save("x2.jpg") return new_img if __name__ == "__main__": processed_img = process_left_black_to_red() plt.imshow(processed_img) plt.axis('off') plt.show() ``` **使用方法**:将 `x.jpg` 替换为实际图像路径,运行脚本,处理后的图像将保存为 `x2.jpg`。 ### 3. 图像左上角四分之一部分接近黑色像素转红色 ```python from PIL import Image import matplotlib.pyplot as plt # 打开图像并转换为 RGB 模式,确保处理的是 RGB 图像 img = Image.open("x.jpg").convert('RGB') width, height = img.size def process_top_left_quarter_black_to_red(): new_img = img.copy() quarter_width = width // 2 quarter_height = height // 2 # 设置阈值,用于判断接近黑色的像素 threshold = 10 for y in range(quarter_height): for x in range(quarter_width): r, g, b = new_img.getpixel((x, y)) # 判断像素是否接近黑色 if r < threshold and g < threshold and b < threshold: new_img.putpixel((x, y), (255, 0, 0)) new_img.save("x3.jpg") return new_img if __name__ == "__main__": # 调用处理函数并获取处理后的图像 processed_img = process_top_left_quarter_black_to_red() # 使用 matplotlib 显示处理后的图像 plt.imshow(processed_img) # 去除坐标轴 plt.axis('off') # 显示图像 plt.show() ``` **使用方法**:将 `x.jpg` 替换为目标图像路径,运行脚本,处理后的图像将保存为 `x3.jpg`。 ### 4. 图像中心 4x4 区域接近黑色像素转红色 ```python from PIL import Image import matplotlib.pyplot as plt # 打开图像并转换为 RGB 模式,确保处理的是 RGB 图像 img = Image.open("x.jpg").convert('RGB') width, height = img.size # 计算图像中心坐标 center_x = width // 2 center_y = height // 2 # 计算 4x4 区域的起始坐标 start_x = center_x - 2 start_y = center_y - 2 def process_center_4x4_black_to_red(): new_img = img.copy() # 设置阈值,用于判断接近黑色的像素 threshold = 10 # 遍历 4x4 区域 for y in range(start_y, start_y + 4): for x in range(start_x, start_x + 4): # 检查坐标是否在图像范围内 if 0 <= x < width and 0 <= y < height: r, g, b = new_img.getpixel((x, y)) # 判断像素是否接近黑色 if r < threshold and g < threshold and b < threshold: new_img.putpixel((x, y), (255, 0, 0)) # 保存处理后的图像 new_img.save("x4.jpg") return new_img if __name__ == "__main__": # 调用处理函数并获取处理后的图像 processed_img = process_center_4x4_black_to_red() # 使用 matplotlib 显示处理后的图像 plt.imshow(processed_img) # 去除坐标轴 plt.axis('off') # 显示图像 plt.show() ``` **使用方法**:将 `x.jpg` 替换为要处理的图像,运行脚本,处理后的图像将保存为 `x4.jpg`。 ### 5. 图像中心 8x8 区域颜色变换 ```python from PIL import Image import matplotlib.pyplot as plt # 打开图像并转换为 RGB 模式,确保后续处理的是 RGB 图像 img = Image.open("x.jpg").convert('RGB') width, height = img.size # 计算图像中心位置 center_x = width // 2 center_y = height // 2 # 计算 8x8 区域的起始坐标 start_x = center_x - 4 start_y = center_y - 4 def process_center_8x8_color_change(): new_img = img.copy() # 设置判断黑色和白色的阈值 black_threshold = 10 white_threshold = 245 # 遍历 8x8 区域 for y in range(start_y, start_y + 8): for x in range(start_x, start_x + 8): # 确保坐标在图像范围内 if 0 <= x < width and 0 <= y < height: r, g, b = new_img.getpixel((x, y)) # 判断是否近似黑色像素 if r < black_threshold and g < black_threshold and b < black_threshold: new_img.putpixel((x, y), (0, 255, 0)) # 判断是否近似白色像素 elif r > white_threshold and g > white_threshold and b > white_threshold: new_img.putpixel((x, y), (255, 255, 0)) # 保存处理后的图像 new_img.save("x5.jpg") return new_img if __name__ == "__main__": # 调用处理函数获取处理后的图像 processed_img = process_center_8x8_color_change() # 使用 matplotlib 显示处理后的图像 plt.imshow(processed_img) # 去除坐标轴 plt.axis('off') # 显示图像 plt.show() ``` **使用方法**:将 `x.jpg` 替换为目标图像路径,运行脚本,处理后的图像将保存为 `x5.jpg`。 ### 6. 分离图像红色通道并保存 ```python import numpy as np from PIL import Image import matplotlib.pyplot as plt try: # 打开图像 image = Image.open('galaxy-full.jpg') # 分离图像的 RGB 通道 r, g, b = image.split() # 创建一个全黑的单通道图像,尺寸与原图像相同 zeros = Image.new('L', image.size, 0) # 合并通道,只保留红色通道,其余通道置为 0 red_channel_image = Image.merge('RGB', (r, zeros, zeros)) # 将 PIL 图像转换为 numpy 数组 red_channel_array = np.array(red_channel_image) # 使用 matplotlib 显示图像 plt.imshow(red_channel_array) # 设置图像标题 plt.title('Red Channel Image') # 关闭坐标轴显示 plt.axis('off') # 显示图像 plt.show() # 保存处理后的图像 red_channel_image.save('red_galaxy.jpg') except FileNotFoundError: print("error") ``` **使用方法**:将 `galaxy-full.jpg` 替换为实际图像路径,运行脚本,分离出的红色通道图像将保存为 `red_galaxy.jpg`。 ### 7. 分离图像绿色通道并保存 ```python from PIL import Image import matplotlib.pyplot as plt try: image = Image.open('galaxy-full.jpg') r, g, b = image.split() zeros = Image.new('L', image.size, 0) green_channel_image = Image.merge('RGB', (zeros, g, zeros)) # 使用 matplotlib 显示图像 plt.imshow(green_channel_image) plt.title("Green Channel of Galaxy Image") # 设置图像标题 plt.axis('off') # 关闭坐标轴显示 plt.show() green_channel_image.save('green_galaxy.jpg') except FileNotFoundError: print("error") ``` **使用方法**:将 `galaxy-full.jpg` 替换为目标图像路径,运行脚本,绿色通道图像将保存为 `green_galaxy.jpg`。 ### 8. 分离图像蓝色通道并保存 ```python import matplotlib.pyplot as plt try: image = Image.open('galaxy-full.jpg') r, g, b = image.split() zeros = Image.new('L', image.size, 0) blue_channel_image = Image.merge('RGB', (zeros, zeros, b)) # 使用 matplotlib 显示图像 plt.imshow(blue_channel_image) plt.title("Blue Channel Image of Galaxy") # 设置图像标题 plt.axis('off') # 关闭坐标轴 plt.show() blue_channel_image.save('blue_galaxy.jpg') except FileNotFoundError: print("error") ``` **使用方法**:将 `galaxy-full.jpg` 替换为图像路径,运行脚本,蓝色通道图像将保存为 `blue_galaxy.jpg`。 ### 9. 为图像添加随机多色条纹效果 ```python from PIL import Image import numpy as np import matplotlib.pyplot as plt import random # 加载图像 image_path = 'earth.jpg' image = Image.open(image_path) # 转换为numpy数组 image_np = np.array(image) height, width, _ = image_np.shape new_image_np = image_np.copy() # 定义多种颜色(RGB格式) colors = [ (255, 0, 0), # 红 (0, 255, 0), # 绿 (0, 0, 255), # 蓝 (255, 255, 0), # 黄 (128, 0, 128), # 紫 (0, 255, 255) # 青 ] color_num = len(colors) # 定义混合比例,可根据需要调整 mixing_ratio = 0.2 # 定义条纹宽度,可根据需要调整 stripe_width = 3 # 生成随机的条纹起始位置列表 stripe_starts = [] current_pos = 0 while current_pos < width: # 随机决定是否开始一个新的条纹 if random.random() < 0.5: stripe_starts.append(current_pos) current_pos += stripe_width else: current_pos += 1 # 应用条纹效果 for i, start in enumerate(stripe_starts): color_idx = i % color_num r, g, b = colors[color_idx] # 按通道分别混合颜色 for channel in range(3): for j in range(stripe_width): if start + j < width: # 计算混合后的通道值 new_image_np[:, start + j, channel] = (1 - mixing_ratio) * image_np[:, start + j, channel] + mixing_ratio * [r, g, b][channel] # 确保像素值在0 - 255范围内 new_image_np = np.clip(new_image_np, 0, 255).astype(np.uint8) # 转换回图像并保存 new_image = Image.fromarray(new_image_np) new_image.save('random_multi_striped_image.jpg') # 显示图像 plt.figure(figsize=(15, 15)) plt.subplot(1, 2, 1) plt.imshow(image) plt.title('Original Image') plt.subplot(1, 2, 2) plt.imshow(new_image) plt.title('Random Multi-color Striped Image') plt.show() ``` **使用方法**:将 `earth.jpg` 替换为实际图像路径,运行脚本,添加条纹效果后的图像将保存为 `random_multi_striped_image.jpg`。 ## 四、注意事项 1. 确保输入的图像文件存在且路径正确,否则会抛出 `FileNotFoundError` 异常。 2. 部分代码中设置的阈值和参数(如条纹宽度、混合比例等)可以根据实际需求进行调整。 3. 处理后的图像会保存在当前工作目录下,可根据需要修改保存路径和文件名。 你可以根据实际情况对上述 `README.md` 内容进行进一步的修改和完善。