Can I use Pillow to find connected component? - python

I am new to Pillow and I would like to learn how to use it.
I would like to seek your helps and expertise to suggest if I could use Pillow to find the connected component of an image?
For example, if I have an image such as the following
May I ask if I could use Pillow to give me the shapes and positions of my two components in my example ? They are a square and a circle, and the circle is inside of the square.
Thank you very much,
Mi

Using Pillow you can find the edges in the given image.
from PIL import Image
from PIL import ImageFilter
image = Image.open("c1LDc.png")
image = image.convert('RGB')
imageWithEdges = image.filter(ImageFilter.FIND_EDGES)
image.show()
imageWithEdges.show()
Output:
You can't use Pillow for object detection, as answered here

Related

Python save and read a grayscale image in two dimension array?

I want to save an image without any channel, so the dimension would only be 2. Thus, is there a way I do it in matplotlib?
I have already tried using
matplotlib.pyplot.imsave('img.png', image, cmap='gray')
but when I read it using
matplotlib.pyplot.imread('img.png')
The dimension is 3. So I'm confusing how. I know maybe I can't use imread but what can I do instead?
If you have opencv installed, you can try:
cv2.imread('1.png', cv2.IMREAD_GRAYSCALE)
Also, you can also try PIL.
from PIL import Image
Image.fromarray(array)
I didn't see this one on the internet, but this one works! thanks to my teacher!
skimage.io.imsave('1.png', np.around(image*255).astype(np.uint8))
To use this, you have to have skimage preinstalled.
pip3 install scikit-image
Thanks #Cris Luengo in the comment above to point out that
"From the docs, it looks like matplotlib only saves RGB or RGBA format
image files. You’ll have to use a different package to save a
gray-scale image. OpenCV as suggested below is just one option. There
are many. Try PIL."
Give him an upvote when you saw it!

Python PIL to extract number from image

I have an image like this one:
and I would like to have a black number written on white so that I can use an OCR to recognise it. How could I achieve that in Python?
Many thanks,
John.
You don't need to manipulate the image for OCR. For example, you could just use pytesser:
from PIL import Image
from pytesser import *
im = Image.open('wjNL6.jpg')
text = image_to_string(im)
print text
Output:
0
If you just want to turn a white-on-black image to black-on-white, that's trivial; it's just invert:
from PIL import Image, ImageOps
img = Image.open('zero.jpg')
inverted = ImageOps.invert(img)
inverted.save('invzero.png')
If you also want to do some basic processing like increasing the contrast, see the other functions in the ImageOps module, like autocontrast. They're all pretty easy to use, but if you get stuck, you can always ask a new question. For more complex enhancements, look around the rest of PIL. ImageEnhance can be used to sharpen an image, ImageFilter can do edge detection and unsharp masking; etc. You may also want to change the format to greyscale (L8), or even black and white (L1); that's all in the Image.convert method.
Of course you have to know what processing you want to do. One thing you might want to try is playing around with the image in Photoshop or GIMP and keeping track of what operations you do, then looking for how to implement those operations in PIL. (It might be simpler to just use gimp-fu scripting in the first place instead of trying to use PIL…)

Watershed in OpenCV

I want to perform an oversegmentation of image using watershed method. Reading documentation, I'd need to use findContour and drawContour function to create marker. How can I use that?
This is my current code,
import cv2
import numpy as np
im=cv2.imread('balls.jpg')
marker=np.zeros(im.shape[:2])
marker[::30,::30]=200
marker=np.int32(marker)
cv2.watershed(im,marker)
out=cv2.convertScaleAbs(marker)
cv2.namedWindow('Out')
cv2.imshow('Out', out)
cv2.waitKey()
P/S: There's another question on this, but they used other approach(based on foreground and background. I want to use contours instead).
This is my goal: produce an oversegmetnation of image:
Input image can be downloaded from here:
http://decsai.ugr.es/~javier/denoise/peppers256.png

Moving crops within the same image

Tried looking in the PIL module but couldn't find this covered.
I have an image, and I need to take a cropped piece of the image and move it to a different area within the same image. Simple enough, but I will need to do this for thousands of images, so I will do batch image processing.
PIL seems too basic for this, any other libraries?
Actually, PIL looks like it can do it very well, if you want to copy and paste rectangular areas.
Check the documentation on the crop and paste methods on image, on the documentation here:
http://effbot.org/imagingbook/image.htm
If you need non-rectangular areas, though, you will have to resort to more sophisticated handling, but that does not preclude you from doing it with PIL.
Yes, check OpenCV.
How to crop images with opencv in python
The Python wrapper for the GD library should do what you need:
copyTo(image[, (dx,dy)[, (sx,sy)[, (w,h)]]])
copy from (sx,sy), width sw and height sh to destination image (dx,dy)

How do I find images with a similar color using Python and PIL?

I have a lot of images in a folder, and I would like to find images with a similar color to a pre chosen image.
I would like to be able to do something like:
python find_similar.py sample.jpg
and have that return something like:
234324.jpg
55.jpg
9945.jpg
345434.jpg
104.jpg
Is this doable?
I cannot give you a canned solution, but here's an angle to tackle the problem. It's not PIL-specific, and it might be entirely bogus, since I have no experience in image processing.
Perform color quantization on the image. That gives you a palette that encodes the color information in the image without any shape information.
Run a principal components analysis to get the dominant components in the color cube. Strictly, you could run this without quantization first, but it might be too expensive.
Do a least-squares fitting on the principal components of different images.
Hope this helps.
The algorithm for finding similar images is discussed in a Question on Stackoverflow, you might want to implement one of those in Python & PIL.
Also, you can straightaway use the ImageChops module from PIL and use the difference method to compare two images like this:
import Image
import ImageChops
im1 = Image.open("original.jpg")
im2 = Image.open("sample.jpg")
diff = ImageChops.difference(im2, im1)
That might help you in getting some idea about the difference in your original image and the others.
There is another similar question on Stackoverflow which discusses this.

Categories