Skip to content

Instantly share code, notes, and snippets.

@sharkdeng
Created October 2, 2020 08:24
Show Gist options
  • Save sharkdeng/cd467d4c6862496d16406c460f3b8732 to your computer and use it in GitHub Desktop.
Save sharkdeng/cd467d4c6862496d16406c460f3b8732 to your computer and use it in GitHub Desktop.
turn pdf pages to jpg (not png, since jpg is smaller than png, website friendly)
# extract pdf to jpg
from pdf2image import convert_from_path
from tqdm import tqdm
import os
import uuid
input_path = 'corridor.pdf'
uid = uuid.uuid4().hex[:4]
output_path = uid + '-' + input_path.split('.')[0]
if not os.path.exists(output_path):
os.makedirs(output_path)
pages = convert_from_path(input_path, 500)
for idx, p in tqdm(enumerate(pages)):
new_f = output_path + '/' + output_path + '-' + str(idx) + '.jpg'
p.save(new_f, quality=85)
os.rename(input_path, uid+'-'+input_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment