luxi78 / joplin2obsidian

Making the move from Joplin to Obsidian
122 stars 13 forks source link

Images Not Imported #11

Open t3chn0t3s opened 2 years ago

t3chn0t3s commented 2 years ago

Greetings,

The migration/conversion tool worked really well. I found that the screenshots/images found in my Joplin notes do not appear in Obsidian. Instead in obsidian I see something that says '"abcd.png" is not created. Click to create.'. I just wanted to confirm that its normal to see that. See example below.

image

I am using Obsidian in Ubuntu.

t3chn0t3s commented 2 years ago

I did some testing in Obsidian to understand where the images are placed. I just moved the images from the resources folder to the correct folder in Obsidian and it corrected the issue. Really great tool. It saved me a lot of time. Thank you!

nodauf commented 2 years ago

I used the tool with joplin 2.8.8 and obsidian 0.15.9 and it works fine. I had issues with internal resources other than images (pdf, ...) but it should be fixed with the PR #12

lyrek commented 1 year ago

For anyone else who is dealing with an image problem that I ran into, I have produced a python script that will fix it. This is for the case where after importing Joplin notes into Obsidian, the image doesn't appear because it was resized in Joplin, and where it has a format like so after importing into Obsidian:

<img src=":/c729aaacb8244e12aee5f53539a11ced" alt="39ed6a48f8c90d3d85cecb6c8f0af7bd.png" width="377" height="298" class="jop-noMdConv">

For some reason, these image lines don't seem to render in Obsidian. To fix this, the script replaces an example like that with a line like so: ![[c729aaacb8244e12aee5f53539a11ced.png]]. This is Obsidian's "wikilink" format that recursively searches for a file within the vault and links to it.

Here is the python script. Make sure to change the path on the 4th line to the path of your local Obsidian vault. Run the script once and it should sufficiently replace all of the broken image links with the correct format one for Obsidian.

Backup your notes just in-case before running this! Hope it also helps someone else out there.

import os
import re

obsidian_vault_path = '/home/admin/Downloads/ObsidianVault'

def convert_img_tag(match):
    img_src = match.group(1)
    src_ext = match.group(2)
    alt_ext = match.group(3)

    file_ext = src_ext or alt_ext

    if file_ext:
        output = f"![[{img_src}.{file_ext}]]"
        print(f"Converting image: {img_src}.{file_ext}")
    else:
        output = f"![[{img_src}]]"
        print(f"Cannot determine file format for image: {img_src}")

    # Remove double periods if they occur
    output = output.replace('..', '.')

    return output

def process_file(file_path):
    with open(file_path, 'r') as file:
        content = file.read()

    updated_content = re.sub(r'<img.*?src=".*?/(.*?)(\..*?)?".*?alt=".*?(?:\.(.*?))?".*?>', convert_img_tag, content)

    if content != updated_content:
        with open(file_path, 'w') as file:
            file.write(updated_content)

def process_directory(directory):
    for root, _, files in os.walk(directory):
        for file in files:
            if file.endswith('.md'):
                file_path = os.path.join(root, file)
                process_file(file_path)

process_directory(obsidian_vault_path)
print("Finished processing all files in the Obsidian vault.")
T4erg commented 1 year ago

My Joplin image format is different from you, So I have made some tweak to make it work. My environment and code as follows:

joplin info:

Joplin 2.9.17 (prod, darwin)

Profile Version: 41
Keychain Supported: Yes

Revision: a84a8e7

obsidian info:

About Obsidian
Version 1.1.16 (Installer 0.15.9)

Python3 script:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author  : t4erg
# @E-mail  : T4erg#foxmail.com
# @File    : joplin_to_obsidian_image_format.py
# @Time    : 2023/4/18 12:16

"""
converto joplin image format to obsidian format:

<img width="590" height="44" src="./../../_resources/cos-file-url_url_https_3A_2F_2Fk_da51977f1a8847be9.png"/>
---->
![[cos-file-url_url_https_3A_2F_2Fk_da51977f1a8847be9.png]]

<img src=":/f04217aa3aa94cd3aa86de3f355ff09e.jpg"/>
---->
![[f04217aa3aa94cd3aa86de3f355ff09e.jpg]]

"""

import os
import re

obsidian_vault_path = '/Users/t4erg/Documents/ObsidianVault'

def convert_img_tag(match):
    img_src = match.group("src")
    output = f"![[{img_src}]]"
    return output

def process_file(file_path):
    with open(file_path, 'r') as file:
        content = file.read()
    updated_content = re.sub(r'<img.*src=".*/(?P<src>.*)"/>', convert_img_tag, content, flags=re.IGNORECASE)

    if content != updated_content:
        with open(file_path, 'w') as file:
            file.write(updated_content)

def process_directory(directory):
    for root, _, files in os.walk(directory):
        for file in files:
            if file.endswith('.md'):
                file_path = os.path.join(root, file)
                process_file(file_path)

process_directory(obsidian_vault_path)
print("Finished processing all files in the Obsidian vault.")