类型错误:参数 1 必须是 ImagingCore,而不是 ImagingCore

发布于 2024-12-07 18:02:32 字数 4151 浏览 1 评论 0原文

在 Windows 下我收到此错误。如何修复PIL?

这是错误:TypeError:参数 1 必须是 ImagingCore,而不是 ImagingCore

#!/usr/bin/python
## -*- coding: utf-8 -*-

from PIL import Image, ImageFont
import ImageDraw, StringIO, string
from random import *
from math import *

import os
SITE_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))

class Captcha3d(object):

    _hypot = 4
    _xx, _yy = 35, 70
    _CIMAGE = None
    _CHARS = string.ascii_lowercase + string.ascii_uppercase + string.digits
    _TEXT = ''

    def __init__(self):
        self._CIMAGE = Image.new("RGB", (self._yy * self._hypot, self._xx * self._hypot), (255,255,255))
        self.generateCode()
        self.render()

    def imageColorAllocate(self, r,g,b):
        hexchars = "0123456789ABCDEF"
        hexcolor = hexchars[r / 16] + hexchars[r % 16] + hexchars[g / 16] + hexchars[g % 16] + hexchars[b / 16] + hexchars[b % 16]
        return int(hexcolor, 16)

    def generateCode(self):
        chars = self._CHARS
        self._TEXT = "".join(choice(chars) for x in range(randint(3, 3)))

    def getText(self):
        return self._TEXT

    def getProection(self, x1,y1,z1):
        x = x1 * self._hypot
        y = z1 * self._hypot
        z = -y1 * self._hypot

        xx = 0.707106781187
        xy = 0
        xz = -0.707106781187

        yx = 0.408248290464
        yy = 0.816496580928
        yz = 0.408248290464 # 1/sqrt(6)

        cx = xx*x + xy*y + xz*z
        cy = yx*x + yy*y + yz*z + 20*self._hypot

        return [cx, cy]

    def zFunction(self, x,y):
        z = 2.6
        if self._CIMAGE.getpixel((y/2,x/2)) == (0,0,0):
            z = 0
        if z != 0:
            z += float(randint(0,60))/100
        z += 1.4 * sin((x+self.startX)*pi/15) * sin((y+self.startY)*pi/15)
        return z

    def render(self):
        fontSans = ImageFont.truetype(os.path.join(SITE_PATH, "data", "fonts", "FreeSans.ttf"), 14)

        draw = ImageDraw.Draw(self._CIMAGE)

        whiteColor = 'white'
        draw.rectangle([0, 0, self._yy * self._hypot, self._xx * self._hypot], fill=whiteColor)

        #textColor = 'black'
        #imgtext = Image.open("i8n.png")
        #self._CIMAGE.paste(imgtext, (0,0))
        imgtext = Image.new("1", (self._yy * self._hypot, self._xx * self._hypot), (1))
        drawtext = ImageDraw.Draw(imgtext)
        drawtext.text((1,0), self._TEXT, font=fontSans, fill=0)
        self._CIMAGE.paste(imgtext, (0,0))

        #draw.text((2,0), self.text, font=fontSans, fill=textColor)

        self.startX = randint(0,self._xx)
        self.startY = randint(0,self._yy)

        crd = {}

        x = 0
        while x < (self._xx+1):
            y = 0
            while y < (self._yy+1):
                crd[str(x) + '&' + str(y)] = self.getProection(x,y,self.zFunction(x,y))
                y += 1
            x += 1

        x = 0
        while x < self._xx:
            y = 0
            while y < self._yy:
                coord = []
                coord.append((int(crd[str(x) + '&' + str(y)][0]),int(crd[str(x) + '&' + str(y)][1])))
                coord.append((int(crd[str(x+1) + '&' + str(y)][0]),int(crd[str(x+1) + '&' + str(y)][1])))
                coord.append((int(crd[str(x+1) + '&' + str(y+1)][0]),int(crd[str(x+1) + '&' + str(y+1)][1])))
                coord.append((int(crd[str(x) + '&' + str(y+1)][0]),int(crd[str(x) + '&' + str(y+1)][1])))

                c = int(self.zFunction(x,y)*32)
                linesColor = (c,c,c)
                draw.polygon(coord, fill=whiteColor, outline=linesColor)
                #draw.polygon(coord, fill=whiteColor)
                y += 1
            x += 1

        draw.rectangle([0, 0, self._xx, self._yy], fill=whiteColor)

        #draw.text((2,0), self.text, font=fontSans, fill=textColor)
        #imageString($this->image, 1, 3, 0, (microtime(true)-$this->time), $textColor);

        del draw
        #self._CIMAGE.save("image.png", "PNG")
        return [self._CIMAGE, self._TEXT]

def main():
    a = Captcha3d()
    print a.getText()

if __name__ == '__main__':
    main()

Under the Windows I get this error. How to fix PIL?

This is error: TypeError: argument 1 must be ImagingCore, not ImagingCore

#!/usr/bin/python
## -*- coding: utf-8 -*-

from PIL import Image, ImageFont
import ImageDraw, StringIO, string
from random import *
from math import *

import os
SITE_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))

