使用声明的类时无法找到PIL模块

时间:2017-08-22 09:06:16

标签: python python-imaging-library python-3.6

我用Pillow编写了一个简单的Python脚本,在1024x256图像上生成一个网格。

然后我定义了一个类,它更像是一个函数集合。

class Letters:

    COLOR_GREEN = (0, 255, 0)
    COLOR_BLUE = (0, 0, 255)
    COLOR_BLACK = (0, 0, 0)
    class A:
        margin_top = 64
        margin_bottom = 64
        section_margin_empty = 16
        section_margin_filled = [i for i in range(0, 15)]

        @staticmethod
        def draw(pixelarray, margin_left):

            draw_range_height = [i for i in range(margin_bottom, 256 - (6 * 16))]
            draw_range_width = [i for i in range(margin_left, margin_left + 15)]

            for j in draw_range_width:
                for i in draw_range_height:
                    if pixelarray[j, i] == (0, 0, 0):
                        pixelarray[j, i] = COLOR_GREEN

            return pixelarray

但问题似乎并不在课堂上......

我的主要代码是:

from PIL import Image
import os

if os.path.exists("banner.png"):
    img = Image.open("banner.png")
else:
    img = Image.new("RGB", (1024, 256), "black")

WIDTH, HEIGHT = img.size
pixels = img.load()

for i in range(0, HEIGHT):
    for j in range(0, WIDTH):
        if j % 16 == 0:
            pixels[j, i] = (0, 0, 255)
        else:
            if i % 16 == 0:
                pixels[j, i] = (0, 0, 255)
            else:
                pixels[j, i] = (0, 0, 0)

pixels = Letters.A.draw(pixels, 33)        # <= Error is here

img.save("banner.png")

出于某种原因,当我尝试使用函数Letters.A.draw()时,脚本在行1中与ImportError: Module PIL not found!

崩溃

当我在那里有那条线时怎么会不起作用,但当我删除它时,它工作正常??

确切的错误讯息:

Traceback (most recent call last):
  File "msp.py", line 1, in <module>
    from PIL import Image
ImportError: No module named PIL

1 个答案:

答案 0 :(得分:0)

显然,它似乎是IDE的一个问题。 使用VisualStudio 2017 python工具它不起作用,但使用LiCLipse它。 奇怪的是,但使用LiClipse帮助了我。

相关问题