Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to make pictures whit python

from PIL import Image, ImageDraw, ImageFont
  
# create Image object
text1 = 'Create Feature Image'
text2 = 'With Python'
img_name = 'featured-image-creation-with-python.png'
color = 'dark_blue' #grey,light_blue,blue,orange,purple,yellow,green
font = 'Roboto-Bold.ttf'
 
background = Image.open('default.jpg')
foreground = Image.open('python-logo.png')
 
#create the coloured overlays
colors = {
    'dark_blue':{'c':(27,53,81),'p_font':'rgb(255,255,255)','s_font':'rgb(255, 212, 55)'},
    'grey':{'c':(70,86,95),'p_font':'rgb(255,255,255)','s_font':'rgb(93,188,210)'},
    'light_blue':{'c':(93,188,210),'p_font':'rgb(27,53,81)','s_font':'rgb(255,255,255)'},
    'blue':{'c':(23,114,237),'p_font':'rgb(255,255,255)','s_font':'rgb(255, 255, 255)'},
    'orange':{'c':(242,174,100),'p_font':'rgb(0,0,0)','s_font':'rgb(0,0,0)'},
    'purple':{'c':(114,88,136),'p_font':'rgb(255,255,255)','s_font':'rgb(255, 212, 55)'},
    'red':{'c':(255,0,0),'p_font':'rgb(0,0,0)','s_font':'rgb(0,0,0)'},
    'yellow':{'c':(255,255,0),'p_font':'rgb(0,0,0)','s_font':'rgb(27,53,81)'},
    'yellow_green':{'c':(232,240,165),'p_font':'rgb(0,0,0)','s_font':'rgb(0,0,0)'},
    'green':{'c':(65, 162, 77),'p_font':'rgb(217, 210, 192)','s_font':'rgb(0, 0, 0)'}
    }
 
def add_color(image,c,transparency):
    color = Image.new('RGB',image.size,c)
    mask = Image.new('RGBA',image.size,(0,0,0,transparency))
    return Image.composite(image,color,mask).convert('RGB')
 
def center_text(img,font,text1,text2,fill1,fill2):
    draw = ImageDraw.Draw(img)
    w,h = img.size
    t1_width, t1_height = draw.textsize(text1, font)
    t2_width, t2_height = draw.textsize(text2, font)
    p1 = ((w-t1_width)/2,h // 3)
    p2 = ((w-t2_width)/2,h // 3 + h // 5)
    draw.text(p1, text1, fill=fill1, font=font)
    draw.text(p2, text2, fill=fill2, font=font)
    return img
 
def add_text(img,color,text1,text2,logo=False,font='Roboto-Bold.ttf',font_size=75):
    draw = ImageDraw.Draw(img)
 
    p_font = color['p_font']
    s_font = color['s_font']
     
    # starting position of the message
    img_w, img_h = img.size
    height = img_h // 3
    font = ImageFont.truetype(font,size=font_size)
 
    if logo == False:
        center_text(img,font,text1,text2,p_font,s_font)
    else:
        text1_offset = (img_w // 4, height)
        text2_offset = (img_w // 4, height + img_h // 5)
        draw.text(text1_offset, text1, fill=p_font, font=font)
        draw.text(text2_offset, text2, fill=s_font, font=font)
    return img
 
def add_logo(background,foreground):
    bg_w, bg_h = background.size
    img_w, img_h = foreground.size
    img_offset = (20, (bg_h - img_h) // 2)
    background.paste(foreground, img_offset, foreground)
    return background
 
def write_image(background,color,text1,text2,foreground=''):
    background = add_color(background,color['c'],25)
    if not foreground:
        add_text(background,color,text1,text2)
    else:
        add_text(background,color,text1,text2,logo=True)
        add_logo(background,foreground)
    return background
 
if __name__ == '__main__':
    background = write_image(background,colors[color],text1,text2,foreground=foreground)
    background.save(img_name)
Comment

PREVIOUS NEXT
Code Example
Python :: Python getting content from xl range 
Python :: import image files from folders 
Python :: auto reload exml odoo 13 
Python :: Python Anagram Using Counter() function 
Python :: how to un register DefaultAdminSite in django 
Python :: Code Example of Comparing None with None type 
Python :: Flatten List in Python Using Lambda Function 
Python :: accessing multiple elements from the list 
Python :: how to change multiple index in list in python 
Python :: pandas check if string has only spaces 
Python :: stop level of the broker 
Python :: python swap two numbers 
Python :: Python slides 
Python :: how to add item to a list in pithon 
Python :: How to use glob.escape() function in python 
Python :: attach short list to pandas dataframe with filler 
Python :: Python NumPy atleast_2d Function Syntax 
Python :: how to murj record in django 
Python :: python generate string of length 
Python :: Python NumPy block Function Example by using simple array 
Python :: configure socketio static file python specific content type 
Python :: maximaze window in tkinter 
Python :: python cos not the same as calculator 
Python :: Program to illustrate the use of nested if statement Average in python Grade =80 and above A =70 and <80 B =60 and <70 C =50 and <60 D Otherwise 
Python :: django view - Generic class based view (listc, create, retrieve, update or delete - GET, POST, GET, PUT, DELETE) 
Python :: instance variables python 
Python :: Creating a Dictionary using built-in function dict() 
Python :: how to remove a strech in pyqt5 
Python :: how to check all possible combinations algorithm python 
Python :: Fetch all links from a given webpage 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =