计算机视觉-基本图像操作和处理

1.1 Python图像处理PIL库

默认图片1.jpg

1.1.1 转换图像格式

from PIL import Image

# 打开JPG格式图像文件
jpg_image = Image.open('input.jpg')

# 转换为PNG格式图像
png_image = jpg_image.convert('RGBA')

# 保存为PNG格式图像文件
png_image.save('output.png')

1.1.2 缩略图

from PIL import Image

def generate_thumbnail(source_image_path, thumbnail_width):
    """生成指定宽度的缩略图"""
    with Image.open(source_image_path) as image:
        # 计算缩略图的高度
        thumbnail_height = int(thumbnail_width * image.height / image.width)
        # 生成缩略图
        thumbnail = image.resize((thumbnail_width, thumbnail_height))
        # 保存缩略图
        thumbnail.save('small.jpg')
generate_thumbnail('1.jpg', 200)

1.1.3 复制和粘贴图像区域

from PIL import Image

# 打开图片
with Image.open('1.jpg') as im:
    # 计算提取区域的大小和位置
    width, height = im.size
    left = 0
    top = 0
    right = width // 2
    bottom = height // 2
    # 提取区域
    region = im.crop((left, top, right, bottom))
    # 保存提取的区域
    region.save('crop.jpg')

1.1.4 调整尺寸和旋转

from PIL import Image

def resize_and_rotate(source_image_path, destination_image_path, size, degrees):
    """调整图片尺寸和旋转角度"""
    with Image.open(source_image_path) as image:
        # 调整图片尺寸
        resized_image = image.resize(size)
        # 旋转图片
        rotated_image = resized_image.rotate(degrees)
        # 保存结果图片
        rotated_image.save(destination_image_path)

resize_and_rotate('1.jpg', 'des.jpg', (300, 300), 45)

1.2 Matplotlib

1.2.1 绘制图像、点和线

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# 加载图像文件
image = mpimg.imread('1.jpg')

# 创建一个新的图像窗口
fig, ax = plt.subplots()

# 显示图像
ax.imshow(image)

# 绘制一个红色的点
ax.plot(250, 1000, 'ro')
ax.plot(1000, 1000, 'ro')
# 绘制一条蓝色的线
ax.plot([250, 1000], [1000, 1000], 'b-')

# 显示图像
plt.show()

1.2.2 图像轮廓和直方图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值