class Captcha3d(object):

    _hypot = 4
    _xx, _yy = 35, 70
    _CIMAGE = None
    _CHARS = string.ascii_lowercase + string.ascii_uppercase + string.digits
    _TEXT = ''

    def __init__(self):
        self._CIMAGE = Image.new("RGB", (self._yy * self._hypot, self._xx * self._hypot), (255,255,255))
        self.generateCode()
        self.render()

    def imageColorAllocate(self, r,g,b):
        hexchars = "0123456789ABCDEF"
        hexcolor = hexchars[r / 16] + hexchars[r % 16] + hexchars[g / 16] + hexchars[g % 16] + hexchars[b / 16] + hexchars[b % 16]
        return int(hexcolor, 16)

    def generateCode(self):
        chars = self._CHARS
        self._TEXT = "".join(choice(chars) for x in range(randint(3, 3)))

    def getText(self):
        return self._TEXT

    def getProection(self, x1,y1,z1):
        x = x1 * self._hypot
        y = z1 * self._hypot
        z = -y1 * self._hypot

        xx = 0.707106781187
        xy = 0
        xz = -0.707106781187

        yx = 0.408248290464
        yy = 0.816496580928
        yz = 0.408248290464 # 1/sqrt(6)

        cx = xx*x + xy*y + xz*z
        cy = yx*x + yy*y + yz*z + 20*self._hypot

        return [cx, cy]

    def zFunction(self, x,y):
        z = 2.6
        if self._CIMAGE.getpixel((y/2,x/2)) == (0,0,0):
            z = 0
        if z != 0:
            z += float(randint(0,60))/100
        z += 1.4 * sin((x+self.startX)*pi/15) * sin((y+self.startY)*pi/15)
        return z

    def render(self):
        fontSans = ImageFont.truetype(os.path.join(SITE_PATH, "data", "fonts", "FreeSans.ttf"), 14)

        draw = ImageDraw.Draw(self._CIMAGE)

        whiteColor = 'white'
        draw.rectangle([0, 0, self._yy * self._hypot, self._xx * self._hypot], fill=whiteColor)

        #textColor = 'black'
        #imgtext = Image.open("i8n.png")
        #self._CIMAGE.paste(imgtext, (0,0))
        imgtext = Image.new("1", (self._yy * self._hypot, self._xx * self._hypot), (1))
        drawtext = ImageDraw.Draw(imgtext)
        drawtext.text((1,0), self._TEXT, font=fontSans, fill=0)
        self._CIMAGE.paste(imgtext, (0,0))

        #draw.text((2,0), self.text, font=fontSans, fill=textColor)

        self.startX = randint(0,self._xx)
        self.startY = randint(0,self._yy)

        crd = {}

        x = 0
        while x < (self._xx+1):
            y = 0
            while y < (self._yy+1):
                crd[str(x) + '&' + str(y)] = self.getProection(x,y,self.zFunction(x,y))
                y += 1
            x += 1

        x = 0
        while x < self._xx:
            y = 0
            while y < self._yy:
                coord = []
                coord.append((int(crd[str(x) + '&' + str(y)][0]),int(crd[str(x) + '&' + str(y)][1])))
                coord.append((int(crd[str(x+1) + '&' + str(y)][0]),int(crd[str(x+1) + '&' + str(y)][1])))
                coord.append((int(crd[str(x+1) + '&' + str(y+1)][0]),int(crd[str(x+1) + '&' + str(y+1)][1])))
                coord.append((int(crd[str(x) + '&' + str(y+1)][0]),int(crd[str(x) + '&' + str(y+1)][1])))

                c = int(self.zFunction(x,y)*32)
                linesColor = (c,c,c)
                draw.polygon(coord, fill=whiteColor, outline=linesColor)
                #draw.polygon(coord, fill=whiteColor)
                y += 1
            x += 1

        draw.rectangle([0, 0, self._xx, self._yy], fill=whiteColor)

        #draw.text((2,0), self.text, font=fontSans, fill=textColor)
        #imageString($this->image, 1, 3, 0, (microtime(true)-$this->time), $textColor);

        del draw
        #self._CIMAGE.save("image.png", "PNG")
        return [self._CIMAGE, self._TEXT]

def main():
    a = Captcha3d()
    print a.getText()

if __name__ == '__main__':
    main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你另情深 2024-12-14 18:02:32

我在 OSX 10.6.8、Python 2.6.5 上也会发生这种情况。我认为某些类被动态导入两次。

尝试更改

from PIL import Image, ImageFont

import Image, ImageFont

对我有用。

Also happens for me on OSX 10.6.8, Python 2.6.5. I think some class is getting dynamically imported twice.

Try changing

from PIL import Image, ImageFont

to

import Image, ImageFont

That worked for me.

策马西风 2024-12-14 18:02:32

就我而言,它解决了从 PIL 导入 ImageDraw 的情况

from PIL import ImageDraw

In my case it solved the situation to also import ImageDraw from PIL

from PIL import ImageDraw
心头的小情儿 2024-12-14 18:02:32

直接从 PIL 导入所有 PIL 东西应该总是可行的。
但是,如果混合导入,

from PIL import Image
import ImageDraw

这可能会导致两个不同的 PIL 库之间发生冲突。
如果您同时安装了 PIL 和 Pillow,则可能会发生这种情况。

我们应该始终这样做,

from PIL import Image
from PIL import ImageDraw
etc.

即具体说明要使用哪个包。

importing all PIL things directly from PIL should always work.
However, if you mix imports, as such,

from PIL import Image
import ImageDraw

This can lead to conflict between two un-identical PIL libraries.
This can happen if you have installed both PIL and Pillow

We should really always do,

from PIL import Image
from PIL import ImageDraw
etc.

I.e. be specific about which package to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文