Skip to content

Instantly share code, notes, and snippets.

@ChrisK91
Last active April 30, 2019 17:42
Show Gist options
  • Select an option

  • Save ChrisK91/99444f9e3ca4d850d6dbbd59e0add4bf to your computer and use it in GitHub Desktop.

Select an option

Save ChrisK91/99444f9e3ca4d850d6dbbd59e0add4bf to your computer and use it in GitHub Desktop.
Edit images externally in Windows
# -*- mode: Python ; coding: utf-8 -*-
# 2018-04-12, V1.01 Win
# Based on "Edit Images Externally, Updated (Mac OSx)"
# Original addon: https://ankiweb.net/shared/info/1575330182
#
# This edit only has the only change, to simply specify the program you want to
# open the pictures in, as requested by OtterBrah
#
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
# Use it AS IS on your own risk.
# ------- USER SETTINGS -------
# if you want to use a custom editor, paste the path below, e.g.:
# open_command = "C:\Program Files\paint.net\PaintDotNet.exe"
# You dont need to escape whitespaces
open_command = ""
# ----- END USER SETTINGS -----
if open_command:
open_command = "\"" + open_command + "\" "
# import the main window object (mw) from aqt
from aqt import mw
# import the "show info" tool from utils.py
from aqt.utils import showInfo
# import all of the Qt GUI library
from aqt.qt import *
#import regular expressions
import re
import os
import subprocess
from anki.hooks import addHook
from anki.hooks import wrap
from aqt.editor import Editor
from aqt.utils import showInfo
# We're going to add a menu item below. First we want to create a function to
# be called when the menu item is activated.
def edit_pic_externally(editor):
#throw all the fields in string form into an array
editor.loadNote()
for i in range(len(editor.note.fields)):
field = editor.note.fields[i]
#iterate through the array and find all image paths
#put all image paths in an array
pictures = re.findall(r'\<img src="(.*?)"', field)
#load the path to the users collection
collection_path = mw.col.media.dir() +"\\"
#for each picture get the full path and then open it externally
for picture in pictures:
if picture:
full_image_path = os.path.join(collection_path, picture)
#escape spaces in image path
subprocess.Popen(open_command + "\"" + full_image_path + "\"", shell = True)
# Uncomment lines below to copy the command to clipboard
# command = 'echo ' + open_command + "\"" + full_image_path + "\"" + '| clip'
# os.system(command)
def button_pressed(self):
edit_pic_externally(self)
def my_setup_buttons(self):
# - size=False tells Anki not to use a small button
# - the lambda is necessary to pass the editor instance to the
# callback, as we're passing in a function rather than a bound
# method
self._addButton("mybutton", lambda s=self: button_pressed(self),
text="Edit Images", size=False, tip="Edit images in external editor.")
Editor.setupButtons = wrap(Editor.setupButtons, my_setup_buttons)
@wo4wangle
Copy link

wo4wangle commented Apr 30, 2019

Thanks for your development!
I am a chinese anki fans, and download your add-on recently.
Some errors happen. Here is the feedback from anki:

一个插件发生了错误。
请把它报告在插件的论坛上:
https://anki.tenderapp.com/discussions/add-ons

Traceback (most recent call last):
File "C:\Users\wangle\AppData\Roaming\Anki2\addons\Edit_Images_Externally_Updated_Windows.py", line 73, in
self._addButton("mybutton", lambda s=self: button_pressed(self),
File "C:\Users\wangle\AppData\Roaming\Anki2\addons\Edit_Images_Externally_Updated_Windows.py", line 66, in button_pressed
edit_pic_externally(self)
File "C:\Users\wangle\AppData\Roaming\Anki2\addons\Edit_Images_Externally_Updated_Windows.py", line 59, in edit_pic_externally
subprocess.Popen(open_command + """ + full_image_path + """, shell = True)
File "subprocess.py", line 679, in init
File "subprocess.py", line 870, in _execute_child
UnicodeEncodeError: 'ascii' codec can't encode characters in position 39-40: ordinal not in range(128)

Here is some information may help you find the problem:
1.the way I use the add-on:
I review a card and I click edit button then choose an image that I want to edit by clicking the button "Edit Images".but some errors happen.
2.my running environment:
win10
anki 2.0.47
3. I choose the default mode: open_command = "", and I can open an image by double click an image and the execute softer is window' own.
4. I also try to use PaintDotNet.exe , but still call errors.

I really need an add-on to provide the function to edit images while reviewing.
hope you can help me!

MY email is 476384374@qq.com.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment