Päivitä
Ask How

How to convert a JPEG into PDF in Python

Using python to convert any file into PDF is different than using web-based programs or downloaded software. When using this programming language, basically you need to install an open-source python package. These packages usually have modules and proper codes to follow after installation.

Published 04 Mar 2021

Using python to convert any file into PDF is different than using web-based programs or downloaded software. When using this programming language, basically you need to install an open-source python package. These packages usually have modules and proper codes to follow after installation.

 python programming language

What is python?

Python is a relatively new programming language that is designed to be “readable” even as a code by making it closer to the English Language. It is used for general programming, software development, management, and testing. You can create a downloadable application or a web-based one with it, which can also hold both data analysis and visualization. Even with its seemingly basic language, it is high-level and object-oriented with dynamic semantics. Because the goal of Python was to make it “readable” it is, therefore, easy to learn and easy to do. Additionally, two benefits of using this language are the fact that developers only need fewer lines to create the program that they want and it can instantly execute the code after it has been written. Therefore, the developer will be able to see its prototype software faster.

easy to program with python

How to convert using Python

Open source Python packages are ready online. Just follow the module provided by the download, and input the proper codes after. Here is an example of how to achieve converting an Image to PDF using the PIL package in Python:

Step one: Install the PIL package by using the command:

pip install Pillow

Step two: Copy the path where your image is located

Step three: Use the code template to convert the image

from PIL import Image

 

image1 = Image.open(r'path where the image is stored\file name.png')

im1 = image1.convert('RGB')

im1.save(r'path where the pdf will be stored\new file name.pdf')

 

Another Python package that you can search for online is img2pdf. It is another open-source Python package that can do the same job but uses the same Pillow module.

Step one: install the package by using this code:

pip install img2pdf

Step two: write the code below

# Python3 program to convert image to pfd

# using img2pdf library

 

# importing necessary libraries

import img2pdf

from PIL import Image

import os

 

# storing image path

img_path = "C:/Users/Admin/Desktop/GfG_images/do_nawab.png"

 

# storing pdf path

pdf_path = "C:/Users/Admin/Desktop/GfG_images/file.pdf"

 

# opening image

image = Image.open(img_path)

 

# converting into chunks using img2pdf

pdf_bytes = img2pdf.convert(image.filename)

 

# opening or creating pdf file

file = open(pdf_path, "wb")

 

# writing pdf files with chunks

file.write(pdf_bytes)

 

# closing image file

image.close()

 

# closing pdf file

file.close()

 

# output

print("Successfully made pdf file")

Alternatives to Python

If you don’t want to use a programming language to convert or edit your JPEG or PDF files, then use web-based online software to convert and edit your documents. Use DeftPDF.com for your PDF needs. You can use various tools like converting, editing, translating, watermark, encryption, unlock, split, merge, and more for free.

chrome extension

 

 

Want to get updates and subscribe to our blog? Get weekly e-notifications by creating an account with us:  DeftPDF online

close