How do you insert an image into a Python program? - python

I'm trying to insert an image into a Python program, but I just can't figure out how to link the image to code so I can view it once the code is run. I am new to python, I have version 3.9.7 on a Mac computer.
Thanks in advance to anyone who can give me a hand in solving the problem.
Greetings, Gaia.

You can use OpenCV for this
https://learnopencv.com/read-display-and-write-an-image-using-opencv/
You need to install the library first, running the following command on your terminal pip install opencv-python
# import the cv2 library
import cv2
# The function cv2.imread() is used to read an image.
img_grayscale = cv2.imread('test.jpg',0)
# The function cv2.imshow() is used to display an image in a window.
cv2.imshow('graycsale image',img_grayscale)
# waitKey() waits for a key press to close the window and 0 specifies indefinite loop
cv2.waitKey(0)
# cv2.destroyAllWindows() simply destroys all the windows we created.
cv2.destroyAllWindows()
# The function cv2.imwrite() is used to write an image.
cv2.imwrite('grayscale.jpg',img_grayscale)

You can use theses modules:
opencv-python:
import cv2
Pillow:
from PIL import Image
scikit-image
import skimage

Related

QRCode generated in python

I wrote some code to generate qrcode in python but at run time it shows this error:
Import "qrcode" could not be resolved Pylance(reportMissingImports)
I am using the qrcode library but I am having some problems.
Installation is pretty straight forward via pip install. Run the following command to install python-qrcode and pillow.
pip install qrcode[pil]
Once you are done, continue installing OpenCV-Python with the following command:
pip install opencv-python
If you intend to detect multiple QR codes in a single image, make sure that the opencv-python version is at least 4.3.0. Older versions do not come with the multi detection functionalities.
Add the following import declaration at the top of your Python file.
import qrcode
from PIL import Image
For basic usage, you can simply run the make() function to generate a QR Code based on your input text:
img = qrcode.make('Your input text')
You can utilize the QRCode class, which comes with a lot more controls and properties.
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
The next step is to call the add_data() function. Pass in the input text of your choice. Continue by appending the following code to generate a QR code with a white background and black fill.
qr.add_data('https://medium.com/#ngwaifoong92')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white").convert('RGB')
You can save it in as an image file as follow:
img.save("sample.png")
Finally you can read this paper.

Hello, i want to perform OCR on image i tried the code

I am performing OCR on an imenter image description hereage, for that i installed pytesseract library, code i have used is requiring one more library "Image", what would be the cmd code in windows for installation that package. I have tried --> "pip install image" that's not working, or is this code right
code is this
import image
import pytesseract
print pytesseract.image_to_string(image.open('ocr.jpg'))
May be the error is appearing because you used lower case image.open while in documentation its Image.open
Try this:
import Image
import pytesseract
# Simple image to string
print(pytesseract.image_to_string(Image.open('ocr.jpg')))

I am not able to import an image into python using the import image way

Before you ask, I did try putting an image in it, and it didn't work like look at this.
import Image
Image = Image.open('clerky.jpeg')
Image.show()
and this code above comes up with this error below.
import Image
ImportError: No module named 'Image'
If you're using Pillow, the correct usage is
from PIL import Image
To install Pillow on Windows, start the Command Prompt application (or hit WinR and type cmd, then hit Enter). Type
pip install Pillow
hit Enter, and everything should install automatically.
I think you mean from PIL import Image
from PIL import Image
PIL (Python Image Library) has to be installed on your system, AKA "pil" and "Pillow" from time to time. effbot.org/imagingbook/pil-index.htm

Video Processing using Python and OpenCV

I am trying to capture a video and convert into frames using python and openCV. I followed the steps that need to done to import openCV2 libraries for python in windows 8 which is as follows:
Download Python, Numpy, OpenCV from their official sites.
Extract OpenCV (will be extracted to a folder opencv)
Copy ..\opencv\build\python\x86\2.7\cv2.pyd
Paste it in C:\Python27\Lib\site-packages
Open Python IDLE or terminal, and type
I have done the above steps to import opencv libraries into python.
But when I import CV2 libraries and try to capture a video, I was unable to generate frames using the function cv.CaptureFromFile() and accessing the frames using cv.QueryFrame() functions. I was unable to capture the frames. Please find the code below.
import sys
import os
import numpy as np
import PIL
from matplotlib import pyplot as plt
import cv2.cv as cv
winname = "myWindow"
win = cv.NamedWindow(winname, cv.CV_WINDOW_AUTOSIZE)
invideo = cv.CaptureFromFile("chayya1.avi")
totalNumberOfFrames = int(cv.GetCaptureProperty(invideo, cv.CV_CAP_PROP_FRAME_COUNT))
framesprocessing = totalNumberOfFrames
print(totalNumberOfFrames)
while (True):
im = cv.QueryFrame(invideo)
cv.ShowImage(winname, im)
if cv.WaitKey() == 27: # ASCII 27 is the ESC key
break
del invideo
cv.DestroyWindow(winname)
The output is getting processed without any errors but the frames are not generating and even the print statement for frame count is also zero.
I just want to what is procedure for installing OpenCV for python in windows and accessing the frames of a video using the obove mentioned functions.
Thanks in advance
Using opencv to read video files
As mentioned in the tutorial here, the recommended approach now is to use cv2 (not cv as you did) to read video files using, e.g.
import numpy as np
import cv2
cap = cv2.VideoCapture("chayya1.avi")
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Python & OpenCV installation on windows
For a simple solution to installing OpenCV's python bindings on windows you might want to try the Python(xy) distribution which bundles a bunch of python modules including opencv.
Python's opencv bindings are included with opencv, so if you didn't get any import errors then your opencv setup is likely to be correct.
Alternative bindings
If you don't like the native bindings, you could try using pip from the command line to install 3rd party bindings, though as berak pointed out this is not generally recommended;
pip install pyopencv
Pip itself can be installed as outlined in this answer

No highgui in python

I'm trying to write some programs using opencv and python. I installed opencv and the python libraries from the repositories. I'm using ubuntu 12.04. The folder /var/lib/python-support/python2.7 contains just 5 files :
cv2.so
cv.py
cv.pyc
simplegeneric-0.7.egg-info
simplegeneric.py
simplegeneric.pyc
From what reading I have done, I think there's supposed to be an opencv folder around here. I'm able to import cv library using
import cv
but
from opencv import cv
And I cannot load the highgui module. Any way to work around this? I would really really like to do something in opencv
You must have installed OpenCV >= 2.3.1. In OpenCV 2.3.1 and higher, Python bindings do not have highgui.
import cv2
import cv2.cv as cv
and you're good to go.
import cv2
img = cv2.imread("image name")
cv2.imshow("window name", img)
cv2.waitKey(0)
You can find more help on OpenCV docs and you could also look at some of the opwncv stuff that I have been doing here.
I hope this helps.
You will get the highgui file in OpenCV folder(installed folder) in include/opencv/highgui.h.

Categories