The following powershell script is for downloading and converting images from PNG and TIFF format to JPG. The CSV file looks something like this.
The script is pretty simple, download files then convert them from TIF to PNG then to JPG
Here is the script
#This script downloads all the images converts them to JPG files for a website.
#first import the CSV File
$CSVFILE = Import-CSV -Path downloadImgs.csv
#Get the length of the url
$CSVFILE | ForEach-Object {$_.'Image'.length}
#Get the length of the url
$CSVFILE | ForEach-Object {$img = $_.'Image'.substring($_.'Image'.length, 5)}
#Get the file Ext
$CSVFILE | ForEach-Object {$_.'Image'.Substring(($_.'Image'.length-9), 4) }
#Save Image File List Replace the website URL with the URL you want to point to. You can use this for having a list of items downloaded or if your importing them in a bulk process
$CSVFILE | ForEach-Object { "https://yourwebsite.com/images/$location"+,-join ($_.'Image')+,".jpg"} | Out-File imageURLs.txt
#Download Image and rename the image to the Name in the CSV file
$CSVFILE | ForEach-Object { Invoke-WebRequest -Uri $_.'Image' -OutFile (-join ($_.'Name'+, $_.'Image'.Substring(($_.'Image'.length-9), 4))) }
#Download if just a JPG image hosted on a website. Name it by Name value and add jpg extension.
$CSVFILE | ForEach-Object { Invoke-WebRequest -Uri $_.'Image' -OutFile (-join ($_.'Name'+,".jpg")) }
function ConvertTIF-To-PNG
{
[cmdletbinding()]
param([Parameter(Mandatory=$true, ValueFromPipeline = $true)] $Path)
process{
if ($Path -is [string])
{ $Path = get-childitem $Path }
$Path | foreach {
$image = [System.Drawing.Image]::FromFile($($_.FullName));
$FilePath = [IO.Path]::ChangeExtension($_.FullName, '.png');
$image.Save($FilePath, [System.Drawing.Imaging.ImageFormat]::PNG);
$image.Dispose();
}
}
}
#Use function:
#Cd to directory w/ TIF files
cd .\
#Run ConvertTIF-To-JPG function
Get-ChildItem *.tif | ConvertTIF-To-PNG
#Remove TIF Files once converted
Remove-Item * -Include *.tif
#once all the tiff files are converted convert PNG to JPG
function ConvertTo-Jpg
{
[cmdletbinding()]
param([Parameter(Mandatory=$true, ValueFromPipeline = $true)] $Path)
process{
if ($Path -is [string])
{ $Path = get-childitem $Path }
$Path | foreach {
$image = [System.Drawing.Image]::FromFile($($_.FullName));
$FilePath = [IO.Path]::ChangeExtension($_.FullName, '.jpg');
$image.Save($FilePath, [System.Drawing.Imaging.ImageFormat]::JPEG);
$image.Dispose();
}
}
}
#Use function:
#Cd to directory w/ png files
cd .\
#Run ConvertTo-JPG function
Get-ChildItem *.png | ConvertTo-JPG
#REMOVE PNG FILES
Remove-Item * -Include *.png