python 影像裁切程序

   参数传入的Polygon 图形坐标

#demo_string="722812.922144461772405 4479018.211012263782322,722806.479693785076961 4479018.211012263782322,723089.947523561189882 4479008.547336248680949,723099.611199576291256 4478660.654999705031514, 722816.143369800178334 4478670.318675720132887,722812.922144461772405 4479018.211012263782322 "

raster_file  栅格文件路径

out_path  输出路径

name 输出文件名称

def clip_raster_from_coorlist(demo_string,raster_file,out_path,name='coord'):
    
    try:
    
        geometry1= coorlisttogeometry(demo_string)
        if(geometry1==None):
            return
        if geometry1.is_empty:
            #print(f"Skipping empty geometry for feature {index}")
            return  1
            # 打开影像文件
        print(raster_file)
        print("-------------------------------------0----------------------------")
   

### Python进行原始图像处理 在Python中,可以使用多个库来进行原始图像处理。其中最常用的是OpenCV和Pillow这两个库[^2]。 #### OpenCV库简介 OpenCV是一个开源的计算机视觉库,提供了大量的函数用于图像处理和计算机视觉应用开发。该库支持多种编程语言,但在Python中的接口尤为友好。通过安装`opencv-python`包即可快速上手使用此工具来加载、显示、保存图片文件并执行各种变换操作。 #### Pillow库概述 Pillow作为PIL(Python Imaging Library)的一个分支,在保持原生功能的同时增加了更多特性和支持新格式的能力。它非常适合于简单的图像编辑任务如调整大小、旋转翻转等基础性工作,并且易于学习掌握。 对于具体的原始图像处理方法: - **读取与展示图像** 可以借助上述提到任一库完成这项最基本的任务。以下是采用这两种方式的例子代码片段: ```python import cv2 from PIL import Image # 使用OpenCV读取图像 image_cv = cv2.imread('example.jpg') cv2.imshow('Image', image_cv) # 使用Pillow打开图像 image_pil = Image.open('example.jpg') image_pil.show() ``` - **裁剪图像** 这里提供了一个基于Pillow实现简单矩形区域裁切的功能示范: ```python def crop_image(image_path, output_path, box=(0, 0, 100, 100)): with Image.open(image_path) as img: cropped_img = img.crop(box) cropped_img.save(output_path) crop_image('input.png', 'output_cropped.png', (50, 50, 200, 200)) ``` - **缩放图像尺寸** 下面这段程序展示了怎样利用OpenCV改变一张照片的比例尺: ```python def resize_image(input_file, output_file, scale_percent=50): original = cv2.imread(input_file) width = int(original.shape[1] * scale_percent / 100) height = int(original.shape[0] * scale_percent / 100) dim = (width, height) resized = cv2.resize(original, dim, interpolation=cv2.INTER_AREA) cv2.imwrite(output_file, resized) resize_image('source.bmp', 'resized.bmp', 75) ``` 以上仅列举了一些入门级的操作实例;实际上这两款软件包还包含了更高级别的特征提取、滤波器设计等功能等待探索者去发掘[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值