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")
Related
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
So basically I have a colored RGB image and I want to add a colored overlay over the RGB image without converting it to gray level.
For example if I have a colored image(RGB). And I want to add a transparent blue color over the index like this
img[200:350, 200:350] = [0, 0, 1] # Blue block
This question is a sibling question to this one:
Applying a coloured overlay to an image in either PIL or Imagemagik
Difference is the color space. The above question is for gray level images rather colored (RGB).
from skimage import io, data
import numpy as np
img = data.astronaut()
Please use the above code to answer.
Here is the code in OpenCV:
import cv2
# load the image
image = cv2.imread("2.jpg")
# I resized the images because they were to big
image = cv2.resize(image, (0,0), fx=0.75, fy=0.75)
overlay = image.copy()
output = image.copy()
#select the region that has to be overlaid
cv2.rectangle(overlay, (420, 205), (595, 385),(0, 255, 255), -1)
#Adding the transparency parameter
alpha = 1
#Performing image overlay
cv2.addWeighted(overlay, alpha, output, 1 - alpha,0, output)
#Save the overlaid image
cv2.imwrite('Output'+str(alpha) +'.jpg', output)
cv2.waitKey(0)
cv2.destroyAllWindows()
Some results:
when alpha = 0.1
when alpha = 0.5
when alpha = 0.8
when alpha = 1.0 (the overlay is no longer transparent but opaque)
Very easy code, I can read image from either image1 as png or image2 as jpg. Same image with different format.
Then filter out the darker part into black, show the brighter part into white.
#image = mpimg.imread('image1.png')
image = mpimg.imread('image2.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
thresh = (180, 255)
binary = np.zeros_like(gray)
binary[(gray > thresh[0]) & (gray <= thresh[1])] = 1
Somehow, when I plot the binary of image1, it is all black, but image2 looks what I tend to do.
The problem is most likely due to matplotlib.image successfully reading the png while the jpg falls back to using Pillow. The image resulting from the png read will be an array of floating point values with a range of 0.0 to 1.0 while the jpg read will be an array of bytes with values 0..255. As a result your clip operation will result in an all black image as everything is below 1.
See http://matplotlib.org/users/image_tutorial.html for more information.
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.
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