maheshakya
maheshakya

Reputation: 2238

importing a JPEG image into an IPython (notebook) program

I'm using a IPython notebook to write a program using pygame library. Here, I want to import an image file to the programme. Following is the code piece I would have used.

car = pygame.image.load('car.jpg')

But to do this, the image has to be in the same directory as the python file. Where are these IPython notebook files saved? Or is there any other way I can import this file to program in IPython notebook?

I'm using windows 7.

Upvotes: 0

Views: 978

Answers (1)

unutbu
unutbu

Reputation: 879759

There are an number of ways to change directories or specify full paths:

  • IPython understands the cd command to change directories (if 'automagic' is enabled; use %cd if not), or
  • use os.chdir, or
  • specify a path:

    load('/path/to/car.jpg')
    

    or

    import os
    load(os.path.expanduser('~/path/to/car.jpg')) # relative to user's home directory
    

    or

     IMAGEDIR = '/path/to/images'
     load(os.path.join(IMAGEDIR, 'car.jpg'))
    

Although cd is convenient while working in IPython, for your program you are better off specifying a full path to car.jpg.

Upvotes: 1

Related Questions