Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Crop Image as Circle with transparent background

from PIL import Image, ImageDraw

filename = 'dog.jpg'

# load image
img = Image.open(filename)

# crop image 
width, height = img.size
x = (width - height)//2
img_cropped = img.crop((x, 0, x+height, height))

# create grayscale image with white circle (255) on black background (0)
mask = Image.new('L', img_cropped.size)
mask_draw = ImageDraw.Draw(mask)
width, height = img_cropped.size
mask_draw.ellipse((0, 0, width, height), fill=255)
#mask.show()

# add mask as alpha channel
img_cropped.putalpha(mask)

# save as png which keeps alpha channel 
img_cropped.save('dog_circle.png')

img_cropped.show()
Comment

PREVIOUS NEXT
Code Example
Python :: build the .pyx file to c and run on python 
Python :: mongoclient python 
Python :: python groupby 1d array 
Python :: python get all keys in dict having value in range 
Python :: python vs python3 
Python :: url encoding in python 
Python :: Incrémenter/décrémenter variable python 
Python :: trigger to print on python 
Python :: python if block 
Python :: matplotlib add abline 
Python :: safe password in python 
Python :: Count the number of Non-Missing Values in the DataFrame 
Python :: postgtres settings.py setup with django 
Python :: dataset to list python 
Python :: design patterns python - restrict what methods of the wrapped class to expose 
Python :: pythonanywhere api 
Python :: change value of element 
Python :: loop through KeyedVectors 
Python :: python literation (looping) 
Python :: Create New Entry Into Table Django 
Python :: make max function returning more than one value python 
Python :: sum function in python 
Python :: Python - Perl - Tcl 
Python :: Matrix Transpose using Nested Loop 
Python :: upper method in python 
Python :: how to input a string character into a numpy zeros imatrix n python 
Python :: sqlalchemy filter getattr 
Python :: python initialize a 2d array 
Python :: vbscript shutdown remote computer 
Python :: assert_series_equal 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =