2023(春)Python程序设计作业4:Numpy 和 Pillow在图像处理中的应用

我喜我生 独丁斯时 2023-春-学生 2023-05-24 21:25:54

一、实验题目

     Numpy 和 Pillow在图像处理中的应用

二、实验目的

1) 掌握Numpy的基本应用

2)掌握 Pillow的简单用法

三、实验任务

任务 1)从以下网站:南通大学[学校掠影](https://www.ntu.edu.cn/77/list.htm)任选一副图片下载,用Python程序显示原始图片。

为了方便,我直接放在了项目之中

引用代码为:

from PIL import Image
import numpy as np
# 打开图片
img = Image.open('Nightday.jpg')
img_array = np.array(img)

 任务 2)将图片变成黑白图片,并用Python程序显示。

# 将 RGB 图像转换为灰度图像
gray_img_array = np.dot(img_array[...,:3], [0.2989, 0.5870, 0.1140])

# 将 numpy 数组转换回图像并显示
gray_img = Image.fromarray(gray_img_array.astype('uint8'))
gray_img.show()

运行效果为:

 任务3)将图片左右翻转,并用Python程序显示。

# 左右翻转图像
flipped_img_array = img_array[:, ::-1]

# 将 numpy 数组转换回图像并显示
flipped_img = Image.fromarray(flipped_img_array)
flipped_img.show()

运行效果:

任务4)将图片上下翻转,并用Python程序显示。 

# 上下翻转图像
flipped_img_array = img_array[::-1]

# 将 numpy 数组转换回图像并显示
flipped_img = Image.fromarray(flipped_img_array)
flipped_img.show()

运行效果:

任务5 将图片亮度增强50%

# 调整图像亮度
bright_img_array = img_array * 1.5

# 将 numpy 数组转换回图像并显示
bright_img = Image.fromarray(np.clip(bright_img_array, 0, 255).astype('uint8'))
bright_img.show()

 

四、将实验任务完成过程和结果,按任务顺序,发表博客

...全文
37 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

163

社区成员

发帖
与我相关
我的任务
社区描述
软件工程老师
python 高校 江苏省·南通市
社区管理员
  • juking@ntu
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