How to use Dithering using pillow library? - python

I want to use Dithering from pillow, (https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Dither.FLOYDSTEINBERG)
My question is how to import dither from pillow,
Following is my code,
import os
from PIL import Image
ORIGIN_PATH = "/home/prajakta/Tasks/Evaluate detection model on scanned customer documents/scanned_docs/"
#DESTIN_PATH = "/home/prajakta/Tasks/Evaluate detection model on scanned customer documents/scanned_docs_grayscale/"
for filename in os.listdir(ORIGIN_PATH):
img = Image.open(ORIGIN_PATH + filename).convert("L", dither=Image.Dither)
img.show()
Thank you in advance.

Related

How to copy all information from image to another image using pillow python?

How can I copy all information from image1 to image2. Image1 contains GPS information and other information. the question is how take all these information from image1 and put into image2.?
Thanks.
Hi you can check this out link
from PIL import Image
Image1 = Image.open('D:\cat.jpg')
Image1copy = Image1.copy()
Image2 = Image.open('D:\core.jpg')
Image2copy = Image2.copy()
Image1copy.paste(Image2copy, (0, 0))
Image1copy.save('D:\pasted2.png')
Thanks
import PIL
from PIL import Image
img1=Image.open('image1_path')
img2=Image.new(mode=img1.mode,size=img1.size)
img2.paste(img1)

Retrieve image from url using dlib

I want to retrieve an image from the url using the dlib library.
I have tried the following code:
win = dlib.image_window()
img = dlib.load_rgb_image(urllib.request.urlopen(url).read())
win.set_image(img)
But the window opens empty, when I expected the image from the url to open.
After a lot of trial and error, this seems to display the image for me:
import urllib.request
import dlib
import numpy
import io
from PIL import Image
win = dlib.image_window()
img = numpy.array(Image.open(io.BytesIO(urllib.request.urlopen(url).read())))
win.set_image(img)

cv2 equivalent attributes to cv

I have come across this link for face detection and image cropping. I would like to use this script but I have cv2 install and only import cv2 works but not import cv.
How can I convert the cv functions in the following function to cv2 functions?
def faces_from_pil_image(pil_image):
"Return a list of (x,y,h,w) tuples for faces detected in the PIL image"
storage = cv.CreateMemStorage(0)
facial_features = cv.Load('haarcascade_frontalface_alt.xml', storage=storage)
cv_im = cv.CreateImageHeader(pil_image.size, cv.IPL_DEPTH_8U, 3)
cv.SetData(cv_im, pil_image.tostring())
faces = cv.HaarDetectObjects(cv_im, facial_features, storage)
# faces includes a `neighbors` field that we aren't going to use here
return [f[0] for f in faces]
Either use
import cv2
storage = cv2.cv.CreateMemStorage(0)
or
from cv2 import *
storage = cv.CreateMemStorage(0)

opencv python face detection from url images

I have no problem getting the opencv face detection using haar feature based cascades working on saved images:
from PIL import Image
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
img = cv2.imread('pic.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
but I can't figure out how to open a url image and pass it into face_cascade. I've been playing around with cStringIO, but I don't know what to do with it...
import cv2.cv as cv
import urllib, cStringIO
img = 'http://scontent-b.cdninstagram.com/hphotos-prn/t51.2885-15/10424498_582114441904402_1105042543_n.png'
file = cStringIO.StringIO(urllib.urlopen(img).read())
source = Image.open(file).convert("RGB")
bitmap = cv.CreateImageHeader(source.size, cv.IPL_DEPTH_8U, 3)
cv.SetData(bitmap, source.tostring())
cv.CvtColor(bitmap, bitmap, cv.CV_RGB2BGR)
is it possible to work with a numpy array instead?
source2 = Image.open(file)
imarr=numpy.array(source2,dtype=numpy.uint8)
I'm a beginner, so I apologize for the poor explanation.
thanks a lot in advance!!
In your first example you are using OpenCV2.imread to read your image in the second you are presumably using PIL.Image then trying to convert.
Why not simply save the file to a temp directory and then use OpenCV2.imread again?
Or in another way you can use VideoCapture() class to open url image.
See the C++ code below,
VideoCapture cap;
if(!cap.open("http://docs.opencv.org/trunk/_downloads/opencv-logo.png")){
cout<<"Cannot open image"<<endl;
return -1;
}
Mat src;
cap>>src;
imshow("src",src);
waitKey();

python open cv loading image

I'm new to python and open cv. I'm trying to find out how to load an image in opencv with python. Can any one provide an example (with code) explaining how to load the image and display it?
import sys
import cv
from opencv.cv import *
from opencv.highgui import *
ll="/home/pavan/Desktop/iff pics/out0291.tif"
img= cvLoadImage( ll );
cvNamedWindow( “Example1”, CV_WINDOW_AUTOSIZE );
cvShowImage( “Example1”, img );
cvWaitKey(10);
cvDestroyWindow( “Example");
There have been quite a few changes in the openCV2 API:
import cv
ll = "/home/pavan/Desktop/iff pics/out0291.tif"
img = cv.LoadImage(ll)
cv.NamedWindow("Example", cv.CV_WINDOW_AUTOSIZE )
cv.ShowImage("Example", img )
cv.WaitKey(10000)
cv.DestroyWindow("Example")
It is a simpler, quite cleaner syntax!
Also, you don't need trailing ; à-la-matlab. Last, be careful about the quotes you use.
For the newer openCV3 API, you should see the other answer to this question.
import cv2
image_path = "/home/jay/Desktop/earth.jpg"
img = cv2.imread(image_path) # For Reading The Image
cv2.imshow('image', img) # For Showing The Image in a window with first parameter as it's title
cv2.waitKey(0) #waits for a key to be pressed on a window
cv2.destroyAllWindows() # destroys the window when the key is pressed
There are 2 possible approaches to this:
Using argparse (recommended):
import cv2
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,help = "Path to the image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
This will take the image as an argument, will then convert the argument, add it to ap and the load it using the imread function()
To run it.
Go to your required folder
source activate your environment
python filename.py -i img.jpg
Hardcoding the image location:
import cv2
img = cv2.imread("\File\Loca\img.jpg")
cv2.imshow("ImageName",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run this similarly, omitting the arguments.

Categories

Resources