How to show image from webcam - python-3.x

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.

Related

Extract small regions from a PIL image python

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

How to draw an reactangle with some text using python

I want to draw in an image using python3. I have implemented same thing in android application using java. Looking for something to be done on the web application. You can see at the bottom of the image where I have used paint in android.
My image is :
Sample Image
I am not getting any library or a code snippet to do so in python. Any suggestions will be a great help. Thanks
Pillow package provides the tools you need to add watermarks to your photos!
How to Watermark Your Photos with Python

Imagemagick's import is frustratingly slow

I am writing an small python script to take screenshots of a game window (which will be in the background/minimized) and performing some simple template matching and ocr using cv2.
I am currently calling im's import as follows:
import -window windowID png:-
to take a screenshot of an inactive window.
However this takes almost 4s and is easily the slowest part of my script by a factor of 100x.
Is there any alternative to import or perhaps another way of approaching this that will be faster?
I have already tried graphicsmagick (ended up being slower than imagemagick) and xwd (did not capture the unfocused window even though the windowID was specified)
Link to full python script (Line 44 is where the screenshot taking happens)
You are doing all the PNG encoding and zlib compression in ImageMagick and then decompressing it all again in OpenCV. I guess you would do better if you found a format that is more closely shared between the two.
Specifically, ImageMagick could give you RGB pixels directly, which you could then convert to BGR very easily in OpenCV with cvtColor().
import -window windowID rgb:
You would have to query the window dimensions to get width and height.
Alternatively, you could use PPM format which OpenCV can also read without any libraries and which includes dimensions:
import -window windowID ppm:

How to apply a change of concentration and brightness of image

I want to apply a modification of the concentration and the brightness of an image and I found this example in opencv and python ,but it dont supported with my version of opencv , i want to get this result
using opencv 3 and python3 can any one help me

getting the color of pixels on the screen in Python3.x

I want to get the color of pixels on the screen in Python3.x. (example x,y)
I work on windows. and using tkinter.
But, It can't use PIL in Python 3.x.
How can i do this.
Thank you.
You can use PIL on Python3.x; there is a binary installer for Windows. The code might not work as is both on Python2.x and Python3.x:
try: import Image # Python 2.x
except ImportError:
from PIL import Image # Python 3.x
You could capture an area of the screen using an analog of grab() method.

Resources