user5586747
user5586747

Reputation:

Confusion between PIL.Image and PIL.Image.Image and the correct way to use them?

I am trying to do simple crops of the images. Here is the code

from PIL.Image import Image


def get_image_half(image, half="upper"):

    if half not in ["upper", "lower", "right", "left"]:
        raise Exception('Not a valid image half')

    img = Image.open(image)
    width, height = img.size

    if half == "upper":
        return img.crop(0, 0, width, height//2)
    elif half == "lower":
        return img.crop(0, height//2, width, height)
    elif half == "left":
        return img.crop(0, 0, width//2, height)
    else:
        return img.crop(width//2, 0, width, height)


def get_image_quadrant(image, quadrant=1):

    if not (1 <= quadrant <= 4):
        raise Exception("Not a valid quadrant")

    img = Image.open(image)
    width, height = img.size

    if quadrant == 2:
        return img.crop(0, 0, width//2, height//2)
    elif quadrant == 1:
        return img.crop(width//2, 0, width, height//2)
    elif quadrant == 3:
        return img.crop(0, height//2, width//2, height)
    else:
        return img.crop(width//2, height//2, width, height)

# test code for the functions


if __name__ == "__main__":

    import os

    dir_path = os.path.dirname(os.path.realpath(__file__))
    file = os.path.join(dir_path,"nsuman.jpeg")
    image = Image.open(file)

    for i in ["upper", "lower", "left", "right"]:
        get_image_half(image, i).show()

    for i in range(1,5):
        get_image_quadrant(image, quadrant=i)

I am getting following error.

image = Image.open(file)
AttributeError: type object 'Image' has no attribute 'open'

Quick googling led me to this link and I changed the import to

import PIL.Image

and changed code to

PIL.Image.open(image)

which gave another error

Traceback (most recent call last):
  File "quadrant.py", line 53, in <module>
    get_image_half(image, i).show()
  File "quadrant.py", line 10, in get_image_half
    img = Image.open(image)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2557, in open
    prefix = fp.read(16)
AttributeError: 'JpegImageFile' object has no attribute 'read'

The question here is how to resolve these errors and more importantly what are PIL.Image and PIL.Image.Image and what is the correct way to use them?

Upvotes: 2

Views: 4036

Answers (1)

pbuck
pbuck

Reputation: 4551

PIL.Image should open a file type object. Once opened, then you'll have a PIL.Image.Image object:

from PIL import Image

image = Image.open('/foo/myfile.png')

open(fp, mode='r')

Opens and identifies the given image file.

This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the PIL.Image.Image.load method). See PIL.Image.new.

fp: A filename (string), pathlib.Path object, or a file object. The file object must implement file.read, file.seek, and file.tell methods, and be opened in binary mode.

returns: a PIL.Image.Image object.

Upvotes: 4

Related Questions