Home > Enterprise >  How to convert an images folder into CBZ (like comics or mangas for Ebook)
How to convert an images folder into CBZ (like comics or mangas for Ebook)

Time:10-06

I just want to know how to convert an images folder into a CBZ file READABLE for my Ebook (I checked the ebook and she can read this format).

Optimally, I would like to convert it without having to install everything. Just a script.

For those who are fast, I already answered my question... Just sharing it.

CodePudding user response:

GO SEE UPDATE PART

Assuming your OS is Windows, we can do it with Batch or PowerShell.

For this one its process is quite easy, we just need to understand that a CBZ file IS a ZIP file with images in it. So we will just:

  1. zip with 7zip because for some reasons the files converted with WinRAR didn't worked in my Ebook (wasn't even in the library) ;
  2. Change the extension from .zip to .cbz.

I'm only going to show the PowerShell one because the .bat script had known issues.


Architecture

Architecture of the directory

The architecture should be:

  1. My first folder
    • first image
    • second image
  2. My second folder
    • first image
    • second image

PowerShell

Here's the code from my "#_ImagesFolderToCBZ.ps1"

Clear-Host

# INPUT - Script folder path
$i = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
# 7Zip path
$7zipPath = "$env:ProgramFiles\7-Zip\7z.exe"
# A list of the path of the zipped files
$listOfZippedFiles = New-Object Collections.Generic.List[String]
$listOfZippedNames = New-Object Collections.Generic.List[String]

# Ask the user if we delete the folders after their conversion
$isSdel = Read-Host -Prompt "Do you want to delete the folders after conversion [Y/N]: "

# Get the files inside the INPUT path and forEach children
Get-ChildItem "$i" | ForEach-Object {
    # Get the full path of the file
    $pathFolder = $_.FullName
    
    # If the path here is a folder
    if (Test-Path -Path "$pathFolder" -PathType Container) {
        # Set the alias
        Set-Alias 7z $7zipPath

        # If the user asked for deletion of folders
        if ("Y" -eq $isSdel.ToUpper()) {
            # Zip the content of the folder
            7z a "$pathFolder.zip" "$pathFolder\*" -sdel | FIND "ing archive"
        }
        else {
            # Zip the content of the folder
            7z a "$pathFolder.zip" "$pathFolder\*" | FIND "ing archive"
        }

        # Add the file name into the list
        $listOfZippedFiles.Add("$pathFolder.zip")
        $listOfZippedNames.Add("$_.zip")
    }

    # If the user asked for deletion of folders
    if ("Y" -eq $isSdel) {
        # Remove the now blank folders
        if( $_.psiscontainer -eq $true){
            if((gci $_.FullName) -eq $null){
                $_.FullName | Remove-Item -Force
            }
        }
    }
}

# For each zipped file
foreach ($file in $listOfZippedFiles) {
    # Change the extension to CBZ
    $dest = [System.IO.Path]::ChangeExtension("$file",".cbz")
    Move-Item -Path "$file" -Destination $dest -Force
}

# Write for the user
Write-Host "`r`n`r`nConverted:"

# Displaying the converted files by their names
foreach ($file in $listOfZippedNames) {
    $newName = [System.IO.Path]::ChangeExtension("$file",".cbz")
    Write-Host "-- $newName"
}

# Blank line
Write-Host ""
# Pause to let us see the result
Pause

Output

output

As we can see, the folder is sucessfully created AND without loops like : I have ZIP files in the root folder of the script and they are also renamed into CBZ ones (I had this loop for my batch script).

I also added the choice to automatically delete the converted folders OR not.

Obviously, there's room for improvements (especially in how we delete the folders). I'll gladly take any advice.


UPDATE

I updated my script and it's much better. Less instructions, a list (in the prompt) that update itself when each folder is really converted. So no more: 1) ZIP all folders 2) rename their extension.

So a code that's more logic and also useful to show a beautiful process in real time.

Here's the updated code :

Clear-Host

# ROOT - Script folder path
$root = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
# 7Zip path
$7zipPath = "$env:ProgramFiles\7-Zip\7z.exe"

# Test if 7zip is installed
if (-not (Test-Path -Path $7zipPath -PathType Leaf)) {
    throw "7 zip file '$7zipPath' not found"
}

# Ask the user if we delete the folders after their conversion
$isSdel = Read-Host -Prompt "Do you want to delete the folders after conversion [Y/N]: "

# Write for the user
Write-Host "`r`nConverted:"

# Get the files inside the INPUT path and forEach children
Get-ChildItem "$root" | ForEach-Object {
    # Get the full path of the file
    $pathFolder = $_.FullName
    
    # If the path here is a folder
    if (Test-Path -Path "$pathFolder" -PathType Container) {
        # If the user asked for deletion of folders
        if ("Y" -eq $isSdel.ToUpper()) {
            # Zip the content of the folder while deleting the files zipped
            & $7zipPath a "$pathFolder.zip" "$pathFolder\*" -sdel > $null

            # Remove the now blank folder
            if( $_.psiscontainer -eq $true){
                if((gci $_.FullName) -eq $null){
                    $_.FullName | Remove-Item -Force
                }
            }
        }
        else {
            # Zip the content of the folder
            & $7zipPath a "$pathFolder.zip" "$pathFolder\*" > $null
        }

        # Change the extension to CBZ
        $newName = [System.IO.Path]::ChangeExtension("$pathFolder.zip",".cbz")
        Move-Item -Path "$pathFolder.zip" -Destination $newName -Force

        # Tells the user this one is finished converting
        Write-Host "--" -ForegroundColor DarkGray -NoNewline
        Write-Host " $_.cbz"
    }
}

# Tells the user it's finished
Write-Host "`r`nFinished`r`n" -ForegroundColor Green

# Pause to let us see the result
Pause

UPDATE 2

I made a GitHub project for this one. The URL is here: https://github.com/PonyLucky/CBZ-Manga-Creator.

  • Related