Skip to content

Instantly share code, notes, and snippets.

@KenjiOhtsuka
Last active May 2, 2025 19:23
Show Gist options
  • Save KenjiOhtsuka/4b06bc490a5eac7f619d03a9c4c623ca to your computer and use it in GitHub Desktop.
Save KenjiOhtsuka/4b06bc490a5eac7f619d03a9c4c623ca to your computer and use it in GitHub Desktop.
extract base64 image in jupyter notebook
import json
import base64
# ipynbファイルを読み込む
with open("notebook.ipynb", "r", encoding="utf-8") as f:
notebook = json.load(f)
# セルを解析して画像データを抽出
for cell in notebook.get("cells", []):
if cell.get("cell_type") == "markdown":
for attachment_name, attachment_data in cell.get("attachments", {}).items():
# Base64データを取得
base64_data = attachment_data.get("image/png", "")
if base64_data:
# PNGファイルとして保存
with open(f"{attachment_name}.png", "wb") as img_file:
img_file.write(base64.b64decode(base64_data))
print(f"Saved: {attachment_name}.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment