Skip to content

Instantly share code, notes, and snippets.

@srustamo
Created November 30, 2019 11:02
Show Gist options
  • Save srustamo/22f1419a7573c0406136e9c57167e89a to your computer and use it in GitHub Desktop.
Save srustamo/22f1419a7573c0406136e9c57167e89a to your computer and use it in GitHub Desktop.
import argparse
import os
import tempfile
import autopy
import img2pdf
def screenshot(top_left, right_bottom, next_page, total_page):
rect_size = (right_bottom[0] - top_left[0], right_bottom[1] - top_left[1])
images = []
temp_dir = tempfile.mkdtemp()
for i in range(total_page):
page_num = "{}".format(i).zfill(len(str(total_page)))
file_name = os.path.join(temp_dir, 'book-page-{}.png'.format(page_num))
images.append(file_name)
# autopy.mouse.move(*next_page)
autopy.key.tap(autopy.key.Code.SPACE)
# autopy.mouse.click(delay=1)
autopy.bitmap.capture_screen((top_left, rect_size)).save(file_name)
return images
def image2pdf(images):
with open("book.pdf", "wb") as f:
f.write(img2pdf.convert(images))
if __name__ == "__main__":
autopy.mouse.move(1466,616)
autopy.mouse.click()
parser = argparse.ArgumentParser(description='Take book screenshots.')
parser.add_argument('top_left', type=str)
parser.add_argument('right_bottom', type=str)
parser.add_argument('next_button', type=str)
parser.add_argument('total_page', type=int)
args = parser.parse_args()
top_left = tuple(map(lambda x: int(x), args.top_left.split(',')))
right_bottom = tuple(map(lambda x: int(x), args.right_bottom.split(',')))
next_button = tuple(map(lambda x: int(x), args.next_button.split(',')))
total_page = args.total_page
print("Take book screenshot at {} {} and next at {} with {} pages".format(
top_left, right_bottom, next_button, total_page
))
images = screenshot(top_left, right_bottom, next_button, total_page)
image2pdf(images)
print("Done, book saved in book.pdf.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment