Code Monkey home page Code Monkey logo

pictureexifoptimizer's Introduction

PictureExifOptimizer

Requirements

Directory to work

C:.
│  exiftool.exe
│
├─1-1_movie_escaping
│      exiftool.exe
│
├─1-2_create_date_setting
│      exiftool.exe
│
└─5_original_files

Workflow of product

Ops 1: Ready.

事前準備

cd C:\Users\%username%\Downloads

mkdir picture_backup & mkdir picture_backup\1-2_create_date_setting & mkdir picture_backup\5_original_files
cd picture_backup

自分のスマホで撮ったファイル(①)とそれ以外(②)に分ける

  • ①は、「C:\Users\${username}\Downloads\picture_backup」に入れる。

②のオペレーション

  • ②は、「C:\Users\${username}\Downloads\picture_backup\1-2_create_date_setting」に入れる。
start powershell
  • mp4ファイルなどを退避させる。(③になる)
Move-Item -Path *.mp4 -Destination .\1-1_movie_escaping
  • ②に対してSetting createDate with my FileCreateDate.
cd 1-2_create_date_setting; $username = (Get-ChildItem Env:\USERNAME).Value; $toCreateDateDir = "1-2_create_date_setting"; $folderDir = "C:\Users\${username}\Downloads\picture_backup\${toCreateDateDir}"; $proc = Start-Process -FilePath "${folderDir}\exiftool" -ArgumentList "-CreateDate<FileCreateDate","-d","%Y:%m:%d:%H:%M:%S",$folderDir -NoNewWindow -PassThru -wait; Write-Host $proc.ExitCode;
  • ③に対してSetting createDate with my FileModifyDate.
cd ..; cd 1-1_movie_escaping; $username = (Get-ChildItem Env:\USERNAME).Value; $toCreateDateDir = "1-1_movie_escaping"; $folderDir = "C:\Users\${username}\Downloads\picture_backup\${toCreateDateDir}"; $proc = Start-Process -FilePath "${folderDir}\exiftool" -ArgumentList "-CreateDate<FileModifyDate","-d","%Y:%m:%d:%H:%M:%S",$folderDir -NoNewWindow -PassThru -wait; Write-Host $proc.ExitCode;

Ops 2: Renaming file with its Exif createDate.

③のmp4ファイルの格納場所をこのコマンドで戻す。

Move-Item -Path .\*.mp4 -Destination ..;
cd ..

①の画像ファイルと③の動画ファイルを、Renaming file with its Exif createDate.

$username = (Get-ChildItem Env:\USERNAME).Value; $folderDir = "C:\Users\${username}\Downloads\picture_backup"; $proc = Start-Process -FilePath "${folderDir}\exiftool" -ArgumentList "-FileName<CreateDate","-d","%Y%m%d%H%M%S.%%e",$folderDir -NoNewWindow -PassThru -wait; Write-Host $proc.ExitCode;

②のjpegとwebpを「C:\Users\${username}\Downloads\picture_backup」に移動する。

cd .\1-2_create_date_setting
Move-Item -Path *.jpg -Destination ..;
Move-Item -Path *.webp -Destination ..;
cd ..

数字重複エラーでリネーム出来なかったやつに対して、Exif情報を付加して、更に自動採番リネームを行う。

$number = 1
$date_formatted = Get-Date -Format "yyyyMMddHH"
$year_this = Get-Date -UFormat "%Y"
$year_last = Get-Date (Get-Date).AddYears(-1) -UFormat "%Y"
$is_correct = $false

function getUnitOfDatetime([string]$numStr){
    $num = [int]$numStr;
    return [string]$num
}

function getZeroPaddedUnit4([string]$numStr){
    return [string]"{0:0000}" -f [int]$numStr
}

function getZeroPaddedUnit2([string]$numStr){
    return [string]"{0:00}" -f [int]$numStr
}

