Convert TIFF to PNG module object is not callable

170 Views Asked by At

I am trying to convert TIFF to PNG using PythonMagick and this is the code

from PythonMagick import Image
from PIL import Image

sImage = 'MySample.tiff'
sOutput = 'MyOutput.png'
sCropped = 'Cropped.png'

def crop(img, image_path, coords, saved_location):
    Image(img).write(image_path)
    image_obj = Image.open(image_path)
    cropped_image = image_obj.crop(coords)
    cropped_image.save(saved_location)
    # cropped_image.show()

if __name__ == '__main__':
    crop(sImage, sOutput, (440, 145, 770, 195), sCropped)

I have encountered an error like that

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-f77d54075818> in <module>
     14 
     15 if __name__ == '__main__':
---> 16     crop(sImage, sOutput, (440, 145, 770, 195), sCropped)

<ipython-input-16-f77d54075818> in crop(img, image_path, coords, saved_location)
      7 
      8 def crop(img, image_path, coords, saved_location):
----> 9     Image(img).write(image_path)
     10     image_obj = Image.open(image_path)
     11     cropped_image = image_obj.crop(coords)

TypeError: 'module' object is not callable

Any idea how to fix such an error?

1

There are 1 best solutions below

0
On

This solved the problem but I welcome any ideas

from PythonMagick import Image as ImageA
from PIL import Image as ImageB

sImage = 'MySample.tiff'
sOutput = 'MyOutput.png'
sCropped = 'Cropped.png'

def crop(img, image_path, coords, saved_location):
    ImageA(img).write(image_path)
    image_obj = ImageB.open(image_path)
    cropped_image = image_obj.crop(coords)
    cropped_image.save(saved_location)

crop(sImage, sOutput, (440, 145, 770, 195), sCropped)