In [1]:
%cd 'D:\\Projects\\Contour\\'
D:\Projects\Contour

Generate the contour from the original, save it so the dimensions equal the input (512*384)

In [2]:
from PIL import Image

im = np.array(Image.open('0O8Vt.jpg').convert('L'))

xpixels = im.shape[1]
ypixels = im.shape[0]

dpi = 72
scalefactor = 1

xinch = xpixels * scalefactor / dpi
yinch = ypixels * scalefactor / dpi

fig = plt.figure(figsize=(xinch,yinch))

ax = plt.axes([0, 0, 1, 1], frame_on=False, xticks=[], yticks=[])

contour(im, levels=[240], colors='black', origin='image', linewidths=[3])

plt.savefig('same_size.png', dpi=dpi, transparent=True)
fig.clf()
plt.close(fig)

Now open both images again and superimpose them

In [3]:
from PIL import Image

im_orig = np.array(Image.open('0O8Vt.jpg'))
im_cnt = np.array(Image.open('same_size.png').convert('L'))

print 'Original dimensions: ', im_orig.shape
print 'Contour dimensions: ', im_orig.shape

xpixels = im_orig.shape[1]
ypixels = im_orig.shape[0]

dpi = 72
scalefactor = 1

xinch = xpixels * scalefactor / dpi
yinch = ypixels * scalefactor / dpi

fig = plt.figure(figsize=(xinch,yinch))

ax = plt.axes([0, 0, 1, 1], frame_on=False, xticks=[], yticks=[])

ax.imshow(im_orig)
ax.imshow(np.ma.masked_equal(im_cnt, 255), cmap=plt.cm.Greys_r)

plt.savefig('overlay2.png', dpi=dpi, transparent=True)
Original dimensions:  (384, 512, 3)
Contour dimensions:  (384, 512, 3)

Back to top