标题: [问题求助] [已解决]PowerShell怎样给图片左右两边加上蓝色? [打印本页]
作者: czjt1234 时间: 2024-5-28 10:32 标题: [已解决]PowerShell怎样给图片左右两边加上蓝色?
本帖最后由 czjt1234 于 2024-5-29 05:31 编辑
- Add-Type -AssemblyName System.Drawing
- function Cover-Image {
- param(
- [string]$ImagePath,
- [int]$TopCover,
- [int]$BottomCover
- )
- $originalImage = [System.Drawing.Image]::FromFile($ImagePath)
- $originalHeight = $originalImage.Height
- $originalWidth = $originalImage.Width
- $graphics = [System.Drawing.Graphics]::FromImage($originalImage)
- $whiteBrush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::White)
- $graphics.FillRectangle($whiteBrush, 0, 0, $originalWidth, $TopCover)
- $graphics.FillRectangle($whiteBrush, 0, $originalHeight - $BottomCover, $originalWidth, $BottomCover)
- $coveredImagePath = [System.IO.Path]::ChangeExtension($ImagePath, "covered.jpg")
- $originalImage.Save($coveredImagePath, [System.Drawing.Imaging.ImageFormat]::Jpeg)
- }
-
- # 白色覆盖顶部100像素,底部150像素
- $topCover = 100
- $bottomCover = 150
-
- dir *.jpg | ForEach-Object {
- Cover-Image -ImagePath $_.FullName -TopCover $topCover -BottomCover $bottomCover
- }
复制代码
这是buyiyang的覆盖图片上下为白色的脚本,测试可行
现在我想给图片左右两边附加上蓝色,不是覆盖而是添加,请问要怎么修改?
作者: Five66 时间: 2024-5-28 17:11
新建对应大小的birmap,填充颜色后将原图贴上去,比如下面的- Add-Type -AssemblyName System.Drawing
- function Cover-Image {
- param(
- [string]$ImagePath,
- # [int]$TopCover,
- # [int]$BottomCover,
- [int]$leftAdd,
- [int]$rightAdd,
- [Drawing.Color]$colorAdd
- )
- $originalImage = [System.Drawing.Image]::FromFile($ImagePath)
- $originalHeight = $originalImage.Height
- $originalWidth = $originalImage.Width
- # $graphics = [System.Drawing.Graphics]::FromImage($originalImage)
- # $whiteBrush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::White)
- # $graphics.FillRectangle($whiteBrush, 0, 0, $originalWidth, $TopCover)
- # $graphics.FillRectangle($whiteBrush, 0, $originalHeight - $BottomCover, $originalWidth, $BottomCover)
- # $coveredImagePath = [System.IO.Path]::ChangeExtension($ImagePath, "covered.jpg")
- # $originalImage.Save($coveredImagePath, [System.Drawing.Imaging.ImageFormat]::Jpeg)
-
- $new_image=[System.Drawing.bitmap]::new($originalWidth+$leftAdd+$rightAdd,$originalHeight,[drawing.imaging.PixelFormat]::Format32bppArgb)
- $img_brush=New-Object System.Drawing.SolidBrush($colorAdd)
- $img_canvas=[System.Drawing.Graphics]::FromImage($new_image)
- $img_canvas.FillRectangle($img_brush,0,0,$new_image.width,$new_image.height)
- $img_canvas.DrawImage($originalImage,$leftAdd,0)
- $new_path = [System.IO.Path]::ChangeExtension($ImagePath, "covered.jpg")
- $new_image.Save($new_path, [System.Drawing.Imaging.ImageFormat]::Jpeg)
- }
-
- # 白色覆盖顶部100像素,底部150像素
- #$topCover = 100
- #$bottomCover = 150
-
- # 用MidnightBlue色,左边添加100像素,右边150像素
- $left_add = 100
- $right_add=150
- $color_add=[System.Drawing.Color]::MidnightBlue
-
- dir *.jpg | ForEach-Object {
- # Cover-Image -ImagePath $_.FullName -TopCover $topCover -BottomCover $bottomCover
- Cover-Image -ImagePath $_.FullName -leftAdd $left_add -rightAdd $right_add -colorAdd $color_add
- }
复制代码
作者: czjt1234 时间: 2024-5-28 20:23
回复 2# Five66
左边成功添加了,但是右边没有添加
作者: Five66 时间: 2024-5-28 21:06
回复 3# czjt1234
额,难道不是这样???
作者: czjt1234 时间: 2024-5-28 21:47
回复 4# Five66
我截屏在画图里保存为一个jpg,可以生成正确的,左右都有
链接: https://pan.baidu.com/s/1SGW_xNcEx68wXJ9uwCr7HQ?pwd=7ere 提取码: 7ere 复制这段内容后打开百度网盘手机App,操作更方便哦
但是类似的图片,试了好几个都不行,只有左边有添加
我传了一个到网盘,你可以试一下
作者: Five66 时间: 2024-5-28 23:40
回复 5# czjt1234
额,是图片dpi跟默认的96不同
2楼代码21行后添加一行- $new_image.SetResolution($originalImage.HorizontalResolution,$originalImage.VerticalResolution)
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |