I want to make a filter to change the clarity of the image, but no matter what I searched, I could not find any function or algorithm for this.
In your terminal, execute this command: pip install Pillow
from PIL import Image, ImageEnhance
# Read the image
img = Image.open("your-image-path-here.png")
# Image brightness enhancer
enhancer = ImageEnhance.Brightness(img)
factor = 1.3 # Change the value here to change the brightness
img_output = enhancer.enhance(factor)
img_output.save('my-modified-image.png') # You can name "my-modified-image" whatever you want.
Related
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')
I have been having trouble trying to save a png image with sRGB and an alpha channel. I first crop an image and then save it like this:
from PIL import Image
import cv2
inputPath = 'picture.png'
img = cv2.imread(inputPath)
crop_img = img[bounds[3]:bounds[2], bounds[1]:bounds[0]]
pth = name + ".png"
crop_img.save(pth)
This however creates a file like this:
I want the file to be like this though:
How can I get this result in python?
P.S. The original image does have an alpha channel and sRGB colour profile.
Any help is greatly appreciated!
You can read your image as follows. It will load your image as such including the alpha channel.
img = cv2.imread(inputPath,-1)
UPDATE
Following code is equivalent to the above given answer since cv2.IMREAD_UNCHANGED=-1 in documentation. Though above snippet solve the issue, it is not a good programming practice to use it since it doesn't give the idea about what is really -1 does. But following code snippet gives an explicit idea about the behavior of the code.
img = cv2.imread(input,cv2.IMREAD_UNCHANGED)
So working with windows, python 2.7 and simplecv I am making a live video with my webcam and want simplecv to give me a grayscale version of the video. Is there any simple way to achieve that?
I found the command
grayscale()
on the opencv page, which should do exactly that but when I run it I get the error:
NameError: name "grayscale" is not defined
I am currently using this prewritten code for object tracking but I don't know whether I should use the command I found, and where in the code I should put it, does anybody have an idea? :
print __doc__
import SimpleCV
display = SimpleCV.Display()
cam = SimpleCV.Camera()
normaldisplay = True
while display.isNotDone():
if display.mouseRight:
normaldisplay = not(normaldisplay)
print "Display Mode:", "Normal" if normaldisplay else "Segmented"
img = cam.getImage().flipHorizontal()
dist = img.colorDistance(SimpleCV.Color.BLACK).dilate(2)
segmented = dist.stretch(200,255)
blobs = segmented.findBlobs()
if blobs:
circles = blobs.filter([b.isCircle(0.2) for b in blobs])
if circles:
img.drawCircle((circles[-1].x, circles[-1].y), circles[-1].radius(),SimpleCV.Color.BLUE,3)
if normaldisplay:
img.show()
else:
segmented.show()
There are multiple ways to do this in SimpleCV.
One way has been already described, it's the toGray() method.
There's also a way you can do this with gaussian blur, which also helps to remove image noise:
from SimpleCV import *
img = Image("simplecv")
img.applyGaussianFilter(grayscale=True)
After the third line, img object contains the image with a lot less high-frequency noise, and converted to grayscale.
You may check out pyimagesearch.com who works with OpenCV, but he explains why applying Gaussian Blur is a good idea.
In simple cv theres a function called toGray() for example:
import SimpleCV as sv
img = img.jpg
sv.img.jpg.toGray()
return gimg.jpg
I want to convert an image so I can read it better using pyocr & tesseract.
The Command line I want to convert to python is :
convert pic.png -background white -flatten -resize 300% pic_2.png
Using python Wand I managed to resize it but I don't know how to do the flattend and the white background
My try :
from wand.image import Image
with Image(filename='pic.png') as image:
image.resize(270, 33) #Can I use 300% directly ?
image.save(filename='pic2.png')
Please help
Edit, Here is the image to make tests on :
For resize & background. Use the following, and note that you'll need to calculate the 300% yourself.
from wand.image import Image
from wand.color import Color
with Image(filename="pic.png") as img:
# -resize 300%
scaler = 3
img.resize(img.width * scaler, img.height * scaler)
# -background white
img.background_color = Color("white")
img.save(filename="pic2.png")
Unfortunately the c method MagickMergeImageLayers has yet to be implemented. You should author an enhancement request with the development team.
Update
If you want to remove the transparency, just disable the alpha channel
from wand.image import Image
with Image(filename="pic.png") as img:
# Remove alpha
img.alpha_channel = False
img.save(filename="pic2.png")
Another way
It might just be easier to create a new image with the same dimensions as the first, and just composite the source image over the new one.
from wand.image import Image
from wand.color import Color
with Image(filename="pic.png") as img:
with Image(width=img.width, height=img.height, background=Color("white")) as bg:
bg.composite(img,0,0)
# -resize 300%
scaler = 3
bg.resize(img.width * scaler, img.height * scaler)
bg.save(filename="pic2.png")
I'm trying to use the Zbar library's QR code detection methods on images I extract with OpenCV's camera methods. Normally the QR code detection methods work with images (jpg, png, etc.) on my computer, but I guess the captured frames of OpenCV are different.
Is there a way of making the captured frame into a PIL Image?
Thank you.
from PIL import Image
import zbar
import cv2.cv as cv
capture = cv.CaptureFromCAM(1)
imgSize = cv.GetSize(cv.QueryFrame(capture))
img = cv.QueryFrame(capture)
#SOMETHING GOES HERE TO TURN FRAME INTO IMAGE
img = img.convert('L')
width, height = img.size
scanner = zbar.ImageScanner()
scanner.parse_config('enable')
zbar_img = zbar.Image(width, height, 'Y800', img.tostring())
# scan the image for barcodes
scanner.scan(zbar_img)
for symbol in zbar_img:
print symbol.data
With the python CV2, you can also do this:
import Image, cv2
cap = cv2.VideoCapture(0) # says we capture an image from a webcam
_,cv2_im = cap.read()
cv2_im = cv2.cvtColor(cv2_im,cv2.COLOR_BGR2RGB)
pil_im = Image.fromarray(cv2_im)
pil_im.show()
I think I may have found the answer. I'll edit later with results.
OpenCV to PIL Image
import Image, cv
cv_im = cv.CreateImage((320,200), cv.IPL_DEPTH_8U, 1)
pi = Image.fromstring("L", cv.GetSize(cv_im), cv_im.tostring())
Source: http://opencv.willowgarage.com/documentation/python/cookbook.html
Are you trying to obtain a RGB image? If that is the case, you need to change your parameters from this:
cv_im = cv.CreateImage((320,200), cv.IPL_DEPTH_8U, 1)
pi = Image.fromstring("L", cv.GetSize(cv_im), cv_im.tostring())
to that:
cv_im = cv.CreateImage((320,200), cv.IPL_DEPTH_8U, 3)
pi = Image.fromstring("RGB", cv.GetSize(cv_im), cv_im.tostring())
since it is documented almost nowhere, but the 'L' parameter of Image.fromstring is for 8-bit B&W images. Besides, you need to change the argument of your cv.CreateImage function from 1 (single channel image) to 3 (3 channels=RGB).
Hope it works for you.
Cheers
A simple way is to directly swap the channels. Suppose you are trying to convert a 3-channel image file between OpenCV format and PIL format. You can just use:
img[...,[0,2]]=img[...,[2,0]]
In this way, you won't be bothered with cv2.cvtColor as this function only works on images with certain depth.