Reputation: 695
I'm currently working on an image processing script in Python (Spyder IDE, Python 3.5, Anaconda 4.0.0). When I first open the IDE, I only have to press 'run script' once for the script to execute. But after that, I have to press 'run script' twice, sometimes even three times, for it to execute. Searching around the internet, it seems that the issue has to do with using matplotlib
and pyplot
. It's mainly an issue because I will spend 5 minutes per test just getting my script to execute. My code is included below. I decided to ask about the issue here to see if anyone might have a suggestion or idea to get my script to execute on the first press.
EDIT: Whenever I restart my kernel (start a new console), I'm able to get the script run on the first press.
import numpy as np
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data, img_as_float
from skimage.filters import gaussian
from skimage.segmentation import active_contour
from skimage import io
from skimage import exposure
import scipy
scipy_version = list(map(int, scipy.__version__.split('.')))
new_scipy = scipy_version[0] > 0 or \
(scipy_version[0] == 0 and scipy_version[1] >= 14)
'''
img = data.astronaut()
img = rgb2gray(img)
'''
openLocation = "file location here"
img = io.imread(openLocation)
#img = rgb2gray(img)
s = np.linspace(0, 2*np.pi, 600)
x = 400 + 300*np.cos(s)
y = 550 + 280*np.sin(s)
init = np.array([x, y]).T
if not new_scipy:
print('You are using an old version of scipy. '
'Active contours is implemented for scipy versions '
'0.14.0 and above.')
if new_scipy:
snake = active_contour(img, init, alpha=0.01, beta=0.01, w_line = 5, w_edge = 0, gamma=0.01, bc = 'periodic')
fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(111)
plt.gray()
ax.imshow(img)
ax.plot(init[:, 0], init[:, 1], '--r', lw=3)
ax.plot(snake[:, 0], snake[:, 1], '-b', lw=3)
ax.set_xticks([]), ax.set_yticks([])
ax.axis([0, img.shape[1], img.shape[0], 0])
Upvotes: 0
Views: 921
Reputation: 7828
I think your issue is related to this its a bug in spyder
.It provides a partial solution so far is to use a different Matplotlib backend. You can change it in:
Preferences > Console > External modules > Matplotlib
from the default (Qt4Agg) to TkAgg (the only other available on Windows).
One more thing you can try is updating spyder
and then try running your script.
Upvotes: 1