Created
November 30, 2020 04:54
-
-
Save MarsTechHAN/d95c181c087387d271895272b9fe3a21 to your computer and use it in GitHub Desktop.
Some gadgets for M5Paper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, os | |
import bitstruct.c as bitstruct | |
import shutil | |
import tqdm | |
from pdf2image import convert_from_path, convert_from_bytes | |
import numpy as np | |
from PIL import Image | |
import PIL.ImageOps | |
from pdf2image.exceptions import ( | |
PDFInfoNotInstalledError, | |
PDFPageCountError, | |
PDFSyntaxError | |
) | |
pdf_snap = convert_from_path(sys.argv[1], dpi=300) | |
tmp_dir = sys.argv[1].replace('.pdf', '.dir') | |
shutil.rmtree(tmp_dir) | |
os.mkdir(tmp_dir) | |
idx = 0 | |
#Paper is 16 grey level, which is 4bit | |
pack_cf = bitstruct.compile('u4' * 540 * 960) | |
for image in tqdm.tqdm(pdf_snap): | |
image = image.convert('L').transpose(Image.FLIP_LEFT_RIGHT) | |
image = image.resize((540, 960), Image.BICUBIC) | |
image = PIL.ImageOps.invert(image) | |
print("Idx: %d Image Shape: %d, %d" % (idx, image.size[0], image.size[1])) | |
image = np.array(image) | |
image = image.flatten(order='F') #C -> Row, F -> Colume | |
image = np.right_shift(image, 4) | |
raw_data = pack_cf.pack(*image.tolist()) | |
open(os.path.join(tmp_dir, str(idx) + '.raw'), 'wb+').write(raw_data) | |
idx = idx + 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time, sys | |
from selenium import webdriver | |
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities | |
from PIL import Image | |
from PIL import ImageEnhance | |
from io import BytesIO | |
import numpy as np | |
from pyvirtualdisplay import Display | |
import bitstruct.c as bitstruct | |
import shutil | |
import numpy as np | |
from PIL import Image | |
import PIL.ImageOps | |
import hashlib | |
pack_cf = bitstruct.compile('u4' * 540 * 960) | |
display = Display(visible=int(sys.argv[1]), size=(540, 1600)) | |
display.start() | |
DRIVER_PATH = './chromedriver' | |
URL = 'https://to-do.office.com/tasks/' | |
caps = DesiredCapabilities().CHROME | |
# caps["pageLoadStrategy"] = "normal" # Waits for full page load | |
caps["pageLoadStrategy"] = "none" # Do not wait for full page load | |
chrome_options = webdriver.ChromeOptions() | |
chrome_options.add_argument("user-data-dir=%s" % './user-data/') | |
#chrome_options.add_argument("headless") | |
driver = webdriver.Chrome(executable_path = DRIVER_PATH, options = chrome_options, desired_capabilities=caps) | |
driver.set_window_size(540, 1600) | |
print('Init OK.') | |
driver.get(URL) | |
print('Waiting for page to load.') | |
idx = 0 | |
while driver.page_source.find("Tasks") == -1: | |
img_binary = driver.get_screenshot_as_png() | |
im = Image.open(BytesIO(img_binary)).convert('LA') | |
im.save('screenshot_waiting_%d.png' % idx) | |
idx = idx + 1 | |
time.sleep(1) | |
print('.', end='') | |
# For clear up the CSS to show on paper | |
driver.execute_script("document.body.style.zoom = '1.5'") | |
driver.execute_script("document.getElementById('O365ShellHeader').remove()") | |
driver.execute_script("document.getElementById('leftColumnOverlay').remove()") | |
driver.execute_script("document.querySelectorAll('.sidebar').forEach(function(a){a.remove()})") | |
driver.execute_script("document.querySelectorAll('.leftColumn').forEach(function(a){a.remove()})") | |
driver.execute_script("document.querySelectorAll('.addToTop').forEach(function(a){a.remove()})") | |
driver.execute_script("document.querySelectorAll('.tasksToolbar-top').forEach(function(a){a.remove()})") | |
#driver.execute_script("document.querySelectorAll('.importanceButton').forEach(function(a){a.remove()})") | |
driver.execute_script("document.getElementById('app').style.paddingLeft=0") | |
time.sleep(1) | |
print('\nLoad Done. Start taking captures') | |
page_content = '' | |
try: | |
while 1: | |
if page_content == driver.execute_script("return document.documentElement.outerHTML"): | |
print('No update on webpage...') | |
time.sleep(2) | |
continue | |
else: | |
#driver.execute_script("document.querySelectorAll('.importanceButton').forEach(function(a){a.remove()})") | |
time.sleep(1) | |
page_content = driver.execute_script("return document.documentElement.outerHTML") | |
img_binary = driver.get_screenshot_as_png() | |
im = Image.open(BytesIO(img_binary)).convert('L') | |
print('Screen Size: %d,%d' % (im.size[0], im.size[1])) | |
im = ImageEnhance.Contrast(im).enhance(factor=1.4) | |
im = ImageEnhance.Sharpness(im).enhance(factor=2) | |
plane = Image.new('RGB', (540, 960), (255, 255, 255)) | |
plane.paste(im, (0, 0)) | |
plane.save('screenshot_outlook.png') | |
print('Screenshot taken, start processing...') | |
plane = PIL.ImageOps.invert(plane) | |
plane = np.array(plane.transpose(Image.FLIP_LEFT_RIGHT), dtype=np.uint8) | |
plane = plane.flatten(order='F') #C -> Row, F -> Colume | |
plane = np.right_shift(plane, 4) | |
raw_data = pack_cf.pack(*plane.tolist()) | |
open('outlook.raw', 'wb+').write(raw_data) | |
open('outlook_content.id', 'w+').write(hashlib.sha256(page_content.encode('utf-8')).hexdigest()) | |
except KeyboardInterrupt: | |
driver.close() | |
display.stop() | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment