Python3: Download PDF to memory and convert first page to image

别等时光非礼了梦想. 提交于 2021-02-10 05:51:28

问题


i try to do the following:

  • Download a PDF file to memory
  • Convert the first page to an image
  • Use that image with tweepy

I tried the following code, but run into an error.

from PIL import Image
from pdf2image import convert_from_path
from urllib.request import urlopen
from io import StringIO, BytesIO

url = 'http://somedomain.com/assets/applets/internet.pdf'
scrape = urlopen(url) # for external files
pdfFile = BytesIO(scrape.read())
pdfFile.seek(0)
pages = convert_from_path(pdfFile,last_page=1, dpi=100)

for page in pages:
    page.save('/home/out.jpg', 'JPEG')

Here is the error:

TypeError: Can't convert '_io.BytesIO' object to str implicitly

The generated image should later be used to upload it to twitter by tweepy. I don't need to store it to disk, that's why i try to do all in memory. Anybody who could help me please?

来源:https://stackoverflow.com/questions/50764130/python3-download-pdf-to-memory-and-convert-first-page-to-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!