I am very new to opencv. Here I am trying to create an Image which will be a shape made of a string with 0's and 1's (The function random_shape generates that string). So the string is to be typed on top of a white background . Here I am able to create a White Background Image but unable to add text in front of it. It just creates a blank image. It also gives a segmentation fault if I give it different coordinates.
Any Help is Appreciated .
font = cv2.FONT_HERSHEY_SIMPLEX
blank_image = np.zeros((100,100,3), np.uint8)
cv2.putText(blank_image,random_shape(100,100),(0,0), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,255,0))
blank_image[:,0:1*100] = (255,255,255)
image=cv2.cv.fromarray(blank_image)
cv2.cv.SaveImage('pic.jpg', image)
Update : I have changed my code to
blank_image = np.zeros((512,512,3), np.uint8)
blank_image[:,0:512] = (255,255,255)
cv2.putText(blank_image,random_shape(100,100)[0:100],(0,0), cv2.FONT_HERSHEY_\
SIMPLEX, 0.1,(0,0,0),cv2.CV_AA)
image=cv2.cv.fromarray(blank_image)
cv2.cv.SaveImage('pic.jpg', image)
It seems to be something garbled (its some sort of text with a line) instead of numbers. The size of the text also appears large.Any Help !!
First, you can just save an image using cv2.imwrite("filename.jpg", blank_image).
The docstring for cv2.putText(...) is:
putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) -> None
The second argument of cv2.putText, "text", is a string. Chances are, random_shape in your code does not store "valid" characters. So you are passing values to cv2.putText that cannot be "translated" to proper characters that can be displayed.
Why don't you debug by manually specifying a string in place of the values obtained from random_shape? That way, you can test whether the problem is with your code or with the libraries that OpenCV uses print characters or write to the image file etc.
It seems I needed to remove cv2.CV_AA and also in case of multiple lines of output there needs to be sufficient vertical spacing.
blank_image = np.zeros((512,512,3), np.uint8)
blank_image[:,0:512] = (255,255,255)
cv2.putText(blank_image,random_shape(100,100)[0:100],(10,10), cv2.FONT_HERSHEY_\
SIMPLEX, 0.2,(0,0,0))
image=cv2.cv.fromarray(blank_image)
cv2.cv.SaveImage('pic.jpg', image)
I managed to create something like this adding the above code in a function. Need to work a bit more on it.
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've got two images, the first one contain multiple items, which shows true colors. Then when I removed most of the item, then the webcam tried to auto-balance the image and yielded really false color.
Is there a way (in code) to apply the color profile of the first (true-color) image to the second image?
(or point me to some keywords, I'm new to the field, thanks)
Attached them here for easy comparison
True color
Falsely-adjusted color
I used Logitech webcam, which I can't figure out how to turn off auto-balance in code (in Linux).
I use this method and it works very well:
#pip install color_transfer
from color_transfer import color_transfer
# Load the two images
img1 = cv2.imread('image12.png')
img2 = cv2.imread('image1.png')
# Apply the color transfer
img2_transferred = color_transfer(img1, img2)
cv2.imshow("image", img2_transferred)
if cv2.waitKey(0) == chr("q"):
exit(0)
I have a given image. I want to create a black strip as an overlay on this image, with text written on the said strip. Here's a visual example of what I mean.
I'm using Python PIL to accomplish this (in a Django project), and here's what I've written so far:
from PIL import Image, ImageFont, ImageDraw
img_width, img_height = img.size #getting the base image's size
if img.mode != 'RGB':
img = img.convert("RGB")
strip = Image.new('RGB', (img_width, 20)) #creating the black strip
draw = ImageDraw.Draw(strip)
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 16)
draw.text((img_width/2,10),"foo bar",(255,255,255),font=font) #drawing text on the black strip
offset = (img_width/2,img_height/2)
img.paste(strip,offset) #pasting black strip on the base image
# and from here on, I save the image, create thumbnails, etc.
This isn't working at all. As in, the image appears without any text or black strip, like it originally was.
Note that if I directly try to write on the image (sans the black strip), it works perfectly. Moreover, image processing itself is working perfectly too (i.e. in cases where I don't write anything on the image).
Can anyone help me point out the problem? Is something wrong with the position (or offset)? Am I pasting it wrong? Is RGB conversion to blame? Or is it something else entirely? An illustrative example would be great. Btw performance matters too; I'm trying to do this as costlessly as I can.
In case it matters, here's what I do with the image file later:
from django.core.files.uploadedfile import InMemoryUploadedFile
img.thumbnail((300, 300))
thumbnailString = StringIO.StringIO()
img.save(thumbnailString, 'JPEG', optimize=True)
newFile = InMemoryUploadedFile(thumbnailString, None, 'temp.jpg','image/jpeg', thumbnailString.len, None)
# and then the image file is saved in a database object, to be served later
The problem is with offset. The docs Image.paste says:
If a 2-tuple is used instead, it’s treated as the upper left corner.
So with (img_width/2, img_height/2), you're pasting the strip with it's top-left corner in the middle of the big image. Here it is pasting "foo bar" onto your example picture:
If you change it to offset = (0, img_height/2), it pastes it halfway down but from the left. Here's "foo bar" pasted into the correct location:
The strip could do with being a bit taller (the height could be calculated from the text at the given font size), and the text could be centred, but I expect those things have already been answered elsewhere on Stack Overflow or in the Pillow docs.
I am trying to do some white blob detection using OpenCV. But my script failed to detect the big white block which is my goal while some small blobs are detected. I am new to OpenCV, and am i doing something wrong when using simpleblobdetection in OpenCV? [Solved partially, please read below]
And here is the script:
#!/usr/bin/python
# Standard imports
import cv2
import numpy as np;
from matplotlib import pyplot as plt
# Read image
im = cv2.imread('whiteborder.jpg', cv2.IMREAD_GRAYSCALE)
imfiltered = cv2.inRange(im,255,255)
#OPENING
kernel = np.ones((5,5))
opening = cv2.morphologyEx(imfiltered,cv2.MORPH_OPEN,kernel)
#write out the filtered image
cv2.imwrite('colorfiltered.jpg',opening)
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
params.blobColor= 255
params.filterByColor = True
# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
detector = cv2.SimpleBlobDetector(params)
else :
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
keypoints = detector.detect(opening)
# Draw detected blobs as green circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
# the size of the circle corresponds to the size of blob
print str(keypoints)
im_with_keypoints = cv2.drawKeypoints(opening, keypoints, np.array([]), (0,255,0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Show blobs
##cv2.imshow("Keypoints", im_with_keypoints)
cv2.imwrite('Keypoints.jpg',im_with_keypoints)
cv2.waitKey(0)
EDIT:
By adding a bigger value of area maximum value, i am able to identify a big blob but my end goal is to identify the big white rectangle exist or not. And the white blob detection i did returns not only the rectangle but also the surrounding areas as well. [This part solved]
EDIT 2:
Based on the answer from #PSchn, i update my code to apply the logic, first set the color filter to only get the white pixels and then remove the noise point using opening. It works for the sample data and i can successfully get the keypoint after blob detection.
If you just want to detect the white rectangle you can try to set a higher threshold, e.g. 253, erase small object with an opening and take the biggest blob. I first smoothed your image, then thresholding it:
and the opening:
now you just have to use findContours and take the boundingRect. If your rectangle is always that white it should work. If you get lower then 251 with your threshold the other small blobs will appear and your region merges with them, like here:
Then you could still do an opening several times and you get this:
But i dont think that it is the fastest idea ;)
You could try setting params.maxArea to something obnoxiously large (somewhere in the tens of thousands): the default may be something lower than the area of the rectangle you're trying to detect. Also, I don't know how true this is or not, but I've heard that detection by color is bugged with a logic error, so it may be worth a try disabling it just in case that is causing problems (this has probably been fixed in later versions, but it could still be worth a try)
I am working with multiple images that I would like to stack on top of each other to create a single image. However, in working with them, I'm noticing that if the image already has transparency (alpha != 255), that part of the image appears faded. If there is no transparency, all is good.
I saved one of the images I was working with to a PNG and created a small bit of code that duplicates the problem. Essentially, I'm creating a new image with a transparent background and then pasting the image on top:
from PIL import Image
img=Image.new('RGBA', (946,627), (0,0,0,0))
overlayImage = Image.open('drawing.png')
img.paste(overlayImage, (0,0), overlayImage)
img.save('drawing-pasted.png')
When this completes, drawing-pasted.png looks like this:
But the original drawing (drawing.png) looked like this:
(Images cropped manually to show detail.) The original image circles fill color has an alpha value of 179.
Has anyone else encountered this, and what am I doing wrong?
Thanks much.
image = Image.open(file_path)
img1 = Image.open(file_path)
cords = (233.22)
image.paste(img1, cords, mask=img1)
image.save(path_where_you_want_to_save_final_image)
simply use this block of code
The background you are creating is black and fully transparent the original is blue but with an alpha of 179 so you have 2 pixels (0,0,0,0) and (0,0,255,179) assuming 100% blue - since you are pasting the image in it will be over the background so will use the alpha of the new image allowing (255-179)/255 or about 30% black. (N.B. The alpha of the background makes no difference since it is behind the new image)
You could use overlayImage.putalpha to set the alpha to 255 start from your image rather than a black background.