Posts

Showing posts with the label code.Python

Face detection using Python/OpenCV on Raspberry Pi, display on ST7789 LCD using Luma.LCD.

Image
Last exercise ILI9488 SPI LCD on Raspberry Pi/Python using Luma.LCD  display images on 3.5 inch 480x320 ILI9488 SPI LCD , run on Raspberry Pi 4B/Raspberry Pi OS 64-bit (bookworm), using Python + PIL + Luma.LCD. This exercise introduce using OpenCV to read images, convert OpenCV ndarray to PIL Image, then display on 2.4 inch TFT Module 240×320 ST7789V using Luma.LCD. Also implement face detection. Connection between ST7789 and Raspberry Pi, follow last exercise, same GPIO pins (for sure, the pin order on display module hanged). Also other setup , create Python virtual environment and install luma.lcd. Read last exercise . To use OpenCV (cv2) in this exercise we have to install opencv-python in Python virtual environment: With Python virtual environment activated, run the command: pip install opencv-python Exercise code: luma_st7789_cv2_image_show.py , read a single jpg image using cv2, convert OpenCV ndarray to PIL...

ILI9488 SPI LCD on Raspberry Pi/Python using Luma.LCD

Image
Luma.LCD  provides a Python3 interface to small LCD displays connected to Raspberry Pi and other Linux-based single-board computers (SBC). It provides a Pillow-compatible drawing canvas, and other functionality. This exercises tested on Raspberry Pi 4/64-bit Raspberry Pi OS (bookworm) using Python3 + Luma.LCD to driver 3.5 inch 480x320 ILI9488 SPI LCD . Connection: Follows the suggested wiring for ILI9488 in Luma.LCD docs . Enable SPI Interface: Make sure SPI is enabled in Raspberry Pi using raspi-config. ( https://luma-lcd.readthedocs.io/en/latest/hardware.html#enabling-the-spi-interface ) sudo raspi-config Create Python virtual environment Install luma.lcd: Create Python virtual environment to include site packages: python -m venv --system-site-packages envPy_luma Activate the virtual environment: source envPy_luma/bin/activate install the latest version of the library in the virtual environment with: pip install --upgrade luma.lcd Ex...

Cartoonized images display on 320×480 ST7796 SPI LCD, using Python on Raspberry Pi 4B

Image
Previous post introduced Cartoonize image using OpenCV in Python, run on Windows 11 . This exercises run on Raspberry Pi 4B/64 bit Raspberry Pi OS (bookworm), and modified to display on Waveshare 3.5" 320×480 ST7796 SPI LCD . Connection and setup demo (include ST7796 driver), read  Test "Waveshare 3.5inch Capacitive Touch LCD" on Raspberry Pi Zero 2 W .  OpenCV (cv2) is needed, to install OpenCV in Python virtual environment: Switch to where you want to Python virtual environment located. Create Python virtual environment to include site packages, where envPy_cv2 is the name of my virtual environment: $ python -m venv --system-site-packages envPy_cv2 Activate the virtual environment: $ source envPy_cv2/bin/activate Install OpenCV: $ pip install opencv-python Exit virtual environment after finished: $ deactivate In Thonny, configure interpreter to select the Python executable in the new virtual environment. Exercise code: pyCartoonize....

Cartoonize image using OpenCV in Python

Image
I found a great example code to cartoonize image using OpenCV in Python, let's give it a try! Reference:  Best Ways to Cartoonize an Image Using OpenCV in Python Modified code in my exercise, pyCartoonize.py. """ Cartoonize image using OpenCV in Python I found a great example code to cartoonize image using OpenCV in Python, let's give it a try! reference: https://blog.finxter.com/5-best-ways-to-cartoonize-an-image-using-opencv-in-python/ In the post, 4 method are listed to Cartoonize an Image Using OpenCV in Python. Method 1: Bilateral Filtering and Edge Detection. Produces a smooth, clean cartoon effect. Good for high-resolution images but can be computationally intensive. Method 2: Color Quantization and Edge Enhancement. Delivers a visually distinct cartoon with flat colors and crisp borders. Works best with strongly contrasting images. The setup is slightly complex due to k-means. Method 3:...

Simple USB Cam viewer in Windows 11, using Python + OpenCV

Image
My scenario is I have two USB Cam deviecs: - A Video Capture Adapter, used to convert HDMI output from Raspberry Pi, convert to USB, my PC will recognize it as a USB web cam. - A normal USB web cam. I use them to record Raspberry Pi operation and unit under developed, as show in the video: https://www.youtube.com/watch?v=JVnYG2-OC8w But in Windows, only one Camera can be opened at the same time. So I make a simple Python code to display the web cam view on screen, no any capture/record control. Such that I can record screen with both my Raspberry Pi operation and the web cam view. In the Python code, OpenCV (cv2) is used to handle the cam, so have to install it. In my practice: - Create a Python virtual environment in Windows 11, named envUSBCAM.      Enter the command in Terminal:    > python -m venv envUSBCAM    Activate the virtual environment:   > .\\Scripts\activate - Install OpenCV in the virtual e...

Python on Raspberry Pi to display images on ST7796S SPI LCD

Image
Python exercises run on Raspberry Pi 4/64-bit Raspberry Pi OS (bookworm) to display images on Waveshare 3.5 inch 320x480 Capacitive Touch LCD, with ST7796S driver . For connection and setup (include downloading of demo and drivers), refer to the post  Test "Waveshare 3.5inch Capacitive Touch LCD" on Raspberry Pi Zero 2 W . Exercise Code: LCD_image_show.py , display single image. Convert and rotate 1024x768 jpg image to 320x480, and display on ST7796 SPI LCD. #!/usr/bin/python # -*- coding: UTF-8 -*- #import chardet """ Python exercise run on Raspberry Pi 4: Read image and display on Waveshare 3.5inch Capacitive Touch LCD with ST7796 SPI driver. Connection and setup, read: https://coxxect.blogspot.com/2025/01/test-waveshare-35inch-capacitive-touch.html remark: All test images were generated by X's Grok, not real. """ import st7796 from PIL import Image, ImageOps if __name__=='__main__': disp = st7796.st77...

Python 3 +OpenCV + PyQt6 to display usbcam

Image
A simple exercise run on Windows 11/Python 3.13.0 to capture images from usbcam display with GUI using OpenCV and PyQt6. It's tested in Python virtual environment on Windows 11, with OpenCV aand PyQt6 installed . pyqt6_cv2_usbcam.py , actually it's provided by MicroSoft Copilot. import sys import cv2 from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout from PyQt6.QtGui import QImage, QPixmap from PyQt6.QtCore import QTimer class WebcamViewer(QWidget): def __init__(self): super().__init__() self.initUI() self.cap = cv2.VideoCapture(0) self.timer = QTimer(self) self.timer.timeout.connect(self.update_frame) self.timer.start(20) def initUI(self): self.setWindowTitle('Webcam') self.image_label = QLabel(self) layout = QVBoxLayout() layout.addWidget(self.image_label) self.setLayout(layout) def update_frame(self): ret, frame = self.cap...

Resize jpg and convert to bmp in RGB888 and RGB565 mode, using Python/GIMP

Image
This video show how to resize jpg and convert to bmp in RGB888 and RGB565 mode, in two approach: - Using Python code - Using GIMP Approach 1 using Python: Tested on Python 3.13.0 with cv2 4.10.0 (OpenCV) installed, run on Python virtual environment/Windows 11. To create Python virtual environment in Windows 11, and install libraries, read  https://coxxect.blogspot.com/2024/10/create-python-virtual-environment-in.html . py_cv_batch_resize_RGB888.py """ Python/cv2 to resize all jpg in 'images' folder, and save to 'resized_images_rgb888' folder in RGB888 mode. This code was generated by Copilot mainly. Run on Windows 11/Python 3.13.0 in virtualenvironment, with OpebCV/cv2 installed. To create Python virtual environment in Windows 11, and libraries include OpenCV/cv2, ref: https://coxxect.blogspot.com/2024/10/create-python-virtual-environment-in.html """ import os import cv2 TARGET_WIDTH = 240 TARGET_HEIGHT = 240 def...

Python code to read .bmp info from header (work on Desktop Python and MicroPython)

Image
This exercise read header of bmp file to get information about the image, such as image width and height, number of bit per pixel, specially offset, such that we can get the bitmap image later. This help me understanding more about bmp file structure. For the BMP file format, I reference  https://en.wikipedia.org/wiki/BMP_file_format . Exercise Code: The Python code work on desktop Python and MicroPython, tested on: - Python 3.13.0 on Windows 11 - MicroPython v1.24.0 on Raspberry Pi Pico2 with RP2350 py_bmp_info.py """ Python/MicroPython exercise to read bmp file and get the bmp info, to understand the structure of bmp header. Work on both: - desktop Python (tested on Windows 11) .bmp image have to be save in 'images' directory under working directory. - MicroPython (tested on Raspberry Pi Pico2 with RP2350 running MicroPython v1.24.0 on 2024-10-25 ) .bmp files have to be saved in /images/ directory. To prepare the bmp using GIMP...

Python/PyQt6 slideshow run on Windows 11

Image
A simple Python/PyQt6 SlideShow to display jpg images in images folder. It's generated by Copilot, except the lines marked in blue. Tested on Windows 11 with PyQt6 installed in Python virtual environment . pyqt6_slideshow.py import sys import glob from PyQt6.QtWidgets import QApplication, QLabel, QMainWindow, QVBoxLayout, QWidget from PyQt6.QtGui import QPixmap from PyQt6.QtCore import QTimer from PyQt6.QtCore import Qt class SlideShowWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Image Slide Show") self.image_label = QLabel(self) self.image_label.setAlignment(Qt.AlignmentFlag.AlignCenter) layout = QVBoxLayout() layout.addWidget(self.image_label) container = QWidget() container.setLayout(layout) self.setCentralWidget(container) self.image_files = glob.glob(" images/ *.jpg") self.current_index = 0 self.show_image() ...

Python code to get system info, for desktop Python, MicroPython and CircuitPython

Image
It's a simple Python code to get system info, tested on cpython (desktop Python), MicroPython on Raspberry Pi Pico2 and CircuitPython on ESP32-S3. ex_py_info.py """ Exercise to get system info in Python Tested on: - desktop Python on Windows 11 - MicroPython on Raspberry Pi Pico 2 (RISCV) - CircuitPython on ESP32-S3 """ import os, sys print(sys.implementation.name, ":") print(sys.version) print(sys.platform) if sys.implementation.name == 'cpython': # Desktop Python import platform print("=============================================") print("Running on: ", platform.platform()) print("=============================================") elif sys.implementation.name == 'micropython' or sys.implementation.name == 'circuitpython': # MicroPython or CircuitPython print("====================================") print(sys.implementation[0], os.uname()...