Home > OS >  how do you insert an image into a python program?
how do you insert an image into a python program?

Time:05-01

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. thank in advance anyone who can give me a hand to solve the problem.

greetings Gaia.

CodePudding user response:

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)

CodePudding user response:

You can use theses modules:

opencv-python:

import cv2

Pillow:

from PIL import Image

scikit-image

import skimage
  • Related