Pillow - Adding transparency depending on grayscale values - python-imaging-library

Based on this post: Converting image grayscale pixel values to alpha values , how could I change an image transparency based on grayscale values with Pillow (6.2.2)?
I would like the brighter a pixel, the more transparent it is. Thus, pixels that are black or close to black would not be transparent.
I found the following script that works fine for white pixels but I don't know how to modify it on order to manage grayscale values. Maybe there is a better or faster way, I'm a real newbie in Python.
from PIL import Image
img = Image.open('Image.jpg')
img_out = img.convert("RGBA")
datas = img.getdata()
target_color = (255, 255, 255)
newData = list()
for item in datas:
newData.append((
item[0], item[1], item[2],
max(
abs(item[0] - target_color[0]),
abs(item[1] - target_color[1]),
abs(item[2] - target_color[2]),
)
))
img_out.putdata(newData)
img_out.save('ConvertedImage', 'PNG')

This is what I finally did:
from PIL import Image, ImageOps
img = Image.open('Image.jpg')
img = img.convert('RGBA') # RGBA = RGB + alpha
mask = ImageOps.invert(img.convert('L')) # 8-bit grey
img.putalpha(mask)
img.save('ConvertedImage', 'PNG')

Related

Discord.py Image Editing with Python Imaging Library only works for some pictures?

I've tried an image-editing-effect which should recolor a picture with little black dots, however it only works for certain images and I honestly don't know why. Any ideas?
#url = member.avatar_url
#print(url)
#response = requests.get(url=url, stream=True).raw
#imag = Image.open(response)
imag = Image.open("unknown.png")
#out = Image.new('I', imag.size)
i = 0
width, height = imag.size
for x in range(width):
i+=1
for y in range(height):
if i ==5:
# changes every 5th pixel to a certain brightness value
r,g,b,a = imag.getpixel((x,y))
print(imag.getpixel((x,y)))
brightness = int(sum([r,g,b])/3)
print(brightness)
imag.putpixel((x, y), (brightness,brightness,brightness,255))
i= 0
else:
i += 1
imag.putpixel((x,y),(255,255,255,255))
imag.save("test.png")
The comments are what I would've used if my tests had worked. Using local pngs also don't work all the time.
Your image that doesn't work doesn't have an alpha channel but your code assumes it does. Try forcing in an alpha channel on opening like this:
imag = Image.open("unknown.png").convert('RGBA')
See also What's the difference between a "P" and "L" mode image in PIL?
A couple of other ideas too:
looping over images with Python for loops is slow and inefficient - in general, try to find a vectorised Numpy alternative
you have an alpha channel but set it to 255 (i.e. opaque) everywhere, so in reality, you may as well not have it and save roughly 1/4 of the file size
your output image is RGB with all 3 components set identically - that is really a greyscale image, so you could create it as such and your output file will be 1/3 the size
So, here is an alternative rendition:
#!/usr/bin/env python3
from PIL import Image
import numpy as np
# Load image and ensure neither palette nor alpha
im = Image.open('paddington.png').convert('RGB')
# Make into Numpy array
na = np.array(im)
# Calculate greyscale image as mean of R, G and B channels
grey = np.mean(na, axis=-1).astype(np.uint8)
# Make white output image
out = np.full(grey.shape, 255, dtype=np.uint8)
# Copy across selected pixels
out[1::6, 1::4] = grey[1::6, 1::4]
out[3::6, 0::4] = grey[3::6, 0::4]
out[5::6, 2::4] = grey[5::6, 2::4]
# Revert to PIL Image
Image.fromarray(out).save('result.png')
That transforms this:
into this:
If you accept calculating the greyscale with the normal method, rather than averaging R, G and B, you could change to this:
im = Image.open('paddington.png').convert('L')
and remove the line that does the averaging:
grey = np.mean(na, axis=-1).astype(np.uint8)

How to apply binary mask to remove background from skin lesion colour image

the figure outputted just displays the binary mask image, however I am trying to get just the foreground of the coloured image, with the background being black.
original = imread('originalImage.jpg');
binaryImage = imread('binaryImage.png');
mask = cat(3,binaryImage, binaryImage, binaryImage);
output = mask.*original;
figure,imshow(output);
the binary mask
The original image
The most likely issue is that binary is an image with values of 0 for background and 255 for foreground. Multiplying a color image with values in the range [0,255] by such a mask leads to overflow. Since the input images are uint8, overflow leads to values of 255. Thus, everywhere where the mask is white, you get white colors.
The solution is to convert the images to double:
output = double(mask)/255 .* double(original)/255;
or to truly binarize the mask image:
output = (mask>0) .* original;

Improving Low Contrast Image Segmentation

