image - 如何使用 Powershell 为图像添加标签

标签 image powershell tags

我创建了一个小型 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");

最佳答案

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

基于库 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   

就是这样:-)

关于image - 如何使用 Powershell 为图像添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51291487/

相关文章:

azure - 如何通过 Azure 中的 PowerShell 获取订阅子资源的 PIM 角色分配?

javascript - 如何在没有网络服务器的情况下获取外部 JavaScript 的文本(即 AJAX)

SQL 选择一个只有 child 的父级

javascript - 在位置可变的图像上创建带有文本叠加的 html

powershell - 如何通过 Powershell 设置 IIS 自定义错误?

c++ - 我可以使用 slider 更改屏幕上显示的图像吗?

arrays - Powershell 根据元素的数量返回不同的数据类型

html - 在 HTML 页面的另一个链接项中创建链接

android - 将图像加载到位图对象时出现奇怪的 OutOfMemory 问题

html - SDTT : "A value for the image field is required"