How do I open an image using Python?
My code:
import os
os.system("D\picture.jpg")
It works in the console, but not in pycharm... what to do?
4 answers
To open an image using the default app on Windows:
#!/usr/bin/env python
import os
os.startfile(r'D:\picture.jpg')
Open probably means upload for display (or processing)? If so, try using PIL, something like this:
from PIL import Image
#...
img = Image.open(r'D:\picture.jpg')
img.show()
Try this (the image will open in a web browser):
import webbrowser
webbrowser.open(r"D:\picture.jpg")
You can do this, for example, by using specialized libraries for image processing and data processing. opencv requires additional packages to be installed. But, at the same time, it allows, among other things, a wide range of possibilities for image processing. The second matplotlib is easy to install, integrates into PyCharm, and allows you to use a fairly convenient built-in interface for viewing. Unfortunately, this library does not provide the ability to image processing, but only for their visualization.
Opencv
import cv2
img = cv2.imread('x.jpg', 0)
cv2.imshow('', img)
cv2.waitKey(0)
Matplotlib:
from matplotlib import pyplot as plt
img = plt.imread('x.jpg')
plt.imshow(img)
plt.show()
Matplotlib:
Installing opencv:
sudo apt-get install -y cmake make libsm6 libxext6 libxrender-dev
git clone https://github.com/opencv/opencv.git
mkdir ./opencv/build
cd ./opencv/build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j4
make install
The j flag specifies the number of processes that will be used during the installation of
Installing matplotlib:
pip install matplotlib
pip install pillow
If the pillow library does not if it is installed, you can only use * . png