Extract small regions from a PIL image python - python-3.x

enter image description here
I have a PIL image in python as shown and I want to extract each of the small red regions separately into a jpeg format. So from this file, I'm expecting file1.jpg, file2.jpg, etc.
How can I obtain each sub-regions in each different file?

PIL doesn't really excel at that. I would consider using:
OpenCV findContours(), or
scikit-image regionprops(), or
ImageMagick Connected Components, or
Python wand bindings to ImageMagick as above

Related

How is transparent background removed from a TIFF file in Python

I have a situation where I need to convert TIFF files to JPEG files in Python. I am using the PIL library to do this and it works fine unless the TIFF has a transparent background on it and then PIL can't open the file and says it is not recognized. Are there other solutions to this in Python?
TIFF format files usually consist of multiple images of different resolutions. Try reading with openslide
Eg:
patch = openslide.OpenSlide(img_path)
patch = patch.read_region((17800,19500), 0, (256, 256))
For more info visit openslide documentation

How to change a part of the color of the background, which is black, to white?

I have been working on PyTesseract OCR and converting PDF to JPEG inorder to OCR the image. A part of the image has a black background and white text, which Tesseract is unable to identify, whereas all other parts of my image are being read perfectly well. Is there a way to change a part of the image that has black background? I tried a few SO resources, but doesn't seem to help.
I am using Python 3, Open CV version 4 and PyTesseract
opencv has a bitwise not function wich correctly reverses the image
you can put a mask / freeze on the rest of the image (the part that is correct already) and use something like this:
imageWithMask = cv2.bitwise_not(imageWithMask)
alternatively you can also perform the operation on a copy of the image and only copy over parts / pixels / regions you need....

Python / PIL - Raw RGB565 data to PNG?

I have raw RGB565 data as a bytes-like object and I want to save it as a PNG.
Although it is possible with libraries such as PyQt5, as you can here:
QtGui.QImage(data, width, height, QtGui.QImage.Format_RGB16)
I would like to use only PIL, but I cannot find a way to do this with only PIL.
More generally, a method that does not involve Qt would be fine.
Thanks.
It so happens that the pypng repository has a tool to convert from Kobo's 15-bit to PNG.
It's here: https://github.com/drj11/pypng/blob/master/code/kobo565topng
One word of warning however, it outputs an 8-bit RGB PNG.
You don't save any space by saving it as an R5G6B5 PNG, so that might not matter much.

How to show image from webcam

I never needed to work with camera before, so I do not know where to start.
I need to display the image of a real-time camera, to capture and save to a file.
I'm using python3 and gtk3.
Does gtk has any feature for this?
The easiest way to get the image from a webcam is to use OpenCV. It allows you to get the image with just 2 lines of code and 2 more to show it, like so:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cv2.imshow('frame',frame)
cv2.waitKey(0)
But there is a downside, namely that OpenCV for Python 3 has to be build from source. Most people don't like building from source so they say it is not available.
Luckily there is the Unofficial Windows Binaries for Python Extension Packages by Gohlke (University of California) which also offers a precompiled version of OpenCV for Python 3. Installing that using pip should be easy.

Create an Image either using Magick Wand or ImageMagick on Python 3

There's not enough information on Internet about imagemagick and magick wand on Python 3. I need to create a lot of images using Python 3, creating the images with a background color or a background image.
When I get the image with the background I want, then I need to add text with a font of my preference on It, I think I can solve the problem of the font, but how to add the "string" in the image?
After that, I want to save the image with a "name".
I have installed Magick wand and Image Magick on Python 3, but the documentation is in a language that I really don't understand. Do I need to install something else?
If you're able to help me, it would be great. Thank you!
You can achieve these using Wand.
Creating the images with a background color or a background image
Add text with a font
Save the image with a “name”

Resources