无法调用将TIFF转换为PNG模块对象

2024-09-25 16:24:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用PythonMagick将TIFF转换为PNG,下面是代码

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)

我遇到过这样的错误

---------------------------------------------------------------------------
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

你知道如何修正这样的错误吗


Tags: pathfromcropimageobjimglocationcoords
1条回答
网友
1楼 · 发布于 2024-09-25 16:24:19

这解决了问题,但我欢迎任何想法

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)

相关问题 更多 >