如何从不同的模块导入两个不同的东西?
How to import two different things called the same from different modules?
我有两个模块,第一个是PIL,第二个是wand,我是从PIL导入的,wand一个叫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
我有这个错误...
---------------------------------------------------------------------------
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
编辑
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
我这样试过但是出现了这个错误...
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>
您可以使用 mhmtsrmn 描述的别名:
from wand.image import Image as <an_alias>
或者您可以简单地导入模块并使用全名:
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:
...
这也可能有助于消除对您的问题的误解:.swirl()
is a function on wand.image.Image
, not on PIL.Image
。由于它们是两个独立的 类,您不能简单地在 PIL.Image
.
的实例上调用 .swirl()
我有两个模块,第一个是PIL,第二个是wand,我是从PIL导入的,wand一个叫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
我有这个错误...
---------------------------------------------------------------------------
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
编辑
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
我这样试过但是出现了这个错误...
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>
您可以使用 mhmtsrmn 描述的别名:
from wand.image import Image as <an_alias>
或者您可以简单地导入模块并使用全名:
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:
...
这也可能有助于消除对您的问题的误解:.swirl()
is a function on wand.image.Image
, not on PIL.Image
。由于它们是两个独立的 类,您不能简单地在 PIL.Image
.
.swirl()