Reputation: 1
I am new to computer coding. We work with Canopy to do PYTHON and we are doing image modification. I have a 'module' object has no attribute 'draw' error and am not sure how to fix it. I have imported the following:
import PIL
import os.path
import PIL.ImageDraw
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
And the code I am trying to run is:
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)
Upvotes: 0
Views: 6861
Reputation: 1
In the site-packages\ultralytics\utils\plotting.py
based on line 121 i.e. self.draw = ImageDraw.Draw(self.im)
you must just replace all self.draw.rectangle
by self.rectangle
Upvotes: 0
Reputation: 1250
Old question but maybe still relevant answer: the error here is in the double import of ImageDraw, rewriting the imports in the following way salved the problem on my end:
from PIL import ImageFont, ImageDraw
Upvotes: 0
Reputation: 1138
From the documentation it looks like the method you are looking for is Draw(), not draw()
http://pillow.readthedocs.io/en/3.1.x/reference/ImageDraw.html
Try this
draw = ImageDraw.Draw(image_list[n])
Upvotes: 1