# 社区 南通大学-Python程序设计布置作业帖子详情 2023（春）Python程序设计作业4：Numpy 和 Pillow在图像处理中的应用

from PIL import Image
import numpy as np
# 任务1：显示原始图片
image = Image.open('img.png')
image.show()

from PIL import Image
import numpy as np
image = Image.open('img.png')
# 将图片转换为numpy数组
image_np = np.array(image)
# 任务2：将图片变成黑白图片，并显示
gray_image_np = np.mean(image_np, axis=2)
gray_image = Image.fromarray(gray_image_np.astype('uint8'))
gray_image.show()

from PIL import Image
import numpy as np
image = Image.open('img.png')
# 将图片转换为numpy数组
image_np = np.array(image)
# 任务3：将图片左右翻转，并显示
flipped_lr_image_np = np.fliplr(image_np)
flipped_lr_image = Image.fromarray(flipped_lr_image_np)
flipped_lr_image.show()

from PIL import Image
import numpy as np
image = Image.open('img.png')
# 将图片转换为numpy数组
image_np = np.array(image)
# 任务4：将图片上下翻转，并显示
flipped_tb_image_np = np.flipud(image_np)
flipped_tb_image = Image.fromarray(flipped_tb_image_np)
flipped_tb_image.show()

from PIL import Image
import numpy as np
# 打开图片并将其转换为numpy数组
image = Image.open('img.png')
image_np = np.array(image)
# 裁剪图片
cropped_image_np = image_np[:1600, :1200]
# 更改RGB值
cropped_image_np[:, :, 0] = cropped_image_np[:, :, 0] * 2
cropped_image_np[:, :, 1] = cropped_image_np[:, :, 1] * 1
cropped_image_np[:, :, 2] = cropped_image_np[:, :, 2] * 2
# 将图片上下翻转
flipped_image_np = np.flipud(cropped_image_np)
# 将像素点乘以1.5
final_image_np = flipped_image_np * 1.5
# 将像素值限制在0-255范围内
final_image_np = np.clip(final_image_np, 0, 255)
# 将numpy数组转换回图片并显示
final_image = Image.fromarray(final_image_np.astype('uint8'))
final_image.show()

...全文
26 回复 打赏 收藏 转发到动态 举报

163

python 高校 江苏省·南通市

• 近7日
• 近30日
• 至今