while($is_correct -eq $false){
  $date_formatted = Read-Host "Input prefix of pictures (yyyyMMddHH)"
	if($date_formatted.length -eq 10 -and $date_formatted -like "${year_this}*" -or $date_formatted -like "${year_last}*"){
		$is_correct = $true
	}

  $y = getZeroPaddedUnit4(getUnitOfDatetime($date_formatted.Substring(0, 4)));
  $m = getZeroPaddedUnit2(getUnitOfDatetime($date_formatted.Substring(4, 2)));
  $d = getZeroPaddedUnit2(getUnitOfDatetime($date_formatted.Substring(6, 2)));
  $h = getZeroPaddedUnit2(getUnitOfDatetime($date_formatted.Substring(8, 2)));
  $newCreateDate = "{0}-{1}-{2} {3}:00:00" -f $y, $m, $d, $h;
  Write-Host $newCreateDate;

}
Get-ChildItem -Path . -File | ForEach-Object {
  if($_ -notlike "${year_this}*" -and $_ -notlike "${year_last}*" -and $_ -notlike "*.exe" -and $_ -isnot [System.IO.DirectoryInfo]){
		Write-Host "LOG-INFO" $_.GetType() "true" $_.Name
    $jpgFilePath = $_.FullName;

		./exiftool.exe -AllDates="$newCreateDate" "$jpgFilePath"
    $number_zeropadded = "{0:0000}" -f $number
		$ext = [System.IO.Path]::GetExtension($_)
		$newName = "${date_formatted}${number_zeropadded}${ext}"
		Write-Host "LOG-INFO" $newName
		Rename-Item -Path ".\${_}" -NewName $newName
		$number++
  }else{
		Write-Host "LOG-INFO" $_.GetType() "false" $_.Name
	}
}
Write-Output("Exif info of {0} images are fixed with prompted dates." -f [string]($number-1));

Ops 3: Converting JPEG to WEBP.

①と②のjpegと③が合流したな。

作業を続ける。

全部webpにする。

Cwebpでwebpにする場合の手順

まずは、Scoopをインストールして、libwebpをインストールする。

cd ..\..\..\..\ # Change to "C:\" directory.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression
cd C:\Users\%USERNAME%\Downloads\picture_backup
scoop update
scoop install libwebp

そして、全部Webpにする。(Jpegはまだ消さない。)

$TargetFolder = "."; if ($null -ne $Args[0]) {$TargetFolder = $Args[0];}; $Files = (Get-ChildItem $TargetFolder -Name -File -Include "*.jpg","*.png"); $updatedCount = 0; foreach ($File in $Files) {$FullName = "$TargetFolder/$File"; $Extension = (Get-Item $FullName).Extension; $GenerateFileName = "$($FullName.Substring(0,$FullName.Length-$Extension.Length)).webp"; Write-Output "<Source File Name: $File>"; cwebp -preset photo -metadata icc -sharp_yuv -q 75 -o $GenerateFileName -progress -short $FullName; $updatedCount++; Write-Output "----------------"; }; Write-Output("{0} .webp images converted." -f $updatedCount);
  • 全部「C:\Users\${username}\Downloads\picture_backup」に入れる。

Ops 4: Copying Exif info from other file.

①と②と③が合流したな。

  • 全部、Copying Exif info from other file.
$updatedCount = 0; Get-ChildItem *.webp | ForEach-Object {$jpg = $_.FullName.Replace(".webp", ".jpg"); if(Test-Path $jpg){ $webpFilePath = $_.FullName;     Write-Host $jpg; Write-Host $webpFilePath; Start-Process -FilePath "./exiftool.exe" -ArgumentList "-tagsFromFile",$jpg,"-exif:all",$webpFilePath -NoNewWindow -Wait; $updatedCount++; }}; Write-Output("Exif info of {0} .webp images are fixed with .jpg images." -f $updatedCount);

Ops 5: Escapng original files.

  • コピー元のoriginalファイルを「C:\Users\${username}\Downloads\picture_backup\original_files」移動する。
Move-Item -Path .\*_original -Destination .\5_original_files;
Move-Item -Path .\*.jpg -Destination .\5_original_files;
Move-Item -Path .\*.png -Destination .\5_original_files;

Ops 6: Upload to Google Photos !!

  • 全部、Googleフォトにアップロードして終わり。アルバムに振り分ける。

Prepared command 01

exiftool -s .\img.webp

Prepared command 02

Execute the batch file: Z1-1_rename_vlcsnap_files.bat.

Change log

Date Topics
2024-02-20 Initial commit.
2024-06-14 Apply Cwebp to convert to .webp files.
2024-06-16 Fix bugs and Edit exif with prompted date.
2024-06-19 Apply the function to rename the files captured with VLC media player.

pictureexifoptimizer's People

Contributors

landmaster135 avatar

Watchers

 avatar

pictureexifoptimizer's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.