zero

Converting JPEG to CBZ

8:39pm. I use HakuNeko to download manga. I never bothered to check the settings and ran all on the defaults. So, all manga was downloaded as .jpg files. Now I had to send manga to a friend. When it comes to sharing, .cbz or comic book archives are better. So I scribbled a small Python program to convert all the image files to comic book archives. Make sure you have the zip program installed on your Linux machine. I ran this code on Python 3.10.12, by the way. Run the command in the root folder manga, so that all chapters are stored in their own sub-directories. For example:

.
├── python_script.py
├── Chapter 1
│   ├── 01.jpg
│   ├── 02.jpg
│   ├── 03.jpg
│   ├── ...
├── Chapter 2
│   ├── 01.jpg
│   ├── 02.jpg
│   ├── 03.jpg
│   ├── ...
│   └── 18.jpg
└── Chapter 99
    ├── 01.jpg
    ├── 02.jpg
    ├── 03.jpg
    └── ...

So that should be the root folder. The script is below.

from pathlib import Path
import subprocess
import os

root = os.getcwd()
iterator = Path.cwd().iterdir()

for entry in iterator:
    if entry.is_dir():
        os.chdir(entry)
        print(f"Current working directory is {str(entry)}")
        archive = entry.name + '.cbz'
        os.system(f"zip '{archive}' *.jpg")

#blog