Windows system uses powershell to crop the picture to the specified size

Table of contents

Write and run the .ps1 file

Code explanation (not important, it is recommended to skip)

Set the size and position of the crop box

report error

Summarize 


Use powershell to crop the picture, and cut out the 120-290 pixels of the original picture

Write and run the .ps1 file

Create a new text document and enter the following content:

# 指定原始图片路径和输出路径
$sourcePath = "C:\path\to\original\image.jpg"
$outputPath = "C:\path\to\output\image.jpg"

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

# 打开原始图片
$image = [System.Drawing.Image]::FromFile($sourcePath)

# 设置裁剪框的大小和位置
$cropRectangle = New-Object System.Drawing.Rectangle(120, 0, 170, $image.Height)

# 创建一个新的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)

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

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

The path is modified according to the situation. The "$source"Path and "$outputPath" in the code represent the path of the original image and the path of the image after screenshot respectively, which can be set according to the requirement.

Save as a file with the suffix ".ps1", such as "111.ps1". Open powershell, enter ".\111.ps1" under the path of "111.ps1" to execute the file.

Code explanation (not important, it is recommended to skip)

The relevant code explanation given by GPT:

Of course, I'd be happy to explain to you what each line of PowerShell code does:

```
# Specify the original image path and output path
$sourcePath = "C:\path\to\original\image.jpg"
$outputPath = "C:\path\to\output\image.jpg"

# Load the System.Drawing namespace
Add-Type -AssemblyName System.Drawing
```
These two lines are used to specify the path and output path of the original image, and import the System.Drawing namespace. Later we will use the classes and methods in this namespace for image processing.

```
# Open the original image
$image = [System.Drawing.Image]::FromFile($sourcePath)
```
By calling the `FromFile()` method of the `System.Drawing.Image` class, we can open the original image , and assign it to a variable `$image`.

```
# Set the size and position of the crop box
$cropRectangle = New-Object System.Drawing.Rectangle(120, 0, 170, $image.Height) ``
`
This line of code sets the size and position of the crop box. In this example, we crop a part from the original image with a width of 170 pixels, a height consistent with the original image, and a position starting at 120 pixels. This crop box is created using `System.Drawing.Rectangle` class.

```
# Create a new Bitmap and draw the cropped part into the 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) ```
These
three lines are used to create a new Bitmap, and Draw the cropped part into the Bitmap. We create a new Bitmap object through the `System.Drawing.Bitmap` class and assign it to the `$croppedImage` variable. Then, we use the `System.Drawing.Graphics` class to create a new Graphics object from the Bitmap and assign it to the `$graphics` variable. Finally, we use the `$graphics.DrawImage()` method to draw the specified area (that is, the cropping box) in the original image into the new Bitmap.

```
# Save the cropped image
$croppedImage.Save($outputPath)

# Release resources
$image.Dispose()
$croppedImage.Dispose()
$graphics.Dispose()
```
These three lines of code are used to save the cropped image and release all occupied resources. We use the `$croppedImage.Save()` method to save the cropped image to the specified output path. Finally, we release all resources used by the `$image`, `$croppedImage`, and `$graphics` objects using the `Dispose()` method.

Set the size and position of the crop box

$cropRectangle = New-Object System.Drawing.Rectangle(`X` , `Y` ,`Width`, `Height` ) 

The `New-Object System.Drawing.Rectangle` class allows us to create a rectangle object that specifies the area in the image that needs to be cropped. It has the following four parameters:

1. The `X` parameter indicates the x-coordinate of the position of the upper left corner of the rectangle.
2. The `Y` parameter indicates the y coordinate of the position of the upper left corner of the rectangle.
3. The `Width` parameter indicates the width of the rectangle.
4. The `Height` parameter indicates the height of the rectangle.

So, if you want to crop a 150x150 pixel portion of the upper left corner of the original image, you can set the size and position of the crop box like this:

```
$cropRectangle = New-Object System.Drawing.Rectangle(0, 0, 150, 150)
```

If you want to crop a 400x300 pixel image from the right side of the original image by 200 pixels, you can set it like this:

```
$cropRectangle = New-Object System.Drawing.Rectangle($image.Width - 200 - 400, 0, 400, 300)
```

Among them, `$image.Width` is the width of the original image, we calculate the offset from the right, and then set the width and height of the cropping box. In short, according to the actual situation, you can customize the position and size of the cropping box.

 $image.Width represents the width of the original image, and $image.Height represents the height of the original image. These variables can also be used for image size settings.

for example:

There is an original image with a width of 350 pixels and a height of 260 pixels. If you want to crop out the time part, you must first preselect the pixels in this area.

This area pixel starts at (153,55), height is 33, width is 94

# Set the size and position of the crop box
$cropRectangle = New-Object System.Drawing.Rectangle(153, 55, 94, 33)

Screenshot effect:

 The units of width and height of the picture are pixels, which can be viewed by using software such as "Drawing".

report error

For powershell running commands, common errors and handling, please refer to:

Common errors and handling of Windows system powershell running commands - Weixin_56337147's Blog - CSDN Blog

Summarize 

Write and run the .ps1 file to realize the function of cropping pictures.

Guess you like

Origin blog.csdn.net/weixin_56337147/article/details/130753933