how to embed a text image in a background image in python? - python

I am trying to embed a text in a background image using python. The background image is 1.jpg and the text image is 2.jpg. The background image contains a wall on which I want to embed the text region 2.jpg which contains scene text "Poppins". Can anybody help me out with the code?
The sample input is given
The sample output is somewhat like this:
Though I have doe it with paint!! Originally the sample output should look like this where natural scene text is embedded in the background of the image
Sample Text Image is:
Which is an extracted scene text

If you want to paste one image on top of another, your text file should be a .png file (so that the overlay file contains no background data), then you can use the following code, importing the PIL library:
from PIL import Image
image1 = Image.open('img1.jpg')
image2 = Image.open('img2.png')
image1.paste(image2, (0,0))
image1.show()
Change the (0,0) values to move the png text image around on x,y co-ordinates.

Related

Change active transparent .png background color in python

I am trying to automate this Gimp Feature, which can change the active background color in a transparent .png file from white to black. The background is somehow saved with the picture but still stays transparent. I could not reproduce this feature anywhere else in another program, but need that for a project.
I tried it like this (runs through all images in my input folder and puts it afterwards as a new file in the output folder, image size is 9150*10980 px).
import random
import PIL
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import glob
def background_apply(input_image_path,
output_image_path):
photo = Image.open(input_image_path).convert('RGBA')
img2 = Image.new('RGBA', (9150, 10980))
print (photo.mode)
img2.paste(photo, mask=photo)
img2.save(output_image_path)
list = glob.glob(r"C:\Users\XX\input\*.*")
for photo in list:
out = photo.replace('input','output')
background_apply(photo,
out)
This saves my image with a transparent background, but not with the desired "black transparent". I hope I made the problem clear, it is not that I want to get a real black background on the image, it should still stay transparent.
Any help would be appreciated. Thanks in advance.

Advanced cropping with Python Imaging Library

I have an A4 png image with some text in it, it's transparent, my question is, how can I crop the image to only have the text, I am aware of cropping in PIL, but if I set it to fixed values, it will not be able to crop another image that has that text in another place. So, how can I do it so it finds where the text, sticker, or any other thing is placed on that big and empty image, and crop it so the thing fits perfectly?
Thanks in advance!
You can do this by extracting the alpha channel and cropping to that. So, if this is your input image:
Here it is again, smaller and on a chessboard background so you can see its full extent:
The code looks like this:
#!/usr/bin/env python3
from PIL import Image
# Load image
im = Image.open('image.png')
# Extract alpha channel as new Image and get its bounding box
alpha = im.getchannel('A')
bbox = alpha.getbbox()
# Apply bounding box to original image
res = im.crop(bbox)
res.save('result.png')
Here is the result:
And again on a chessboard pattern so you can see its full extent:
Keywords: Image processing, Python, PIL/Pillow, trim to alpha, crop to alpha, trim to transparency, crop to transparency.
from PIL import Image
im = Image.open("image.png")
im.getbbox()
im2 = im.crop(im.getbbox())
im2.save("result.png")

add a black space for an image in Python pillow

I wish to expand an image, so I can write something at the black expanded space under the original image, but it doesn't work.
I can't expand a black space and add it to the image, neither can write at a specific place
I'm new to the Pillow library, can anyone help?
You could do something like this:
read the image
create a new image (black by default) with the desired size
get data of the input image and put it down on the new one
from PIL import Image
HEIGH_OF_THE_BLACK_AREA = 100
with Image.open('image.jpg') as im:
new_im = Image.new(im.mode, size = (im.size[0], im.size[1] + HEIGH_OF_THE_BLACK_AREA))
new_im.putdata(im.getdata())
new_im.save('out.jpg')

Convert non-transparent image to transparent GIF image PIL

How can I convert a non-transparent PNG file into a transparent GIF file with PIL?
I need it for my turtle-graphics game. I can only seem to transparentize a PNG file, not a GIF file.
It's not obvious, to me at least, how you are supposed to do that! This may be an unnecessary work-around for a problem that doesn't exist because I don't know something about how PIL works internally.
Anyway, I messed around with it long enough using this input image:
#!/usr/bin/env python3
from PIL import Image, ImageDraw, ImageOps
# Open PNG image and ensure no alpha channel
im = Image.open('start.png').convert('RGB')
# Draw alpha layer - black square with white circle
alpha = Image.new('L', (100,100), 0)
ImageDraw.Draw(alpha).ellipse((10,10,90,90), fill=255)
# Add our lovely new alpha layer to image
im.putalpha(alpha)
# Save result as PNG and GIF
im.save('result.png')
im.save('unhappy.gif')
When I get to here, the PNG works and the GIF is "unhappy".
PNG below:
"Unhappy" GIF below:
Here is how I fixed up the GIF:
# Extract the alpha channel
alpha = im.split()[3]
# Palettize original image leaving last colour free for transparency index
im = im.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255)
# Put 255 everywhere in image where we want transparency
im.paste(255, ImageOps.invert(alpha))
im.save('result.gif', transparency=255)
Keywords: Python, image processing, PIL, Pillow, GIF, transparency, alpha, preserve, transparent index.

coloring a jpg image using PIL

I have written a python code using PIL and Tkinter where I display an image and put a red circle on the image where a user clicked. Here is the relevant code.
def paint_img(event, canvas):
x, y = event.x, event.y
image_draw.ellipse((x-10, y-10, x+10, y+10), fill='red')
canvas._image_tk = ImageTk.PhotoImage(pilImg)
canvas.itemconfigure(canvas._image_id, image=canvas._image_tk)
It seems that it works with some images, but in some cases it displays a grey circle. I used identify on the test images, for the successful one the output is
totestcolor.jpg JPEG 561x549 561x549+0+0 8-bit DirectClass 18.3kb
for the unsuccessful one, the output is:
totestcolor1.jpg JPEG 1533x1095 1533x1095+0+0 8-bit PseudoClass 256c 70.4kb
I want to know why this is happening and if there is any way to output a red circle for the unsuccessful image as well.
That's because the original image is grayscale. You can convert it to full RGB before drawing the red circle. In the PIL library, it is code like this:
pilImg = pilImg.convert("RGB") # or "RGBA" to keep transparency

Categories