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?

Author: Эникейщик, 2015-03-08

4 answers

To open an image using the default app on Windows:

#!/usr/bin/env python
import os

os.startfile(r'D:\picture.jpg')
 5
Author: jfs, 2015-03-31 21:45:19

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()
 4
Author: abalckin, 2019-09-04 05:11:25

Try this (the image will open in a web browser):

import webbrowser
webbrowser.open(r"D:\picture.jpg")
 2
Author: Алексей Лобанов, 2019-09-04 05:13:16

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.

enter a description of the image here

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:

enter a description of the image here

Opencv: enter a description of the image here

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

 0
Author: hedgehogues, 2020-05-06 19:35:23