Capture Image and Convert into PDF in Python

Vaibhav Mishra
Oct 26, 2020

Coding is fun for programmers, But when you code in python that fun becomes double. So purpose of this blog is to spread fun everywhere.

There is multiple project where we need to capture the image and then we need to upload that image to cloud for some of our documents. I can say you a scenario where you need to use this blog, Suppose you are creating a job portal where you need to generate users details in PDF with his image then you can use this small piece of python code and save your time.

Import Modules Open CV and Pillow

import cv2
from PIL import Image

Initiating Video Object and Capture the Image

while True:
# Initiating frame
ch, fr = video.read()
cv2.imshow("Your Beautiful Image",fr)
stop_key = cv2.waitKey(1)
if stop_key == ord('s'):
break

Save the image to convert into PDF and shut off the camera

your_pic = cv2.imwrite("Your_Beautiful_Pic.jpg",frame)
video.release()
cv2.destroyAllWindows

Convert your saved image into PDF

your_pdf_image = Image.open(r'Your_Beautiful_Pic.jpg')
your_pdf_image_obj = your_pdf_image.convert('RGB')
your_pdf_image_obj.save(r'Your_Beautiful_Pic.pdf')

Make sure you have installed below module using:

pip3 install opencv-python
pip3 install Pillow

Happy Coding…..

--

--

Vaibhav Mishra

I am passionate software developer working in different technologies like Python, ML, Django and JavaScript.