Putting a .png file into a PDF using PyPDF2, but the .png file jagged edges at its borders
This is the graph generated through matplotlib:
It's saved in a folder, and then added to the PDF through PyPDF2.
For reasons unclear, despite the high quality of the .png, on the PDF it appears as having jagged edges (especially near the bottom). These disappear when you zoom in, but they're otherwise always there.
I can't figure out what I'm doing wrong. I've added similar .png pictures to powerpoint slides before without having this problem!
Using matplotlib, the image is saved using dpi = 1500:
fig1.savefig(saving_path, transparent = True,dpi=1500)
PyPDF2 is used to attach the .png that's outputted into the PDF file:
from reportlab.pdfgen import canvas from PyPDF2 import PdfFileWriter, PdfFileReader pdf_name = input("Enter the name of the template pdf file (add a .pdf at the end): \n") output_file = PdfFileWriter() input_file = PdfFileReader(open(pdf_name, "rb")) c = canvas.Canvas('image.pdf',pagesize=(2480, 3508)) c.drawImage(r'.\matplotlib_pic.png', 440, 600,130,155,mask=[250,255,250,255,250,255]) c.save() image = PdfFileReader(open("image.pdf", "rb")) input_file = PdfFileReader(open(pdf_name, "rb")) page_count = input_file.getNumPages() output_file = PdfFileWriter() for page_number in range(page_count): input_page = input_file.getPage(page_number) input_page.mergePage(image.getPage(0)) output_file.addPage(input_page) with open("testimageprint.pdf", "wb") as outputStream: output_file.write(outputStream)