Why does the search image in my code doesn't work? - python

This is my code:
import pyautogui
images = ['colordonkergrijsDCphrasev2.png'] #the image I want it to find
while True:
for image in images: #search for the image in images
pos = pyautogui.locateOnScreen(image, region=(740,870, 50, 20)) #search on the screen for the image
if pos is not None: # this checks that the image was found
pyautogui.click(pos) # click the position of the image
I want that my code clicks on a special region when he is seeing that image.
But my code doesn't do that, my code clicks every time also when the image isn't there. The image I'm using is very similar as the background. But I added confidence = 1 and it still doesn't work
Does someone know how to fix it?
I use Python 3.9.4 64-bit.
I already read the docs but there isn't anything in there what can help me.
If you want to docs here it is: https://pyautogui.readthedocs.io/en/latest/screenshot.html

Related

how to take a screnshot by Pyautogui automatically and randomly

objective i want to extract text from image.
i play a game which an icon appears randomly ,and there is a text(text as image) near to the icon from the right.
i want the script take screenshot of the region of the text only.
so, i want the script every time he locatonscreen the i con, i want him take screen shot of the text.
here is an image to understand the idea :
enter image description here
this is my code:
import pyautogui as py
import time
from PIL import Image
from pytesseract import *
pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
while 1:
indice1 = py.locateOnScreen("icon.png")
if indice1:
print("indice see it ")
myScreenshot = py.screenshot()
myScreenshot.save(r'C:\Users\rachidel07\Desktop\ok\venv\image.png')
img=Image.open("image.png")
output = pytesseract.image_to_string(img)
print(output)
else:
print ("non")
If you just want the text, check for the icon and when it does find it, take a picture of the whole box with coordinates relative to the icon (you get this easily get this since locateonscreen returns coordinates and you can just measure how big the text box is and do the math.) Then use PIL to crop only the text and then use tesseract for ocr.
To crop the text, you would use the crop() from PIL.
from PIL import Image
img = Image.open("image.png")
newimg = img.crop((100, 100, 150, 150))
newimg.save("croppedimage.png")

How to set a background image in python pptx

I am currently working on a project aiming to create a PowerPoint thanks Python pptx. However I am trying to set an image as the background of the slide and I can’t seem to find the solution in the docs of Python pptx. Is it possible to set an image as background if so can someone help me ? If it is not does anyone know another solution using python ?
Thank you
import os
import fnmatch
from pptx import Presentation
#Create presentation and setting layout as blank (6)
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
#Find number of slides to create
#First Method = Count number of images in screenshot files (change path depending on the user)
nbSlide = len(fnmatch.filter(os.listdir("mypath"), '*.jpeg'))
#Loop to create as number of slides as there is report pages
for i in range(nbSlide):
slide = prs.slides.add_slide(blank_slide_layout)
#change background with an image of the slide …
background=slide.background
#Final step = Creation and saving of pptx
prs.save('test.pptx')
Is it possible to set an image as background ... ?
So far with python-pptx there is no direct way to insert image as background of an slide
If it is not does anyone know another solution using python ?
You could insert picture of interest into given slide on the regular basis, considering proper width/height parameters:
#Loop to create as number of slides as there is report pages
for i in range(nbSlide):
slide = prs.slides.add_slide(blank_slide_layout)
#change background with an image of the slide …
left = top = 0
pic = slide.shapes.add_picture('/your_file.jpeg', left-0.1*prs.slide_width, top, height = prs.slide_height)

Lower the brightness of all RGB pixels by 20% in Python?

I have been trying to teach myself more advanced methods in Python but can't seem to find anything similar to this problem to base my code off of.
First question: Is this only way to display an image in the terminal to install Pillow? I would prefer not to, as I'm trying to then teach what I learn to a very beginner student. My image.show() function doesn't do anything.
Second question: What is the best way to go about lowering the brightness of all RGB pixels in an image by 20%? What I have below doesn't do anything to the alter the brightness, but it also can compile completely. I would prefer the most simple way to go about this as far as importing minimal libraries.
Third Question: How do I made a new picture instead of changing the original? (IE- lower brightness 20%, "image-decreasedBrightness.jpg" is created from "image.jpg")
here is my code - sorry it isn't formatted correctly. Every time i tried to indent it would tab down to the tags bar.
import Image
import ImageEnhance
fileToBeOpened = raw_input("What is the file name? Include file type.")
image = Image.open(fileToBeOpened)
def decreaseBrightness(image):
image.show()
image = image.convert('L')
brightness = ImageEnhance.Brightness(image)
image = brightness.enhance(20)
image.show()
return image
decreaseBrightness(image)
To save the image as a file, there's an example on the documentation:
from PIL import ImageFile
fp = open("lena.pgm", "rb")
p = ImageFile.Parser()
while 1:
s = fp.read(1024)
if not s:
break
p.feed(s)
im = p.close()
im.save("copy.jpg")
The key function is im.save.
For a more in-depth solution, get a nice beverage, find a comfortable place to sit and enjoy your read:
Pillow 3.4.x Documentation.

How do I convert an RGB picture into graysacle using simplecv?

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

Why are histograms the same [python + simplecv]

My question is why are the two histograms in following code the same.
Because the picture does change, first show shows original picture and second shows completely black picture.
Am I miss-using simpleCV or is this perhaps a bug?
Code:
from itertools import product
from SimpleCV import Image
from SimpleCV import Color
if __name__ == '__main__':
pass
def number_of_hues(picture):
image = Image(picture)
#convert the picture's space to HSV
image = image.toHSV()
image.show()
original_histogram = image.histogram()
(image_x_length, image_y_length) = image.size()
for i,j in product(range(image_x_length), range(image_y_length)):
image[i,j] = Color.BLACK
image.show()
new_histogram = image.histogram()
for o,n in zip(original_histogram, new_histogram):
if o != n:
print o,n
When was the last time you did a pull from the develop github repo? There was a bug in the set item call for the image class that kept images from getting set directly. It was fixed a couple weeks ago. Generally you should try to avoid directly looping over image objects and setting pixels directly as it can be really slow. If you think you found a bug please submit an issue to our github repo and we will try to address it as soon as we can.

Categories