I have images like this, which I believe have 4 data channels (RGB + an alpha channel).
When I try to open this image like so, I can't access the transparency layer.
from PIL import Image
fn = 'FUgqUA5.png'
im = Image.open(fn)
im.mode # returns RGB instead of desired .png
im.getData()[0] # returns (0,0,0) instead of desired (0,0,0,255)
How do I open this image as an actual .png? Is it possible the image is saved as a 3-channel png instead and that this image simply doesn't have a 4th channel?
I saved the image and saw the properties via image editor and it did not mention that there is a 4th channel.
PS: (image editor is ImageMagick)
Related
I have this image and I read as a PIL file. Then, I save it back using save method in PIL and imwrite method in cv2. Saving the image with imwrite downgrades the image quality (it becomes black and white and text can't be read).
image = Image.open("image.png")
cv2_image = numpy.asarray(image)
image.save("pil.png")
cv2.imwrite("opencv.png", cv2_image)
Here are the output files:
pil.png
opencv.png
The input image is a palette image - see here. So, you need to convert it to RGB otherwise you just pass OpenCV the palette indices but without the palette.
So, you need:
image = Image.open(...).convert('RGB')
Now make it into a Numpy array:
cv2image = np.array(image)
But that will be in RGB order, so you need to reverse the channel order:
cv2image = cv2image[..., ::-1]
What is the way to convert RGB images to RGBA in python using OpenCV? My requirements are specific as I am trying to display an image on E-Paper Display. I need to convert an image to 16 bit RGBA. The code that I have written is converting the image but it's coming as all black. I have tried with convert 1 bit deep images to RGBA? NumPy convert 8-bit to 16/32-bit image these solutions as well. I have to use GMU image editor on Linux to convert it manually then it works. Since I am trying to open a pdf and convert each page as an image to display it on E-Paper Diaplay. I can't go that way.
i = cv2.imread(FNAME, cv2.IMREAD_UNCHANGED)
img = np.array(i, dtype=np.uint16)
img *= 256
filename = 'khrgb.png'
cv2.imwrite(filename, img)
use cv2.cvtColor
rgba=cv2.cvtColor(i,cv2.COLOR_BGR2RGBA)
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')
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.
I have looked at the below links to see how to convert PNG to JPG:
How to get alpha value of a PNG image with PIL?
PIL Convert PNG or GIF with Transparency to JPG without
The conversion works as expected, but when the image color itself is not black! I have the below image:
And the code is:
im.convert('RGB').save('test.jpg', 'JPEG')
It makes the whole picture black. How should I convert this PNG in correct format and color? The color can be anything from black to white.
Convert it like this, only thing to do is find out which backgroundcolor to set:
from PIL import Image
im = Image.open(r"C:\pathTo\pen.png")
fill_color = (120,8,220) # your new background color
im = im.convert("RGBA") # it had mode P after DL it from OP
if im.mode in ('RGBA', 'LA'):
background = Image.new(im.mode[:-1], im.size, fill_color)
background.paste(im, im.split()[-1]) # omit transparency
im = background
im.convert("RGB").save(r"C:\temp\other.jpg")