首页 > 解决方案 > python中的枕头矩形不让我有足够的论据

问题描述

我已经尝试运行此代码并根据此 youtube视频制作类似骰子的图像版本, 但出现错误:

runfile('C:/Users/Sohrab/Desktop/temp.py', wdir='C:/Users/Sohrab/Desktop')
Traceback (most recent call last):

  File "C:\Users\Sohrab\Desktop\temp.py", line 25, in <module>
    nimd.rectangle([x, y, x+dicesize, y+dicesize],thisSectorColor, thisSectorColor)

  File "C:\Users\Sohrab\anaconda3\lib\site-packages\PIL\ImageDraw.py", line 248, in rectangle
    ink, fill = self._getink(outline, fill)

  File "C:\Users\Sohrab\anaconda3\lib\site-packages\PIL\ImageDraw.py", line 113, in _getink
    ink = self.draw.draw_ink(ink)

SystemError: new style getargs format but argument is not a tuple

这是代码:

from PIL import Image,ImageOps,ImageDraw
img = Image.open('best.jpg')

img = ImageOps.grayscale(img)
img = ImageOps.equalize(img)

dicew = 100

dicesize = int( img.width * 1.0 / dicew )
diceh = int(img.height * 1.0 / img.width * dicew)

# print(img.width,img.height, dicew, diceh, dicesize)

nim = Image.new('L', (img.width,img.height), 'white')
nimd = ImageDraw.Draw(nim)

for y in range( 0 , img.height - dicesize , dicesize ):
    for x in range(0, img.width-dicesize, dicesize):
        thisSectorColor = 0
        for dicex in range(0,dicesize):
            for dicey in range(0,dicesize):
                thisColor=img.getpixel((x+dicex, y+dicey))
                thisSectorColor += thisColor
        thisSectorColor /= (dicesize ** 2)
        nimd.rectangle([(x, y), (x+dicesize, y+dicesize)],thisSectorColor)
nim.show()

我在 Spyder 上运行了内核 3.8 的代码,并且已经安装了 Pillow 库。提前致谢。

标签: python

解决方案


推荐阅读