c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
Python

expected str, bytes or os.PathLike object, not JpegImageFile - Python

| | python

I got this following error message while creating a pdf using the python program "expected str, bytes or os.PathLike object, not JpegImageFile". It indicates that the drawing method expects a file path(string) ,byte objects or PathLike objects. But it receives directly the JpegImageFile object.

expected str

CODE:

import requests

from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from PIL import Image
from io import BytesIO

def download_image_from_url(image_url):
try:
response = requests.get(image_url)
if response.status_code == 200:
image_data = BytesIO(response.content)
return image_data
else:
print("Failed to download image from URL.")
return None
except Exception as e:
print(f"Error downloading image: {e}")
return None

def create_pdf_from_image(image_url, pdf_filename):
# Download image from URL
image_data = download_image_from_url(image_url)

if image_data:
try:
img = Image.open(image_data)
pdf_path = f"{pdf_filename}.pdf"
c = canvas.Canvas(pdf_path, pagesize=img.size)
c.drawImage(img, 0, 0)
c.save()

print(f"PDF saved successfully: {pdf_path}")
except Exception as e:
print(f"Error creating PDF: {e}")

image_url = 'https://tensix.com/wp-content/uploads/2023/11/P6-EPPM-Baseline-Update-Fig-4.jpg'
pdf_filename = 'image_to_pdf' create_pdf_from_image(image_url, pdf_filename)

SOLUTION:

To resolve this problem download the image as temporary file and use its file path and draw it on PDF canvas.

import requests

from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from PIL import Image
from io import BytesIO
import tempfile

def download_image_from_url(image_url):
try:
response = requests.get(image_url)
if response.status_code == 200:
image_data = BytesIO(response.content)
return image_data
else:
print("Failed to download image from URL.")
return None
except Exception as e:
print(f"Error downloading image: {e}")
return None

def create_pdf_from_image(image_url, pdf_filename):
# Download image from URL
image_data = download_image_from_url(image_url)

if image_data:
try:
img = Image.open(image_data)

temp_img_file= tempfile.NamedTemporaryFile(delete=False, suffix='.jpg')
temp_img_path= temp_img_file.name
img.save(temp_img_path)
temp_img_file.close()

pdf_path = f"{pdf_filename}.pdf"
c = canvas.Canvas(pdf_path, pagesize=img.size)
c.drawImage(temp_img_path, 0, 0)
c.save()

print(f"PDF saved successfully: {pdf_path}")
except Exception as e:
print(f"Error creating PDF: {e}")

image_url = 'https://tensix.com/wp-content/uploads/2023/11/P6-EPPM-Baseline-Update-Fig-4.jpg'
pdf_filename = 'image_to_pdf' create_pdf_from_image(image_url, pdf_filename)

VIDEO GUIDE: