#CONCLUSION #1. The irregular shapes on desktop icons are accomplished by using the alpha channel to make the edges transparent #2. You would edit the icon so the area beyond the thumbnail is transparent, then use the paste() function on all of the images to put the thumbnail on all 2000 of them. #3. The functions all served a different purpose for the master function, which was the function that edited all the pictures at once. The functions that aided it #was the function that edited specific images, and the function that returned a list of all the images. The made reusing the code easier because it was easier to understand #and develop new code that resembled it. import PIL import matplotlib.pyplot as plt import os.path import PIL.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""" #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 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) ''' 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 frame_image(image,division,r,g,b): width,height = image.size frame = PIL.Image.new('RGBA', (width, height), (0, 0, 0, 0)) drawing_layer = PIL.ImageDraw.Draw(frame) drawing_layer.rectangle([(width/division,height/division), ((width/division)*(division-1),(height/division)*(division-1))],fill=(127,0,127,255)) result = PIL.Image.new('RGBA', image.size, (r,g,b,255)) result.paste(image,(0,0),mask=frame) return result def frame_all_images(): directory = os.getcwd() new_directory = os.path.join(directory, "Framed_Images") try: os.mkdir(new_directory) except: pass images, files = get_images(directory) for x in range(len(images)): fname,ftype = files[x].split('.') new_image = modify_image(images[x],10,255,0,0) newName = os.path.join(new_directory,fname + ".png") new_image.save(newName)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question