スキップしてメイン コンテンツに移動

EPUBからJPEG

EPUBの中のJPEGのファイル名が連番でないのでMCOMIXで読みにくい問題を解決するべく ChatGptがだしたパイソンコード
epubリーダーの見開き設定とか思うようにできなかったので
epubを解凍してjpegのファイルを連番に書き換えるコード 以下のフォルダの構成が前提

manga/ (manga.epub)
├ OLEBPS/
└ META-INF/

-----以下コード---------

import os
import glob
import re
import zipfile
import sys
import shutil  # フォルダ削除用

# コマンドライン引数からEPUBファイル名を取得
if len(sys.argv) < 2:
    print("❌ 使用方法: python3 rename_images.py <epubファイル名>")
    sys.exit(1)

EPUB_FILE = sys.argv[1]

# 出力ディレクトリ(EPUBファイル名からフォルダ名を作成)
EXTRACT_DIR = os.path.splitext(EPUB_FILE)[0]  # 例: "manga.epub" → "manga"

# ① EPUBを解凍
print(f"📖 EPUBを解凍中: {EPUB_FILE} → {EXTRACT_DIR}/")
os.makedirs(EXTRACT_DIR, exist_ok=True)

with zipfile.ZipFile(EPUB_FILE, 'r') as zip_ref:
    zip_ref.extractall(EXTRACT_DIR)

# ② OEBPSフォルダを探す(デフォルト: OEBPS, なければ他をチェック)
oebps_path = os.path.join(EXTRACT_DIR, "OEBPS")
if not os.path.exists(oebps_path):
    possible_dirs = glob.glob(os.path.join(EXTRACT_DIR, "*"))
    for d in possible_dirs:
        if os.path.isdir(d) and any(f.startswith("part") for f in os.listdir(d)):  
            oebps_path = d
            break

if not os.path.exists(oebps_path):
    print("❌ OEBPSフォルダが見つかりません。")
    sys.exit(1)

# ③ XHTMLファイルを取得(ソート)
xhtml_files = sorted(glob.glob(os.path.join(oebps_path, "part*.xhtml")))

# ④ 画像ファイルをXHTMLの記述から抽出 & リネーム
print("🖼 画像をリネーム中...")
for i, xhtml_file in enumerate(xhtml_files, start=1):
    with open(xhtml_file, "r", encoding="utf-8") as file:
        content = file.read()

    match = re.search(r'<img src="(.*?)"', content)
    if match:
        img_file = match.group(1)
        img_path = os.path.join(oebps_path, img_file)
        new_name = os.path.join(EXTRACT_DIR, f"{i:04}.jpg")

        if os.path.exists(img_path):
            os.rename(img_path, new_name)
            print(f"✔ {img_file} → {new_name}")

# ⑤ OEBPS & META-INF を削除
print("🗑 不要なフォルダを削除中...")
for folder in ["OEBPS", "META-INF"]:
    folder_path = os.path.join(EXTRACT_DIR, folder)
    if os.path.exists(folder_path):
        shutil.rmtree(folder_path)
        print(f"🗑 削除: {folder_path}")

print("✅ すべての画像をリネーム & 不要フォルダを削除しました!")
print(f"📂 画像が保存されたフォルダ: {EXTRACT_DIR}/")



以上のコードをrename_images.py で保存してepubファイル(複数可)があるフォルダに置いて
python3 rename_images.py manga.epub
を実行

コメント

このブログの人気の投稿

Arduino IDE が "Downloading index: library_index.tar.bz2" で固まる問題

PCとのシリアル通信が原因の一つらしい '/home/usename/.arduino15/packages' を消すといいらしい ので消すと治った IDEの起動中にフリーズしてたのが治った Downloading index: library_index.tar.bz2 とダウンロード中だったが終了したので起動中のフリーズが起こるようになった

Blogger でp5jsがつかえた

”HTMLビュー”でHTMLを編集  divとcanvasを関連付ければ良いみたい (div id="p5canvas とcreateCanvasの.parent("p5canvas");) p5js本体はCDNを参照(https://cdnjs.com/libraries/p5.js) コードはP5サイトのEXAMPLEから(https://p5js.org/examples/3d-geometries.html) <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.5.0/p5.js"></script> <div id="p5canvas"></div> <script> function setup() { createCanvas(710, 400, WEBGL).parent("p5canvas"); } function draw() { background(250); translate(-240, -100, 0); normalMaterial(); push(); rotateZ(frameCount * 0.01); rotateX(frameCount * 0.01); rotateY(frameCount * 0.01); plane(70); pop(); translate(240, 0, 0); push(); rotateZ(frameCount * 0.01); rotateX(frameCount * 0.01); rotateY(frameCount * 0.01); box(70, 70, 70); pop(); translate(240, 0, 0); push(); rotateZ(frameCount * 0.01); rotateX(frameCount * 0.01); rotateY(frameCount * 0.01); cylinder(70, 70); pop(); ...

クラスカル法

chat-gptにきいたらおしえてくれた                                                                参考動画 <!DOCTYPE html> <html lang="en"> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.5.0/p5.js"></script> <div id="p5canvas"></div> <script> class Graph {   constructor() {     this.vertices = [];     this.edges = [];   }   addVertex(x, y) {     this.vertices.push({ x, y });   }   addEdge(source, destination, weight) {     if (this.vertices[source] && this.vertices[destination]) ...