I got this following error while running the python program."AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS' in PYTHON".


The below python program is to download image from URL and save it to the pdf file.

CODE:

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

image_url = 'https://tensix.com/wp-content/uploads/2023/11/P6-EPPM-Baseline-Update-Fig-1.jpg'

response = requests.get(image_url)

if response.status_code == 200:
    img = Image.open(BytesIO(response.content))

    pdf_file = "output.pdf"
    c = canvas.Canvas(pdf_file, pagesize=letter)

    max_width = 500 
    width_percent = max_width / float(img.size[0])
    height = int(float(img.size[1]) * float(width_percent))

    img = img.resize((max_width, height), Image.Resampling.LANCZOS)
    temp_img_path = 'temp_image.png'
    img.save(temp_img_path)
    c.drawImage(temp_img_path, 50, 700 - height)
    c.save()
    print(f"PDF generated with the image: {pdf_file}")
else:
    print("Failed to fetch the image.")

SOLUTION:

ANTIALIAS is worked in old versions. If you are using Pillow 10.0.0 and above  You need to use PIL.Image.LANCZOS or PIL.Image.Resampling.LANCZOS.

  img = img.resize((max_width, height), Image.Resampling.LANCZOS)

VIDEO GUIDE: