import sys, os from io import BytesIO from urllib.request import urlopen from matplotlib.image import pil_to_array from mpl_toolkits.axes_grid.axes_grid import ImageGrid from scipy import ndimage from bs4 import BeautifulSoup from PIL.JpegImagePlugin import Image from PyQt4.QtGui import * from PyQt4.QtCore import * from PyQt4.QtWebKit import * class Renderer(QWebPage): def __init__(self, url): self.app = QApplication(sys.argv) QWebPage.__init__(self) @self.loadFinished.connect def _loadFinished(result): self.frame = self.mainFrame() self.app.quit() self.mainFrame().load(QUrl(url)) self.app.exec_() r = Renderer('http://www.minteye.com/products.aspx') page = r.frame.toHtml() html = BeautifulSoup(page) imgs = [] for img in html.find_all(class_='minteyeimageloaded'): img_bytes = BytesIO(urlopen(img['src']).read()) imgs.append(pil_to_array(Image.open(img_bytes).convert('L'))) side1 = round(len(imgs) ** (1/2)) side2 = len(imgs) // side1 fig = plt.figure(1, (side1*3, side2*4)) grid = ImageGrid(fig, 111, nrows_ncols=(side1, side2), axes_pad=.1) for i, img in enumerate(imgs): grid[i].imshow(img, cmap=cm.Greys_r) fig = plt.figure(1, (side1*3, side2*4)) grid = ImageGrid(fig, 111, nrows_ncols=(side1, side2), axes_pad=.1) sobels = [] total = [] for img in imgs: img = img.astype('int32') sobel = ndimage.generic_gradient_magnitude(img, ndimage.sobel) sobels.append(sobel) total.append(np.sum(sobel)) print(total) res = np.argmin(total) for i, sobel in enumerate(sobels): grid[i].set_label(str(total[i])) grid[i].imshow(sobel, cmap=cm.gist_earth if i==res else cm.Greys_r)