I have phase-contrast microscopy images that needs to be segmented. It seems very difficult to segment them due to the lack of contrast between the objects from the background (image 1). I used the function adapthisteq to increase the visibility of the cells (image 2). Is there any way I can improve the segmentation of the cells?
normalImage = imread(fileName);
channlImage = rgb2gray(normalImage);
histogramEq = adapthisteq(channlImage,'NumTiles',[50 50],'ClipLimit',0.1);
saturateInt = imadjust(histogramEq);
binaryImage = im2bw(saturateInt,graythresh(saturateInt));
binaryImage = 1 - binaryImage;
normalImage - raw image
histogramEq - increased visibility image
binaryImage - binarized image
Before to apply the threshold, I would separate the different patterns from the background by using a white top-hat. See here the result. Then you stretch the histogram.
Then you can apply what you did.
I would like to build up on FiReTiTi's answer. I have the code below and some screenshots. I have done this using OpenCV 3.0.0
import cv2
x = 'test.jpg'
img = cv2.imread(x, 1)
cv2.imshow("img",img)
#----converting the image to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', gray)
#----binarization of image
ret,thresh = cv2.threshold(gray,250,255,cv2.THRESH_BINARY)
cv2.imshow("thresh",thresh)
#----performing adaptive thresholding
athresh=cv2.adaptiveThreshold(thresh, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
cv2.imshow('athresh', athresh)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7, 7))
#----morphological operation
closing = cv2.morphologyEx(athresh, cv2.MORPH_CLOSE, kernel)
cv2.imshow('closing', closing)
#----masking the obtained result on the grayscale image
result = cv2.bitwise_and(gray, gray, mask= closing)
cv2.imshow('result ', result )

Matlab - remove image border using logical indexing

I have a retinal fundus image which has a white border along the corners. I am trying to remove the borders on all four sides of the image. This is a pre-processing step and my image looks like this:
fundus http://snag.gy/XLGkC.jpg
It is an RGB image, and I took the green channel, and created a mask using logical indexing. I searched for pixels which were all black in the image, and eroded the mask to remove the white edge pixels. However, I am not sure how to retrieve the final image, without the white pixel border using the mask that I have. This is my code, and any help would be appreciated:
maskIdx = rgb(:,:,2) == 0; # rgb is the original image
se = strel('disk',3); # erode 3-pixel using a disk structuring element
im2 = imerode(maskIdx, se);
newrgb = rgb(im2); # gives a vector - not the same size as original im
Solved it myself. This is what I did with some help.
I first computed the mask for all three color channels combined. This is because the mask for each channel is not the same when applied to all the three channels individually, and residual pixels will be left in the final image if I used only the mask from one of the channels in the original image:
mask = (rgb(:,:,1) == 0) & (rgb(:,:,2) == 0) & (rgb(:,:,3) == 0);
Next, I used a disk structuring element with a radius of 9 pixels to dilate my mask:
se = strel('disk', 9);
maskIdx = imdilate(mask,se);
EDIT: A structuring element which is arbitrary can also be used. I used: se = strel(ones(9,9))
Then, with the new mask, I multiplied the original image with the new dilated mask:
newImg(:,:,1) = rgb(:,:,1) .* uint8(maskIdx); # image was of double data-type
newImg(:,:,2) = rgb(:,:,2) .* uint8(maskIdx);
newImg(:,:,3) = rgb(:,:,3) .* uint8(maskIdx);
Finally, I subtracted the computed color-mask from the original image to get my desired border-removed image:
finalImg = rgb - newImg;
Result:
image http://snag.gy/g2X1v.jpg

Fill Color of PIL Cropping/Thumbnailing

I am taking an image file and thumbnailing and cropping it with the following PIL code:
image = Image.open(filename)
image.thumbnail(size, Image.ANTIALIAS)
image_size = image.size
thumb = image.crop( (0, 0, size[0], size[1]) )
offset_x = max( (size[0] - image_size[0]) / 2, 0 )
offset_y = max( (size[1] - image_size[1]) / 2, 0 )
thumb = ImageChops.offset(thumb, offset_x, offset_y)
thumb.convert('RGBA').save(filename, 'JPEG')
This works great, except when the image isn't the same aspect ratio, the difference is filled in with a black color (or maybe an alpha channel?). I'm ok with the filling, I'd just like to be able to select the fill color -- or better yet an alpha channel.
Output example:
How can I specify the fill color?
I altered the code just a bit to allow for you to specify your own background color, including transparency.
The code loads the image specified into a PIL.Image object, generates the thumbnail from the given size, and then pastes the image into another, full sized surface.
(Note that the tuple used for color can also be any RGBA value, I have just used white with an alpha/transparency of 0.)
# assuming 'import from PIL *' is preceding
thumbnail = Image.open(filename)
# generating the thumbnail from given size
thumbnail.thumbnail(size, Image.ANTIALIAS)
offset_x = max((size[0] - thumbnail.size[0]) / 2, 0)
offset_y = max((size[1] - thumbnail.size[1]) / 2, 0)
offset_tuple = (offset_x, offset_y) #pack x and y into a tuple
# create the image object to be the final product
final_thumb = Image.new(mode='RGBA',size=size,color=(255,255,255,0))
# paste the thumbnail into the full sized image
final_thumb.paste(thumbnail, offset_tuple)
# save (the PNG format will retain the alpha band unlike JPEG)
final_thumb.save(filename,'PNG')
Its a bit easier to paste your re-sized thumbnail image onto a new image, that is the colour (and alpha value) you want.
You can create an image, and speicfy its colour in a RGBA tuple like this:
Image.new('RGBA', size, (255,0,0,255))
Here there is there is no transparency as the alpha band is set to 255. But the background will be red. Using this image to paste onto we can create thumbnails with any colour like this:
If we set the alpha band to 0, we can paste onto a transparent image, and get this:
Example code:
import Image
image = Image.open('1_tree_small.jpg')
size=(50,50)
image.thumbnail(size, Image.ANTIALIAS)
# new = Image.new('RGBA', size, (255, 0, 0, 255)) #without alpha, red
new = Image.new('RGBA', size, (255, 255, 255, 0)) #with alpha
new.paste(image,((size[0] - image.size[0]) / 2, (size[1] - image.size[1]) / 2))
new.save('saved4.png')