【问题标题】:How to add tags to image with Powershell如何使用 Powershell 为图像添加标签
【发布时间】:2018-12-19 20:23:12
【问题描述】:

我创建了一个小型 powershell 脚本来将图像从 SQL 服务器数据库(存储在 varbinary 中)提取到服务器中的文件。它完美地工作!问题是我想根据数据库中的信息向这些 .jpeg 文件添加标签。我在网上没有找到任何解决方案。那里有 Powershell 大师吗,我需要你的帮助 :-)

谢谢,

萨布丽娜

$SqlBH = "MYSQLQUERY"
# Open DB Connection 
$con = New-Object Data.SqlClient.SqlConnection;
$con.ConnectionString = "Data Source=$Server;" +
"Integrated Security=False;" +
"Initial Catalog=$Database; User ID = $uid; Password = $pwd;";
$con.Open();

# New Command and Reader
$cmd = New-Object Data.SqlClient.SqlCommand $SqlBH, $con;
$rd = $cmd.ExecuteReader();

# Create a byte array for the stream.
$out = [array]::CreateInstance('Byte', $bufferSize)

# Looping through records
While ($rd.Read())
{
Write-Output ("Exporting file: {0} ParentID: {1}" -f 
$rd.GetString(0),$rd.GetString(2));
# New BinaryWriter

# Create parent folder if not present
$fl = $rd.GetString(2)
$Destfl = $Dest + $fl
$FileExists = Test-Path $Destfl
If ($FileExists -eq $False) {new-item $Destfl -itemtype directory}

$fs = New-Object System.IO.FileStream ($Destfl + $rd.GetString(0)), Create, 
Write;
$bw = New-Object System.IO.BinaryWriter $fs;

$start = 0;
$received = $rd.GetBytes(1, $start, $out, 0, $bufferSize - 1);
While ($received -gt 0)
{
$bw.Write($out, 0, $received);
$bw.Flush();
$start += $received;
$received = $rd.GetBytes(1, $start, $out, 0, $bufferSize - 1);
}

$bw.Close();
$fs.Close();
}

# Closing & Disposing all objects
$fs.Dispose();
$rd.Close();
$cmd.Dispose();
$con.Close();

Write-Output ("Finished");

【问题讨论】:

  • 请提供 powershell 代码,以便其他人可以查看并为您提供答案。
  • 用你的代码编辑你的问题,请不要在评论中粘贴

标签: image powershell tags


【解决方案1】:

已经有些挣扎,我已经实施了以下解决方案。我希望它可以在某个时候帮助某人:-)

基于库 Exiv 2 (http://www.exiv2.org/),我在第 70 行 ($fs.Close();) 之后添加了以下代码:

# Tag images
$cmd_exe '.\exiv2.exe'
$arg1 = '-M'
$arg2_BH = """add Iptc.Application2.Keywords String " + $rd.GetString(3) + """"
$arg2_LSD = """add Iptc.Application2.Keywords String " + $rd.GetString(4) + """"
$arg2_logger = """add Iptc.Application2.Keywords String " + $rd.GetString(5) + """"
$arg3 = $Destfl + $rd.GetString(0)

& $cmd_exe $arg1 $arg2_BH $arg1 $arg2_LSD $arg1 $arg2_logger $arg3   

就是这样:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-02
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多