How do i read image using PILLOW image?

I wanted read a image using PIL.Image.open().But I've image in different path. The following is the path I've the python script

"D:\YY_Aadhi\holy-edge-master\hed\test.py"

The following is the path I've the image file.

"D:\YY_Aadhi\HED-BSDS\test\2018.jpg"

from PIL import Image
'''some code here'''
image = Image.open(????)

How should I fill the question mark to access the image file.

Upvotes: 8

Views: 55522

Answers (4)

Aghahowa Jeffrey
Aghahowa Jeffrey

Reputation: 1

try this, it should work

from PIL import Image
import os
'''some code here'''

image = Image.open(r"D:\YY_Aadhi\HED-BSDS\test\2018.jpg")

Make sure you add the r letter and also use backslash

if this doesn't work then it could mean your image file path is wrong, use this to get the correct file path

from PIL import Image
'''some code here'''

file_name = '2018.jpg'
file_path = os.path.abspath(file_name)
image = Image.open(file_path)

Upvotes: 0

Codertjay
Codertjay

Reputation: 997

You can use this to read an online image

from urllib.request import urlopen

url = 'https://somewebsite/images/logo.png'
msg_image = urlopen(url).read()

Upvotes: 0

SaGwa
SaGwa

Reputation: 602

you can simply do

from PIL import Image
image = Image.open("D:\\YY_Aadhi\\HED-BSDS\\test\\2018.jpg")

or

from PIL import Image
directory = "D:\\YY_Aadhi\\HED-BSDS\\test\\2018.jpg"
image = Image.open(directory)

like this.

you have to write escape sequence twice in windows, when you want to define as directory. and It will be great if you try some stupid code. It helps you a lot.

Upvotes: 30

MuadDev
MuadDev

Reputation: 452

Does this image = Image.open("D:\YY_Aadhi\HED-BSDS\test\2018.jpg") not do the trick?

Upvotes: 3

Related Questions