Raspberry Pi 4 & Raspberry Pi 5

RPi IR-CUT Camera, Better Image in Both Day and Night(5 megapixel OV5647 sensor)

If you have a little headache with cameras and PIL libraries, try that below steps in order .

Python Camera Test Code

#
import tkinter as tk
import subprocess
from PIL import Image, ImageTk
# ozgurcinar@gmail.com
# mdemirkiran@hotmail.com

def run_rpicam_hello():
    # terminal subprocess ile calis
    command = "rpicam-hello -t 0 --qt-preview --shutter 3000"
    process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    # komut sonucu oku
    output, error = process.communicate()
    
    # ciktiyi goruntuye donustur ve tkinter penceresinde goster
    img = Image.open("output.jpg")  # output.jpg olarak kaydet
    img = img.resize((640, 480))  # cozunurluk ayarla
    photo = ImageTk.PhotoImage(img)
    label.config(image=photo)
    label.image = photo

root = tk.Tk()
root.title("rpicam-hello - Orman Gezer")

button = tk.Button(root, text="Kamera Ac", command=run_rpicam_hello)
button.pack()

label = tk.Label(root)
label.pack()

root.mainloop()

Raspi 4 and Raspi 5 and Camera exactly run with that steps.

I prefer that fresh installed Rasbian OS. I have chose 32 bit mode beacuse of 64 bit is still does not have correct driver or I couldn’t manage 64 bit OS and Camera .

Here are magical steps in order to use camera and PIL library on Raspberry pi 4 and Raspberry pi 5

  • Create a virtual environment and activate it

ozgurcinar@raspberrypi:~ $ python3 -m venv myenv

ozgurcinar@raspberrypi:~ $ source myenv/bin/activate

(myenv) ozgurcinar@raspberrypi:~ $ pip install Pillow

(myenv) ozgurcinar@raspberrypi:~ $ pip install –upgrade Pillow

  • Attach virtual env

echo $PYTHONPATH  (empty result)

echo $PATH  (check from result if path attached or not)

export PATH=/home/ozgurcinar/myenv/bin:$PATH

echo $PATH  (check from result if path attached or not)

  • Attach the packages of virtual environment with Python

import sys

#sys.path.append(“/path/to/your/virtualenv/lib/python3.x/site-packages”)

sys.path.append(“/home/ozgurcinar/myenv/lib/python3.11/site-packages”)

  • Update and re-install somethings 🙂

(myenv) ozgurcinar@raspberrypi:~ $ sudo apt-get install python3-tk

(myenv) ozgurcinar@raspberrypi:~ $ sudo apt-get install python3-pil.imagetk