简体   繁体   English

如何从不同的模块导入两个不同的东西?

[英]How to import two different things called the same from different modules?

I have two modules, first one is PIL, and the second one is wand, which I import from PIL and wand something called Image, and there is conflict, how do I overcome this problem?我有两个模块,第一个是 PIL,第二个是 wand,我从 PIL 导入并使用一个叫做 Image 的东西,并且有冲突,我该如何克服这个问题?

from PIL import Image, ImageDraw

img = Image.new('RGB', (1600,1600), (0,0,0)) 
draw = ImageDraw.Draw(img)

red = (255,0,0)
for x in range(0, 1600, 200):
    for y in range(0, 1600, 200):
        draw.rectangle(((0+x,0+y),(99+x,99+y)), red)
for x in range(100, 1600, 200):
    for y in range(100, 1600, 200):
        draw.rectangle(((0+x,0+y),(99+x,99+y)), red)


from wand.image import Image
img.swirl(degree =-90)

img

I have this error...我有这个错误...

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [9], in <cell line: 18>()
     14         draw.rectangle(((0+x,0+y),(99+x,99+y)), top)
     16 from wand.image import Image
---> 18 img.swirl(degree =-90)
     20 img

File /usr/lib/python3.10/site-packages/PIL/Image.py:548, in Image.__getattr__(self, name)
    541     warnings.warn(
    542         "Image categories are deprecated and will be removed in Pillow 10 "
    543         "(2023-07-01). Use is_animated instead.",
    544         DeprecationWarning,
    545         stacklevel=2,
    546     )
    547     return self._category
--> 548 raise AttributeError(name)

AttributeError: swirl

EDIT编辑

from PIL import Image, ImageDraw

img = Image.new('RGB', (1600,1600), (0,0,0)) 
draw = ImageDraw.Draw(img)

top = (255,0,0)

for x in range(0, 1600, 200):
    for y in range(0, 1600, 200):
        draw.rectangle(((0+x,0+y),(99+x,99+y)), top)
        
for x in range(100, 1600, 200):
    for y in range(100, 1600, 200):
        draw.rectangle(((0+x,0+y),(99+x,99+y)), top)

from wand.image import Image as ooo
        
with ooo(img) as img:
    img.swirl(degree =-90)

img

I tried like this but got this error...我试过这样但得到这个错误......

TypeError: image must be a wand.image.Image instance, not <PIL.Image.Image image mode=RGB size=1600x1600 at 0x7FE681031600>
from wand.image import Image as <an_alias>

You can either use an alias as mhmtsrmn described:您可以使用 mhmtsrmn 描述的别名:

from wand.image import Image as <an_alias>

Or you can simply import modules and use full names:或者您可以简单地导入模块并使用全名:

import PIL
import wand.image

img = PIL.Image.new('RGB', (1600,1600), (0,0,0)) 
draw = PIL.ImageDraw.Draw(img)

with wand.image.Image(filename='file.png') as wand_img:
   ...

This might also help clear misunderstanding around your issue: .swirl() is a function on wand.image.Image , not on PIL.Image .这也可能有助于消除对您的问题的误解: .swirl()wand.image.Image上的函数,而不是PIL.Image上的函数。 Since these are two separate classes, you cannot simply call .swirl() on an instance of PIL.Image .由于这是两个独立的类,您不能简单地在PIL.Image的实例上调用.swirl()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM