kerrickstaley / genanki

A Python 3 library for generating Anki decks
MIT License
1.99k stars 150 forks source link

Can't load media in Anki 2.1.35 #59

Closed julen3 closed 3 years ago

julen3 commented 3 years ago

When creating a new deck with a set of new cards, running the line my_package.media_files = media_list, and importing the .apkg file to Anki the media names do appear in the cards fields, but neither the images are shown or the sound is being played, which seems to indicate that my_package.media_files is not doing its job (here is a sample code that supposedly should work that I also tried: https://gist.github.com/jlumbroso/e17abff02d89be04240072191af09ab2)

It took me a while to realize why my new Anki decks could not show the media files that I wanted to, until I ended up changing my_package.media_files = media_list with the following function, where media_path is the folder where Anki stores all the media (C:\Users\user\AppData\Roaming\Anki2\User 1\collection.media)

def move_media(media_list, media_path): for file in media_list: ...destination = media_path + '\\' + os.path.basename(file) ...shutil.move(file, destination)

kerrickstaley commented 3 years ago

Is it possible to provide the .apkg file that is generated by your program (upload it on Google Drive, Dropbox, etc.)? Or if it's private, you could generate a minimal example and upload that.

Media files are supposed to be embedded in the .apkg file with names 0, 1, etc. .apkg files are just zip files, so you can change the extension from .apkg to .zip and open it to see what's getting generated.

If you run the sample code you linked, there should be three files in output.apkg: collection.anki2, media, and 0. If you rename 0 to 0.jpg and open it, it should be the same image as format.jpg. If you rename media to media.txt and open it, it should say {"0": "format.jpg"}.

This issue might be happening because of some difference between Windows and Linux.

woodsonchilduniverse commented 3 years ago

I meet the same question.And use same method posted above can solve this question.I check the media file and media it is all right.Can I give my apkg file to you.By the way the image original name is "0.png".My media is below: {"0": "1.png", "1": "2.png", "2": "3.png", "3": "4.png", "4": "5.png", "5": "6.png", "6": "7.png", "7": "8.png", "8": "9.png", "9": "10.png", "10": "11.png", "11": "12.png", "12": "13.png", "13": "14.png", "14": "15.png", "15": "16.png", "16": "17.png", "17": "18.png", "18": "19.png", "19": "20.png", "20": "21.png"}

kerrickstaley commented 3 years ago

@woodsonchilduniverse I can't fix this issue based on the information you've provided. Can you please provide a detailed bug report? Specifically, you need to give the steps to reproduce the bug. See this blog post.

woodsonchilduniverse commented 3 years ago

Environment : Python 3.6.6 genanki 0.10 (I used pip to update it today) Windows Problem : image cannot show on the anki The apkg file:1the picture are in the file 2the media which I open in notebook is right.And I post its context below. {"0": "21.png", "1": "1.png", "2": "2.png", "3": "3.png", "4": "4.png", "5": "5.png", "6": "6.png", "7": "7.png", "8": "8.png", "9": "9.png", "10": "10.png", "11": "11.png", "12": "12.png", "13": "13.png", "14": "14.png", "15": "15.png", "16": "16.png", "17": "17.png", "18": "18.png", "19": "19.png", "20": "20.png"} My code is below.

pictureList =['D:\\Desktop\\xyz\\word\\media\\21.png', 'D:\\Desktop\\xyz\\word\\media\\1.png', 'D:\\Desktop\\xyz\\word\\media\\2.png', 'D:\\Desktop\\xyz\\word\\media\\3.png', 'D:\\Desktop\\xyz\\word\\media\\4.png', 'D:\\Desktop\\xyz\\word\\media\\5.png', 'D:\\Desktop\\xyz\\word\\media\\6.png', 'D:\\Desktop\\xyz\\word\\media\\7.png', 'D:\\Desktop\\xyz\\word\\media\\8.png', 'D:\\Desktop\\xyz\\word\\media\\9.png', 'D:\\Desktop\\xyz\\word\\media\\10.png', 'D:\\Desktop\\xyz\\word\\media\\11.png', 'D:\\Desktop\\xyz\\word\\media\\12.png', 'D:\\Desktop\\xyz\\word\\media\\13.png', 'D:\\Desktop\\xyz\\word\\media\\14.png', 'D:\\Desktop\\xyz\\word\\media\\15.png', 'D:\\Desktop\\xyz\\word\\media\\16.png', 'D:\\Desktop\\xyz\\word\\media\\17.png', 'D:\\Desktop\\xyz\\word\\media\\18.png', 'D:\\Desktop\\xyz\\word\\media\\19.png', 'D:\\Desktop\\xyz\\word\\media\\20.png']

def makeApkg(List):
    #Using a list to generate a apkg.file
    my_model = genanki.Model(
    1091735104,
    'Simple Model with Media',
    fields=[
    {'name': 'Question'},
    {'name': 'Answer'},
    {'name': 'MyMedia'},
    {'name': 'MyAnswerMedia'}       # ADD THIS
    ],
    templates=[
    {
    'name': 'Card 1',
    'qfmt': '{{Question}}<br>{{MyMedia}}',              # AND THIS
    'afmt': '{{FrontSide}}<hr id="answer">{{MyAnswerMedia}}<br>{{Answer}}',
    },
    ])
    deckname = "newDecks"
    decknumber = 98765675
    my_deck = genanki.Deck(
    decknumber,
    deckname)

    mediaFile = r"D:\Desktop\xyz\word\media"
    #The pictures are in this file.
    imageList = []
    #anki media files

    MyMedia = ""
    MyAnswerMedia = ""

    for picture in List:
        MyMedia += '<img src = ' + '"' + os.path.basename(picture) + '"' + '/>'
        MyAnswerMedia += '<br>'+ '<img src = ' + '"' + os.path.basename(picture) + '"' + '/>'
        question = "sdfg"
        answer = "kjhgh"
        # two randon strings

        imageList.append(picture)
        my_note=genanki.Note(
        model=my_model,
        fields=[question, answer, MyMedia, MyAnswerMedia])
        my_deck.add_note(my_note)

    my_package = genanki.Package(my_deck)
    my_package.media_files = imageList
    my_package.write_to_file(deckname + r".apkg")
makeApkg(pictureList)

Here is my apkg file.I rename it to upload. newDecks.zip

By the way thanks for your article and your package,Both help me greatly.It is really a fantastic article.

remiberthoz commented 3 years ago

Hey, @woodsonchilduniverse

You really need to help us if you want the problem solved.

First of all, use Markdown to properly format and highlight your code (see the GitHub doc for how-to) and use the Preview feature to make sure it looks good before posting.

Then, you need to provide a complete example of what you are doing. In the above comment, you are showing a python function named makeApkg that has one List parameter. Presumably you call this function with a parameter, and we need to see what its value is.

We will happily help you, but we cannot if we have to guess what you doing. See this StackOverflow post about Minimal, Reproducible Example.

Thanks

woodsonchilduniverse commented 3 years ago

Hey, @woodsonchilduniverse

You really need to help us if you want the problem solved.

First of all, use Markdown to properly format and highlight your code (see the GitHub doc for how-to) and use the Preview feature to make sure it looks good before posting.

Then, you need to provide a complete example of what you are doing. In the above comment, you are showing a python function named makeApkg that has one List parameter. Presumably you call this function with a parameter, and we need to see what its value is.

We will happily help you, but we cannot if we have to guess what you doing. See this StackOverflow post about Minimal, Reproducible Example.

Thanks

I update my comment.Is it clear?And I am sorry for my terrible comment and thanks for your reply .

remiberthoz commented 3 years ago

Thanks for updating your comment!

With your complete code, and by comparing to mine, I narrowed down the problelm to a strange behavior. It seems that the images are not properly displayed in Anki when ones leaves a space around the = signs when giving the src of HTML image tags. What I mean is that:

This works:         <img src="1.png"/>
This does NOT work: <img src = "1.png"/>

So you can fix your issue by repacing:

MyMedia = '<img src = ' + '"' + os.path.basename(picture) + '"' + '/>'

with (remove spaces around HTML = sign):

MyMedia = '<img src=' + '"' + os.path.basename(picture) + '"' + '/>'

It is very strange because if you modify the line for MyMedia only, then the image that appears in the answer (with MyAnswerMedia) will be fine. I would guess that this is not a genanki issue but a problem in Anki itself. Have you tested if the same problem appears on AnkiDroid?

woodsonchilduniverse commented 3 years ago

Thanks for updating your comment!

With your complete code, and by comparing to mine, I narrowed down the problelm to a strange behavior. It seems that the images are not properly displayed in Anki when ones leaves a space around the = signs when giving the src of HTML image tags. What I mean is that:

This works:         <img src="1.png"/>
This does NOT work: <img src = "1.png"/>

So you can fix your issue by repacing:

MyMedia = '<img src = ' + '"' + os.path.basename(picture) + '"' + '/>'

with (remove spaces around HTML = sign):

MyMedia = '<img src=' + '"' + os.path.basename(picture) + '"' + '/>'

It is very strange because if you modify the line for MyMedia only, then the image that appears in the answer (with MyAnswerMedia) will be fine. I would guess that this is not a genanki issue but a problem in Anki itself. Have you tested if the same problem appears on AnkiDroid?

Thanks for your help greatly.Yes,I import my anki file into my ipad.It has the same problem.In my case I only change MyMedia and import it into my ipad.The 'MyAnswerMedia' doesn't change the answer field doesn't display image correctly.And I am curious how can you find and fix this bug?You are so awesome.

remiberthoz commented 3 years ago

You're welcome. Can you confirm that your script is now correctly importing the media if you modify the two fields (MyMedia and MyAnswerMedia)? If everything is fine you can close the issue.


To figure it out, I observed that the script you posted did not work, but the one I use in my anki deck works. So I simply replaced the part of your code that imports media with my code.

This solved the problem, so I knew that the issue comes from one of the differences. I checked them one by one, and stumbled upon the spaces around the equal sign.


@kerrickstaley, you may want to rename the issue for future reference.,

woodsonchilduniverse commented 3 years ago

You're welcome. Can you confirm that your script is now correctly importing the media if you modify the two fields (MyMedia and MyAnswerMedia)? If everything is fine you can close the issue.

To figure it out, I observed that the script you posted did not work, but the one I use in my anki deck works. So I simply replaced the part of your code that imports media with my code.

This solved the problem, so I knew that the issue comes from one of the differences. I checked them one by one, and stumbled upon the spaces around the equal sign.

@kerrickstaley, you may want to rename the issue for future reference.,

Thanks a lot.My script works well.However I can not close this issue.Just like you I am a commenter of this issue.I have the same problem as origional poster.

kerrickstaley commented 3 years ago

Thanks for the help @remiberthoz !!