Skip to content

Instantly share code, notes, and snippets.

@rekhubs
Created August 6, 2015 16:46
Show Gist options
  • Save rekhubs/5bf57815a7e265fb97b4 to your computer and use it in GitHub Desktop.
Save rekhubs/5bf57815a7e265fb97b4 to your computer and use it in GitHub Desktop.
# coding = utf-8
import time
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# using default firefox profile
# selenium looks for user.js rather than prefs.js
# defaultProfileDir = "C:\\Users\\rek\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\dsqsvpe2.default";
# profile = webdriver.FirefoxProfile(defaultProfileDir)
# driver = webdriver.Firefox(profile)
# driver = webdriver.Chrome()
driver = webdriver.Firefox()
# open a site, screenshot
driver.get('http://www.seemh.com/comic/17832/191888.html#p=21')
# get pic addr
picEle = driver.find_element_by_id('mangaFile')
print type(picEle), '\n', picEle
addr = picEle.get_attribute('src')
print addr
# crop image with PIL
driver.get_screenshot_as_file('D:\\whole_page_screenshot.png')
location = picEle.location
print location
size = picEle.size
print size
im = Image.open('D:\\whole_page_screenshot.png')
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
im = im.crop((left, top, right, bottom)) # defines crop points
im.save('D:\\selenium-pic-crop.png') # saves new cropped image
time.sleep(5)
# driver.quit()
driver.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment