4

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.

3 Answers3

18

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.

SaGwa
  • 462
  • 3
  • 5
3

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

MuadDev
  • 392
  • 3
  • 12
  • Its giving following error " Error with image file D:\YY_Aadhi\HED-BSDS\test\100007.jpg cannot identify image file 'D:\\YY_Aadhi\\HED-BSDS\\test\\100007.jpg'" – Thiruvettai Athithiyan S Nov 13 '17 at 09:19
  • This error indicates that there is something wrong with the file. Although it might also be a bug in Pillow, see here: https://stackoverflow.com/a/20863145/8488985 – MuadDev May 29 '18 at 07:07
0

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()
Codertjay
  • 588
  • 8
  • 13