1

I am copying a code from online example how to search the resolution of an image.

import PIL
batman = PIL.Image.open("image.jpg")
batmobile, batplane = batman.size
print(batmobile,"x",batplane)

There is attributeError. PIL has no attribute name 'Image'.

from PIL import Image
batman = Image.open"image.jpg")

If I changed like this, the code can run perfectly. So, I used print(dir(PIL)) to see attributes. There is no 'Image' attribute. I don't understand.

import datetime
batman = datetime.datetime.now()

is the same as

from datetime import datetime
batman = datetime.now()

right? I downloaded 'pillow' with pip.

white dark
  • 65
  • 6
  • The answer is already here: [Use 'import module' or 'from module import'?](https://stackoverflow.com/questions/710551/use-import-module-or-from-module-import) – ThePyGuy Jun 12 '21 at 04:49
  • I guess you mean `batman = Image.open("image.jpg")`? Opening an image with PIL works fine like `im= PIL.Image.open(i)` where `i` is the full path to the image file. In the case of a png file `im` is then an object `PIL.PngImagePlugin.PngImageFile` – braulio Jun 12 '21 at 04:58

3 Answers3

0
import module_name

This will import all the functions in that module.

from module_name import function_name

This will only import a specific function from the module.

Klaus D.
  • 13,874
  • 5
  • 41
  • 48
VFXGamer
  • 1
  • 1
  • 1
    The first one will import the module, not the *objects* inside. – Klaus D. Jun 12 '21 at 04:51
  • yeah. But did the first 'import PIL' only program do some mistakes? Please, point my mistake, so I can avoid it. – white dark Jun 12 '21 at 04:53
  • You import specific things to improve your runtime. It is correct if you have to use more than one function in your code but if you only need 1 function is it better to import that specific function. – VFXGamer Jun 12 '21 at 05:05
0

example :

import leaf
environment = leaf.tree("Oxygen")

if you import everything or that specific function from an import, you don't need to mention the function again

from leaf import *
environment = tree("Oxygen")

or

from leaf import tree
environment = tree("Oxygen")
-1

from PIL import Image is required to work with pillow (>=1.0),

you can also try using import Image directly, and it would lead to an error due to the updates.

and regarding your directory query, look into the pillow ( fork of PIL ) and it should result to your satisfaction, as the documentation includes the image module, only the access for pillow (> 1.0), does not support import Image but uses the syntax from PIL import Image.

Refer to official Pillow documentation for more.