I've seen many questions and answers here, but they're not for Python. I'm trying to make a meme generator, and I have a folder full of emme templates. I want to make the user get a meme template, view it then add text. Here is my code so far:
from PIL import Image
im = Image.open("Two-Buttons.jpg")
im.show()
Yeah, I know it's not much but I need help with getting user input on a certain part of the text.
First, you would need to import the necessary objects to draw on images from the PIL library:
from PIL import Image, ImageDraw, ImageFont
Then, you instantiate the ImageDraw class:
draw = ImageDraw.Draw(im)
Then you can simply do the following to draw on the original image im:
text = 'Insert meme here'
font_type = ImageFont.truetype("arial.ttf", 18) # 18 -> font size
draw.text((100, 100), text, (255, 255, 0), font=font_type) # "draw.text(LOCATION, TEXT, COLOR, FONT_TYPE)"
im.show()
Related
I am a beginner in Pillow. Basically, I am doing some basic image manipulation task but when I add text to an Image, the text resolution gets break.
Below is my code and image attached.
from PIL import Image, ImageDraw, ImageFont
text = 'Any fool can write code that a computer can understand. \nGood programmers write code that humans can understand.'
font = ImageFont.truetype('Poppins-Bold.ttf', 15 )
im = Image.open("quotes-background.png")
d1 = ImageDraw.Draw(im)
d1.text((60 , 200), text.title(), fill='yellow', font=font)
im.save('quotes.png')
Image
I am trying to add some text on my image using PIL, see the code below,
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import sys
image = Image.open('image.png')
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('arial',40)
draw.text((700, 470),'Text',(0,0,0),font=font)
img.save('out-image.png','PNG')
But I lost the original colors of the image, see below images,
Original Image
After adding text
How I can preserve the original colors.
Thank You
That looks like a bug in PIL to me. I think it is because your image is palettised and the draw.text() is messing up the palette.
For a work-around, you can convert to an RGB image when you open it to avoid palette issues. Change to this:
image = Image.open('image.png').convert('RGB')
I have a bunch of images I need to put a text-overlay on top of. I created the overlay with GIMP (PNG with transparency) and tried pasting it on top of the other image:
from PIL import Image
background = Image.open("hahn_echo_1.png")
foreground = Image.open("overlay_step_3.png")
background.paste(foreground, (0, 0), foreground)
background.save("abc.png")
However, instead of displaying a nice black text on top, I get this:
overlay.png looks like this in Gimp:
So I would expect some nice and black text instead of this colorful mess.
Any ideas? Some PIL option I am missing?
As vrs pointed out above, using alpha_composite like this answer: How to merge a transparent png image with another image using PIL
does the trick. Make sure to have the images in the correct mode (RGBA).
Complete solution:
from PIL import Image
background = Image.open("hahn_echo_1.png").convert("RGBA")
foreground = Image.open("overlay_step_3.png").convert("RGBA")
print(background.mode)
print(foreground.mode)
Image.alpha_composite(background, foreground).save("abc.png")
Result:
I'm trying to place a PNG watermark with partial transparency on top of a Facebook profile pic (jpg) using the Python Image Library. The part that should be transparent simply comes off as white. Here's my code:
con = urllib2.urlopen('facebook_link_to_profile_pic')
im = Image.open(cStringIO.StringIO(con.read()))
overlayCon = urllib2.urlopen('link_to_overlay')
overlay = Image.open(cStringIO.StringIO(overlayCon.read()))
im.paste(overlay, (0, 0))
im.save('name', 'jpeg', quality=100)
I've tried a few different ways, but haven't gotten anything to work. Any help is appreciated.
The 3rd option to paste is a mask (see the docs). It accepts an RGBA image, so the simplest solution is to use your overlay image again: im.paste(overlay, (0, 0), overlay).
I need to protect my photos by filling them with copyright logos. My OS is Ubuntu 10.10 and Python 2.6. I intend to use PIL.
Supposed I have a copyright logo like this (You can do this in Photoshop easily):
and a picture like this:
I want to use PIL to get copyrighted pictures like the following (Fill the original pic with pattern):
and Final Result by altering the opacity of logos:
Is there any function in PIL that can do this? Any hint?
Thanks a lot!
PIL is certainly capable of this. First you'll want to create an image that contains the repeated text. It should be, oh, maybe twice the size of the image you want to watermark (since you'll need to rotate it and then crop it). You can use Image.new() to create such an image, then ImageDraw.Draw.text() in a loop to repeatedly plaster your text onto it, and the image's rotate() method to rotate it 15 degrees or so. Then crop it to the size of the original image using the crop() method of the image.
To combine it first you'll want to use ImageChops.multiply() to superimpose the watermark onto a copy of the original image (which will have it at 100% opacity) then ImageChops.blend() to blend the watermarked copy with the original image at the desired opacity.
That should give you enough information to get going -- if you run into a roadblock, post code showing what you've got so far, and ask a specific question about what you're having difficulty with.
Just a sampleļ¼
#!/usr/bin/python
# -*- coding: utf-8 -*-
from PIL import Image
def fill_watermark():
im = Image.open('a.jpg').convert('RGBA')
bg = Image.new(mode='RGBA', size=im.size, color=(255, 255, 255, 0))
logo = Image.open('logo.png') # .rotate(45) if you want, bg transparent
bw, bh = im.size
iw, ih = logo.size
for j in range(bh // ih):
for i in range(bw // iw):
bg.paste(logo, (i * iw, j * ih))
im.alpha_composite(bg)
im.show()
fill_watermark()