新编码并且不了解我得到的错误。下面是我的代码和追溯:
import PIL
import matplotlib.pyplot as plt # single use of plt is commented out
import os.path
import PIL.ImageDraw
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
def round_corners(original_image, percent_of_side):
""" Rounds the corner of a PIL.Image
original_image must be a PIL.Image
Returns a new PIL.Image with rounded corners, where
0 < percent_of_side < 1
is the corner radius as a portion of the shorter dimension of original_image
"""
font = ImageFont.truetype("arial.ttf", 15)
# Opening the file gg.png
imageFile = ("new.jpg")
im1=Image.open(imageFile)
# Drawing the text on the picture
draw = ImageDraw.Draw(im1)
draw.text((0,0), "SAMSUNG",(0,0,255),font=font)
im1.save("marked_image.png")
#set the radius of the rounded corners
width, height = original_image.size
radius = int(percent_of_side * min(width, height)) # radius in pixels
###
#create a mask
###
#start with transparent mask
rounded_mask = PIL.Image.new('RGBA', (width, height), (127,0,127,0))
drawing_layer = PIL.ImageDraw.Draw(rounded_mask)
# Overwrite the RGBA values with A=255.
# The 127 for RGB values was used merely for visualizing the mask
# Draw two rectangles to fill interior with opaqueness
drawing_layer.polygon([(radius,0),(width-radius,0),
(width-radius,height),(radius,height)],
fill=(127,0,127,255))
drawing_layer.polygon([(0,radius),(width,radius),
(width,height-radius),(0,height-radius)],
fill=(127,0,127,255))
#Draw four filled circles of opaqueness
drawing_layer.ellipse((0,0, 2*radius, 2*radius),
fill=(0,127,127,255)) #top left
drawing_layer.ellipse((width-2*radius, 0, width,2*radius),
fill=(0,127,127,255)) #top right
drawing_layer.ellipse((0,height-2*radius, 2*radius,height),
fill=(0,127,127,255)) #bottom left
drawing_layer.ellipse((width-2*radius, height-2*radius, width, height),
fill=(0,127,127,255)) #bottom right
# Uncomment the following line to show the mask
# plt.imshow(rounded_mask)
# Make the new image, starting with all transparent
result = PIL.Image.new('RGBA', original_image.size, (0,0,0,0))
result.paste(original_image, (0,0), mask=rounded_mask)
return result
def get_images(directory=None):
""" Returns PIL.Image objects for all the images in directory.
If directory is not specified, uses current directory.
Returns a 2-tuple containing
a list with a PIL.Image object for each image file in root_directory, and
a list with a string filename for each image file in root_directory
"""
if directory == None:
directory = os.getcwd() # Use working directory if unspecified
image_list = [] # Initialize aggregaotrs
file_list = []
directory_list = os.listdir(directory) # Get list of files
for entry in directory_list:
absolute_filename = os.path.join(directory, entry)
try:
image = PIL.Image.open(absolute_filename)
file_list += [entry]
image_list += [image]
except IOError:
pass # do nothing with errors tying to open non-images
return image_list, file_list
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('.')
# 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 + '.png')
new_image.save(new_image_filename)
有完整的代码,这里是追溯:
IOError Traceback (most recent call last)
<ipython-input-19-8a0b83840aa4> in <module>()
----> 1 round_corners_of_all_images()
C:\Users\ewainscott2019\Desktop\Computer Science\Semester 2\Image Artist\Image Artist Code.py in round_corners_of_all_images(directory)
124
125 # Round the corners with radius = 30% of short side
--> 126 new_image = round_corners(image_list[n],.30)
127 #save the altered image, suing PNG to retain transparency
128 new_image_filename = os.path.join(new_directory, filename + '.png')
C:\Users\ewainscott2019\Desktop\Computer Science\Semester 2\Image Artist\Image Artist Code.py in round_corners(original_image, percent_of_side)
20 # Opening the file gg.png
21 imageFile = ("new.jpg")
---> 22 im1=Image.open(imageFile)
23
24 # Drawing the text on the picture
C:\Users\ewainscott2019\AppData\Local\Enthought\Canopy\User\lib\site-packages\PIL\Image.pyc in open(fp, mode)
2270 filename = str(fp.resolve())
2271 if filename:
-> 2272 fp = builtins.open(filename, "rb")
2273
2274 try:
IOError: [Errno 2] No such file or directory: 'Phone_1.jpg'
我不理解我在代码中遇到的错误。我曾尝试在其他地方接受帮助,但无法找到任何东西。这是一个过期的学校项目,我需要完成它。