Can't paste image due to ValueError: bad transparency mask - python

Creating a program to paste another image on top of another image. And when I paste a campaign logo that I made in Photoshop, And when I run the program, I get the error:
ValueError: bad transparency mask
I tried converting the image from RGBA to RGB, and that did not work, here is the code:
def test():
background = Image.open("photo.png")
logo = Image.open("66.png")
background_small = logo.resize(bg_size)
logo_small = logo.resize(logo_size)
background.paste(logo, (0, 600), logo)
background.show()
background.save('out.png')
Edit: I fixed the error using this stackoverflow post: Convert RGBA PNG to RGB with PIL

Try converting both to "RGBA" first.

Related

Converting PNG to JPEG creates pixel noise in the background

I have to convert some PNGs with transparent bg to simple JPEGs where the transparent background turns to white (which I assumed will happen by default). I have tried all of the solutions I could find here, but after saving the PNG as JPEG the image will look like this: (the noisy area was transparent on the PNG and the black was drop shadow)
Converted image
Original image
This is the code I use:
# save the PNG
response = requests.get(image_url, headers = header)
file = open("%s-%s.png" % (slug, item_id,), "wb")
file.write(response.content)
file.close()
# open the PNG and save as a JPEG
im1 = Image.open(filepath_to_png)
rgb_im = im1.convert('RGB')
rgb_im.mode
rgb_im.save(filepath_normal)
My question is that how could I prevent the JPEG to have that corrupted background? My goal is just simply have the same image in JPEG what I had in PNG.
The method you are using to convert to RGB would work on some images that just require straight-forward conversion like the ones with hard-edged transparency masks, but for those with soft-edged masking (like the transparency shadows in your image) it is not be effective as the conversion does not know how to deal with that semi-transparency.
A better approach to handle this would be to create a new Image with the same dimensions and fill it with a white background, then you just need to paste your original image:
new_im = Image.new( "RGB", im1.size, ( 255,255,255 ) )
new_im.paste( im1, im1 )
new_im.save( filepath_normal )
I have tested this approach using an image with soft-edged masking and obtained the following result:
You could use pillow library in python.
from PIL import Image
im = Image.open("1.png")
bg = Image.new("RGB", im.size, (255,255,255))
bg.paste(im,im)
bg.save("2.jpg")
Result I got had transparent background turned to white.

Pillow pasting transparent image shows black image

I'm trying to use Pillow to paste an image with a transparent background onto a background image.
The background is a simple color background, and the transparent background image is as follows:
The transparent image
The code I'm using is below (the path is deliberately incorrect):
backgnd = Image.open("images/background.png")
backgnd.convert("RGBA")
body = Image.open("images/Untitled.png")
body.convert("RGBA")
backgnd.paste(body, (0,0), body)
backgnd.show()
The resulting image is as follows:
The resulting image, being all black
I've tried using the alpha_composite method, but it returns ValueError: image has wrong mode.
Try: backgnd.paste(body, (0,0), mask=body)

Image.open() gives a plain white image

I am trying to edit this image:
However, when I run
im = Image.open(filename)
im.show()
it outputs a completely plain white image of the same size. Why is Image.open() not working? How can I fix this? Is there another library I can use to get non-255 pixel values (the correct pixel array)?
Thanks,
Vinny
Image.open actually seems to work fine, as does getpixel, putpixel and save, so you can still load, edit and save the image.
The problem seems to be that the temp file the image is saved in for show is just plain white, so the image viewer shows just a white image. Your original image is 16 bit grayscale, but the temp image is saved as an 8 bit grayscale.
My current theory is that there might actually be a bug in show where a 16 bit grayscale image is just "converted" to 8 bit grayscale by capping all pixel values to 255, resulting in an all-white temp image since all the pixels values in the original are above 30,000.
If you set a pixel to a value below 255 before calling show, that pixel shows correctly. Thus, assuming you want to enhance the contrast in the picture, you can open the picture, map the values to a range from 0 to 255 (e.g. using numpy), and then use show.
from PIL import Image
import numpy as np
arr = np.array(Image.open("Rt5Ov.png"))
arr = (arr - arr.min()) * 255 // (arr.max() - arr.min())
img = Image.fromarray(arr.astype("uint8"))
img.show()
But as said before, since save seems to work as it should, you could also keep the 16 bit grayscale depth and just save the edited image instead of using show.
you can use openCV library for loading images.
import cv2
img = cv2.imread('image file')
plt.show(img)

how i can show image after image writing

I'm trying simple code that read an image and convert it to grey scale then show both of them, and finally save the grey scale image and display it after saving. The problem is that cv2.imshow (image show) for saved image doesn't work.
The images before image writing are displayed correctly and the image saved correctly in the same path but can't be displayed using cv2.imshow.
'''
python
'''
import cv2
img=cv2.imread('cover.jpg')
cv2.imshow('image', img)
img_grey = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
cv2.imshow('image_grey', img_grey)
savedimage='new.jpg'
cv2.imwrite('new.jpg',img_grey)
cv2.imshow('testsavedimage',savedimage)
cv2.waitKey(0)
I receive error for showing saved image
File "C:/1.py", line 8, in <module>
cv2.imshow('testsavedimage',savedimage)
TypeError: Expected Ptr<cv::UMat> for argument '%s
savedimage is just a string in this case. If you want to make sure that your grey scale image was saved properly, you need to first read it back into a Mat object:
cv2.imwrite('new.jpg',img_grey)
savedImg = cv2.imread('new.jpg')
cv2.imshow('testsavedimage', savedImg)
Hope this helps.
(PS #Mark Setchel is right, you should be using cv2.COLOR_BGR2GRAY here. This is because imread() and imshow() default to BGR colorspace, not RGB)

Transparency with Python Image Library

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).

Categories

Resources