# youtingyang333 **Repository Path**: youtingyang/youtingyang333 ## Basic Information - **Project Name**: youtingyang333 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-09-27 - **Last Updated**: 2025-10-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 图像基础处理 环境要求 - Python 3.6+ - 所需库:matplotlib, numpy, PIL (Pillow) 安装依赖 pip install matplotlib numpy pillow 文件说明 · image_processing.py - 主程序文件,包含所有图像处理任务 · earth.jpg - 输入图像文件(请替换为您自己的图像) · output/ - 输出目录,包含所有处理后的图像 一.代码示例说明 1. 任务1: 图像上半部分处理 height, width, channels = img_array.shape upper_half = img_array[:height//2, :, :].copy() # 可选:对上部分进行一些处理,比如增加亮度 upper_half = np.clip(upper_half * 1.2, 0, 255).astype(np.uint8) result = img_array.copy() result[:height//2, :, :] = upper_half return result 2.任务2: 图像左半部分处理 height, width, channels = img_array.shape left_half = img_array[:, :width//2, :].copy() # 可选:对左半部分进行处理,比如增加对比度 left_half = np.clip((left_half - 128) * 1.3 + 128, 0, 255).astype(np.uint8) result = img_array.copy() result[:, :width//2, :] = left_half return result 3.任务3: 图像左上四分之一处理 height, width, channels = img_array.shape upper_left = img_array[:height//2, :width//2, :].copy() # 可选:对左上四分之一进行处理,比如变为灰度 gray = np.dot(upper_left[...,:3], [0.2989, 0.5870, 0.1140]) upper_left[:, :, 0] = gray upper_left[:, :, 1] = gray upper_left[:, :, 2] = gray result = img_array.copy() result[:height//2, :width//2, :] = upper_left return result 4. 任务4: 小区域像素处理 height, width, channels = img_array.shape result = img_array.copy() # 在图像中心创建一个50x50的小区域 center_y, center_x = height//2, width//2 small_region_size = 50 # 将小区域变为红色 start_y = center_y - small_region_size//2 end_y = center_y + small_region_size//2 start_x = center_x - small_region_size//2 end_x = center_x + small_region_size//2 result[start_y:end_y, start_x:end_x, 0] = 255 # R result[start_y:end_y, start_x:end_x, 1] = 0 # G result[start_y:end_y, start_x:end_x, 2] = 0 # B return result 5.任务5: 指定区域颜色处理 height, width, channels = img_array.shape result = img_array.copy() # 在右下角创建一个100x100的绿色区域 region_size = 100 start_y = height - region_size end_y = height start_x = width - region_size end_x = width result[start_y:end_y, start_x:end_x, 0] = 0 # R result[start_y:end_y, start_x:end_x, 1] = 255 # G result[start_y:end_y, start_x:end_x, 2] = 0 # B return result 6.任务6-8: 星系图像颜色通道处理 # 分离RGB通道 r_channel = img_array[:, :, 0].copy() g_channel = img_array[:, :, 1].copy() b_channel = img_array[:, :, 2].copy() # 创建单通道图像 red_img = np.zeros_like(img_array) red_img[:, :, 0] = r_channel green_img = np.zeros_like(img_array) green_img[:, :, 1] = g_channel blue_img = np.zeros_like(img_array) blue_img[:, :, 2] = b_channel return red_img, green_img, blue_img 9. 任务9: 条纹效果处理(基于提供的代码) height, width, _ = img_array.shape # 调整图像亮度(使图像更亮) bright_img_array = np.clip(img_array.astype(np.float32) * 1.5, 0, 255).astype(np.uint8) # 生成条纹效果(每隔10列设置条纹) strip_width = 10 result = bright_img_array.copy() for j in range(width): if j % strip_width == 0: # 将条纹列设置为稍微亮一些的蓝色 result[:, j, :] = [200, 200, 255] return result def main(): """主函数:执行所有图像处理任务""" # 创建输出目录 create_output_dir() # 读取图像(请替换为您的图像路径) img_path = "earth.jpg" # 或者使用其他图像文件 try: img = Image.open(img_path) except FileNotFoundError: print(f"错误:找不到图像文件 {img_path}") print("请确保图像文件存在于当前目录中") return # 转换为numpy数组 img_array = np.array(img) # 执行所有任务 tasks = { "1_upper_half": task1_upper_half(img_array), "2_left_half": task2_left_half(img_array), "3_upper_left_quarter": task3_upper_left_quarter(img_array), "4_small_region": task4_small_region(img_array), "5_specified_region": task5_specified_region(img_array), "9_stripes_effect": task9_stripes_effect(img_array) } # 任务6-8:颜色通道分离 red_img, green_img, blue_img = task6_8_galaxy_channels(img_array) tasks["6_red_channel"] = red_img tasks["7_green_channel"] = green_img tasks["8_blue_channel"] = blue_img # 显示和保存结果 fig, axes = plt.subplots(3, 4, figsize=(20, 15)) axes = axes.ravel() # 显示原图 axes[0].imshow(img_array) axes[0].set_title('Original Image') axes[0].axis('off') # 显示处理后的图像 for i, (task_name, result_array) in enumerate(tasks.items(), 1): axes[i].imshow(result_array) axes[i].set_title(f'Task {task_name}') axes[i].axis('off') # 保存处理后的图像 result_img = Image.fromarray(result_array) result_img.save(f'output/{task_name}.jpg') # 隐藏多余的子图 for i in range(len(tasks) + 1, len(axes)): axes[i].axis('off') plt.tight_layout() plt.savefig('output/all_tasks_comparison.jpg', dpi=150, bbox_inches='tight') plt.show() print("所有图像处理任务已完成!") print("结果已保存到 'output' 目录中") if __name__ == "__main__": main() 使用方法 1. 确保已安装所需依赖 2. 将您的图像文件命名为 earth.jpg 或修改代码中的图像路径 3. 运行程序:python image_processing.py 4. 查看 output 目录中的结果图像 学习要点 · 图像的基本读写操作 · 图像像素级操作 · 区域选择和修改 · 颜色通道分离 · 图像增强技术 个人信息 学号:202452320229 年级:2024 专业:智能科学与技术 班级:2 班