Last active
May 2, 2025 19:23
-
-
Save KenjiOhtsuka/4b06bc490a5eac7f619d03a9c4c623ca to your computer and use it in GitHub Desktop.
extract base64 image in jupyter notebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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