I use the text as a variable to make images #easy to keep track of. Out = Image.alpha_composite(img, im) #Merge base image with text. #Fill takes 4 arguments, the first 3 are RGB values (use google color picker) #Text placement is calculated based on the size of the final text & image. W,h = myFont.getsize(msg) #Gets the size of the text Im = Image.new("RGBA",(W,H),(255, 255, 255, 0)) #Create a new, transparent image to add text toĭraw = ImageDraw.Draw(im) #create drawing context MyFont = uetype("OpenSans-ExtraBold.ttf", 200) #once done with edits to the main image we move on to the text Img.putdata(newData) #you'll get your new img ready Img=Image.open('/content/image1.jpg').convert("RGBA")ĭata=img.getdata() #you'll get a list of tuplesĪ=a #you'll get your tuple shorten to RGBĪ=a+(75,) #change the 100 to any transparency number you like between (0,255) #This first part opens your image and adds transparency from PIL import Image, ImageDraw, ImageFont If your text is too long or font size too big it'll go outside the image. Hey everyone, after posting yesterday on r/LearnPython and getting some guidance on a module that could help me automate the creation of featured images that are basically a semi-transparent image + some text, I wanted to leave the final solution for anyone that could benefit from it.īefore moving on to the code, this is an example of the final output:ĮDIT: This does not work for multiline text. Introduction to Programming with Python (from Microsoft Virtual Academy)./r/git and /r/mercurial - don't forget to put your code in a repo!./r/pyladies (women developers who love python)./r/coolgithubprojects (filtered on Python projects)./r/pystats (python in statistical analysis and machine learning). /r/inventwithpython (for the books written by /u/AlSweigart)./r/pygame (a set of modules designed for writing games)./r/django (web framework for perfectionists with deadlines)./r/pythoncoding (strict moderation policy for 'programming only' articles).NumPy & SciPy (Scientific computing) & Pandas.Transcrypt (Hi res SVG using Python 3.6 and turtle module).Brython (Python 3 implementation for client-side web programming).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |