Windows系统批量裁剪文件夹里的图片为同一大小

目录

编写运行.ps1文件

代码解释与设置裁剪框的大小和位置

报错

图片位深度变化

其他

总结 


使用powershell批量裁剪图片,文件夹里的图片批量裁剪成指定大小,保存到另一个文件夹中。

比如,网课或者讲座的截图,想把他们的PPT部分裁剪出来,PPT一般是截图图片中的固定像素区域,通过本文方法可以把文件夹里的图片中一定像素区域截取出来。

编写运行.ps1文件

新建文本文档,输入如下内容:

# 指定原始图片所在的文件夹和输出路径
$sourceFolder = "D:\111\111_1"
$outputFolder = "D:\111\111_2"

# 加载System.Drawing命名空间
Add-Type -AssemblyName System.Drawing

# 获取文件夹中所有图片文件
$files = Get-ChildItem $sourceFolder -Filter *.jpg

# 对每个文件进行处理
foreach ($file in $files) {
    # 打开原始图片
    $image = [System.Drawing.Image]::FromFile($file.FullName)

    # 设置裁剪框的大小和位置,
    $newWidth = 1080
    $newHeight =797
    ##这里假设要对图片进行等比例缩放到宽度为500
    ##$newHeight = [int]($image.Height * $newWidth / $image.Width)
    $cropRectangle = New-Object System.Drawing.Rectangle(0, 292, $newWidth, $newHeight)

    # 创建一个新的Bitmap并把裁剪后的部分绘制到Bitmap中
    $croppedImage = New-Object System.Drawing.Bitmap($cropRectangle.Width, $cropRectangle.Height)
    $graphics = [System.Drawing.Graphics]::FromImage($croppedImage)
    $graphics.DrawImage($image, 0, 0, $cropRectangle, [System.Drawing.GraphicsUnit]::Pixel)

    # 构造输出路径
    $outputPath = Join-Path $outputFolder $file.Name

    # 保存裁剪后的图片
    $croppedImage.Save($outputPath)

    # 释放资源
    $image.Dispose()
    $croppedImage.Dispose()
    $graphics.Dispose()
}

路径根据情况修改。代码中的“$source”Path和“$outputPath”分别代表原图的路径和截图后的图片路径,根据需求设定。

保存为“.ps1”为后缀的文件,如“111.ps1”。打开powershell,在“111.ps1”的路径下输入“ .\111.ps1”,执行该文件。

成功运行

代码解释与设置裁剪框的大小和位置

参考:

Windows系统使用powershell裁剪图片为指定大小_weixin_56337147的博客-CSDN博客

报错

图片位深度变化

可以通过显式指定 PixelFormat 参数来保持输出的图像格式不变。

在创建新的 Bitmap 对象时,显式指定了 PixelFormat 参数为 Format24bppRgb,以保持输出图像的位深度不变。同时,也需要在保存图像时指定格式为 JPEG,以确保输出的图像格式正确。

修改后程序:

# 指定原始图片文件夹路径和输出文件夹路径
$sourceFolder = "C:\path\to\original\folder"
$outputFolder = "C:\path\to\output\folder"

# 加载System.Drawing命名空间
Add-Type -AssemblyName System.Drawing

# 获取文件夹中所有图片文件
$files = Get-ChildItem $sourceFolder -Filter *.jpg

# 对每个文件进行处理
foreach ($file in $files) {
    # 打开原始图片
    $image = [System.Drawing.Image]::FromFile($file.FullName)

    # 设置裁剪框的大小和位置,这里假设要对图片进行等比例缩放到宽度为500
    $newWidth = 500
    $newHeight = 700
    $cropRectangle = New-Object System.Drawing.Rectangle(0, 0, $newWidth, $newHeight)

    # 创建一个新的Bitmap并把裁剪后的部分绘制到Bitmap中
    $croppedImage = New-Object System.Drawing.Bitmap($cropRectangle.Width, $cropRectangle.Height, [System.Drawing.Imaging.PixelFormat]::Format24bppRgb)
    $graphics = [System.Drawing.Graphics]::FromImage($croppedImage)
    $graphics.DrawImage($image, 0, 0, $cropRectangle, [System.Drawing.GraphicsUnit]::Pixel)

    # 构造输出路径
    $outputPath = Join-Path $outputFolder $file.Name

    # 保存裁剪后的图片
    $croppedImage.Save($outputPath, [System.Drawing.Imaging.ImageFormat]::Jpeg)

    # 释放资源
    $image.Dispose()
    $croppedImage.Dispose()
    $graphics.Dispose()
}

可以看出,在

    $croppedImage = New-Object System.Drawing.Bitmap($cropRectangle.Width, $cropRectangle.Height, [System.Drawing.Imaging.PixelFormat]::Format24bppRgb)

    # 保存裁剪后的图片
    $croppedImage.Save($outputPath, [System.Drawing.Imaging.ImageFormat]::Jpeg)

两处修改。 

其他

powershell运行指令,常出现的报错及处理参见:

Windows系统powershell运行指令常见报错及处理_weixin_56337147的博客-CSDN博客

总结 

编写运行.ps1文件,实现批量的裁剪图片的功能。

猜你喜欢

转载自blog.csdn.net/weixin_56337147/article/details/130794930
今日推荐