Writing Strings as a Bitmap image - python

I have a couple of strings that I want to save as a BitMap Image of resolution 264*176 for displaying on an E-ink screen(because apparently eInk displays can't show text horizontally).
Please note that the blue background is only to show the dimensions of the image. The actual background will be white.
I've tried PIL without success. Can someone suggest any approach in Python3?

Here is a PIL example
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
img = Image.new('RGB', (264, 176), color = (255, 255, 255))
font_path = '/usr/share/fonts/truetype/ubuntu/UbuntuMono-R.ttf'
font = ImageFont.truetype(font_path, 20)
draw = ImageDraw.Draw(img)
draw.text((15, 15), 'Is it your text?', font=font, fill=(0, 0, 0))
img.save('img_with_text.bmp', 'bmp')
Result

Related

How to make transparent pixels on an image blue with PIL [duplicate]

I need to replace the transparency layer of a png image with a color white. I tried this
from PIL import Image
image = Image.open('test.png')
new_image = image.convert('RGB', colors=255)
new_image.save('test.jpg', quality=75)
but the transparency layer turned black. Anyone can help me?
Paste the image on a completely white rgba background, then convert it to jpeg.
from PIL import Image
image = Image.open('test.png')
new_image = Image.new("RGBA", image.size, "WHITE") # Create a white rgba background
new_image.paste(image, (0, 0), image) # Paste the image on the background. Go to the links given below for details.
new_image.convert('RGB').save('test.jpg', "JPEG") # Save as JPEG
Take a look at this and this.
The other answers gave me a Bad transparency mask error. The solution is to make sure the original image is in RGBA mode.
image = Image.open("test.png").convert("RGBA")
new_image = Image.new("RGBA", image.size, "WHITE")
new_image.paste(image, mask=image)
new_image.convert("RGB").save("test.jpg")

Transparent background looking weird after Dithering - Pillow

we have a Python script:
from PIL import Image, ImageOps, ImageFilter
import cv2
import numpy as np
def remove_trans(image):
#Replace transparency on image with white background
image = image.convert('RGBA')
new_image = Image.new("RGBA", image.size, "WHITE") # Create a white rgba background
new_image.paste(image, (0, 0), image) # Paste the image on the background.
new_image = new_image.convert('RGB')
return new_image
img = Image.open('test.png')
img1 = remove_trans(img)
img1.save('NO_ALPHA_PNG.png')
print(img.mode)
img = img.convert('LA')
img = img.convert('1')
img.save('PNG_filter.png')
However we have a bug there. When we load a transparent png image, and then "process" the image with the script, the transparent background is very weird. We found out the reason is the dithering. but we havent found any solution so far. Anybody knows how to get the background transparent, with dithering also?
After process
Original

PIL - Unable to change the transparency level for JPEG image

I'm trying to create an image and fill it with a semi-transparent black colour:
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ImageEnhance
fnt = create_font()
# my background rectangle
img1 = Image.new("RGBA", 100, 100, color=(0, 0, 0, 230)) #RGBA
dr1 = ImageDraw.Draw(img1)
dr1.text((5, 5), "some text", font=fnt)
# my source image
my_img.paste(dr1, (10, 10))
my_img.save(out_file, "JPEG")
But it ignores the "230" being the transparency level. If I change it to "0" or any other number, the transparency level of "dr1" rectangle
stays the same -- it's completely black.
update:
I have a source in jpeg my_img. I want to put a semi-transparent rectangle on its part img1 with a text. How can I do that? How can I get img1 more transparent?
Firstly, JPEG doesn't support transparency, so if you want an image file with transparency you'll need to use a different format, eg PNG.
I don't know where that create_font function is defined; there isn't a function of that name in my PIL ImageFont (I'm using PIL.PILLOW_VERSION == '3.3.0' on Python 3.6 on 32 bit Linux).
Also, that paste operation won't work, but you don't need it.
Here's a modified version of your code.
from PIL import Image, ImageFont, ImageDraw
img1 = Image.new("RGBA", (100, 100), color=(0, 0, 0, 64))
dr1 = ImageDraw.Draw(img1)
fnt = ImageFont.load_default()
dr1.text((5, 5), "some text", font=fnt, fill=(255, 255, 0, 128))
#img1.show()
img1.save('test.png')
And here's the PNG file it creates:
Here's some code for your updated question.
from PIL import Image, ImageFont, ImageDraw
img1 = Image.open('hueblock.jpg').convert("RGBA")
overlay = Image.new("RGBA", (100, 100), color=(0, 0, 0, 63))
dr1 = ImageDraw.Draw(overlay)
fnt = ImageFont.load_default()
dr1.text((5, 5), "some text", font=fnt, fill=(255, 255, 255, 160))
img1.paste(overlay, (64, 64), overlay)
img1.show()
img1.save('test.jpg')
Here are hueblock.jpg and test.jpg
Note the arguments to the paste call:
img1.paste(overlay, (64, 64), overlay)
The final argument is an image mask. By supplying an RGBA image as the mask arg its alpha channel is used as the mask, as mentioned in the Pillow docs
[...] If a mask is given, this method updates only the regions
indicated by the mask. You can use either “1”, “L” or “RGBA” images
(in the latter case, the alpha band is used as mask). Where the mask
is 255, the given image is copied as is. Where the mask is 0, the
current value is preserved. Intermediate values will mix the two
images together, including their alpha channels if they have them.
You are saving the image file as a JPEG.
JPEGs do not support transparency. In order for your image to have transparency, you must save as a format that supports transparency in the image, such as a PNG.

How to paste a PNG image with transparency to another image in PIL without white pixels?

I have two images, a background and a PNG image with transparent pixels. I am trying to paste the PNG onto the background using Python-PIL but when I paste the two images I get white pixels around the PNG image where there were transparent pixels.
My code:
import os
from PIL import Image, ImageDraw, ImageFont
filename='pikachu.png'
ironman = Image.open(filename, 'r')
filename1='bg.png'
bg = Image.open(filename1, 'r')
text_img = Image.new('RGBA', (600,320), (0, 0, 0, 0))
text_img.paste(bg, (0,0))
text_img.paste(ironman, (0,0))
text_img.save("ball.png", format="png")
My images:
my output image:
How can I have transparent pixels instead of white?
You need to specify the image as the mask as follows in the paste function:
import os
from PIL import Image
filename = 'pikachu.png'
ironman = Image.open(filename, 'r')
filename1 = 'bg.png'
bg = Image.open(filename1, 'r')
text_img = Image.new('RGBA', (600,320), (0, 0, 0, 0))
text_img.paste(bg, (0,0))
text_img.paste(ironman, (0,0), mask=ironman)
text_img.save("ball.png", format="png")
Giving you:
To centre both the background image and the transparent image on the new text_img, you need to calculate the correct offsets based on the images dimensions:
text_img.paste(bg, ((text_img.width - bg.width) // 2, (text_img.height - bg.height) // 2))
text_img.paste(ironman, ((text_img.width - ironman.width) // 2, (text_img.height - ironman.height) // 2), mask=ironman)

Convert image color space to RGB in Python

I'm working on the image processing script(Python with PIL library) and i need to convert color space of any image to RGB. I've tried this trick, but it works only with png images in RGBa color space:
image = Image.open(imageFile)
image.load()
# replace alpha channel with white color
self.im = Image.new('RGB', image.size, (255, 255, 255))
self.im.paste(image, mask=image.split()[3])
How to make this code universally to all images in any colorspace?
Thanks.
Found solution:
image = Image.open(imageFile)
image.load()
# replace alpha channel with white color
self.im = Image.new('RGB', image.size, (255, 255, 255))
self.im.paste(image, None)
in the self.im variable will be stored an image in RGB colorspace with white(255, 255, 255) alpha channel.
Do you only want to use PIL? I would suggest the openCV version 2 python bindings cv2
http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#cv2.cvtColor
It has many possible conversions between the most common colourspaces.
If you dont want openCV you can use skimage
http://scikit-image.org/docs/dev/api/skimage.color.html#convert-colorspace

Categories