【问题标题】:What does 'module' object has no attribute 'draw' mean? How do I fix it?“模块”对象没有“绘制”属性是什么意思?我如何解决它?
【发布时间】:2017-02-07 16:46:39
【问题描述】:

我是计算机编码的新手。我们与 Canopy 合作做 PYTHON,我们正在做图像修改。我有一个“模块”对象没有属性“绘制”错误,不知道如何修复它。我已导入以下内容:

import PIL
import os.path  
import PIL.ImageDraw            
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

我要运行的代码是:

def round_corners_of_all_images(directory=None):
    """ Saves a modfied version of each image in directory.

    Uses current directory if no directory is specified. 
    Places images in subdirectory 'modified', creating it if it does not exist.
    New image files are of type PNG and have transparent rounded corners.
    """

    if directory == None:
        directory = os.getcwd() # Use working directory if unspecified

    # Create a new directory 'modified'
    new_directory = os.path.join(directory, 'modified')
    try:
        os.mkdir(new_directory)
    except OSError:
        pass # if the directory already exists, proceed  

    #load all the images
    image_list, file_list = get_images(directory)  

    #go through the images and save modified versions
    for n in range(len(image_list)):
        # Parse the filename
        filename, filetype = file_list[n].split('.')

        # drawing the text on the picture
        draw = ImageDraw.Draw(image_list[n])
        font = ImageFont.truetype("Infinite_Stroke",size=24,index=0,encoding="unic")
        draw.text((10, 25),(0,0,255),"SAMSUNG", font=font)

        # Round the corners with radius = 30% of short side
        new_image = round_corners(image_list[n],.30)
        #save the altered image, suing PNG to retain transparency
        new_image_filename = os.path.join(new_directory, filename + '.jpg')
        new_image.save(new_image_filename)    

【问题讨论】:

  • 我相信你的大小写不正确:draw = ImageDraw.draw(image_list[n]) -> draw = ImageDraw.Draw(image_list[n])
  • 我试过了,但我仍然收到相同的消息。
  • 不要描述您的错误——复制并粘贴完整的回溯。

标签: python canopy image-editing image-editor


【解决方案1】:

从文档看来,您正在寻找的方法是 Draw(),而不是 draw()

http://pillow.readthedocs.io/en/3.1.x/reference/ImageDraw.html

试试这个

draw = ImageDraw.Draw(image_list[n])

【讨论】:

  • 我试过了,但我仍然收到相同的消息。
  • 您可以编辑您的帖子以显示新代码吗?我发现您仍然会收到相同的错误消息,这很奇怪。
  • 也许再试一次 pip install PIL?检查您是否将模块安装在您可以访问的目录中?我想不出别的了
【解决方案2】:

老问题,但可能仍然相关的答案:这里的错误是在 ImageDraw 的双重导入中,以以下方式重写导入解决了我的问题:

from PIL import ImageFont, ImageDraw

【讨论】:

    猜你喜欢
    • 2018-02-18
    • 2019-10-31
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 2020-11-24
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多