How to open an image in Paint and save it by Python? - python

I'm trying to write a code in python that is able to open an image in paint and save it with a different name of the original one.
My code is the follow:
import os
import subprocess
new_path='img.png'
path_img='./img.jpeg'
paintPath = os.path.splitdrive(os.path.expanduser("~"))[0]+r"\WINDOWS\system32\mspaint.exe"
subprocess.call([paintPath, path_img])
At this point, my image appears on Paint, but I would like automatically to save it and close the program, if it is possible. I didn't find any solution online.

Related

Screenshot with selenium and python

I need to take a screenshot of my entire screen for some automated tests I need to perform.
I was able to do this, using driver.get_screenshot_as_file , but the problem is that it only takes the picture of the web page, I need to get the whole picture from the browser, since the data I need to check is in the devtools.
Pic:
enter image description here
I need this:
enter image description here
Thankss!
You can use the package pyautogui to get screenshot of the desktop on the os level. This take screenshot of the entire desktop rather than just the webpage.
import pyautogui
pyautogui.screenshot().save('screenshot.png')
Another alternative to pyautogui would be PIL's ImageGrab. The advantage is that you are able to specify a bounding box:
from PIL import ImageGrab
image = ImageGrab.grab(bbox=None) # bbox=None gives you the whole screen
image.save("your_browser.png")
# for later cv2 use:
import numpy
import cv2
image_cv2 = cv2.cvtColor(numpy.array(image), cv2.COLOR_BGR2RGB)
This also makes it possible to adapt to your browser's window size and only capture its specific window. You can get your browsers bounding box as shown in this answer: https://stackoverflow.com/a/3260811/20161430.
From a speed perspective, it doesn't seem to make much of a difference whether you are using pyautogui or PIL.

How To Save Image Via Image Processing [Raspberry Pi]

I am creating a project with python and Raspberry Pi. I am trying to use my Webcam, as I, unfortunately burned my Camera Module. I was following along: https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/image_processing/
Everything is working fine, except for one problem. I am not able to save the image file that is captured. I would like to take the photo I have created and turn it into a .jpg image. Code I have currently:
from imgproc import *
import cv2
# open the webcam
my_camera = Camera(320, 240)
# grab an image from the camera
my_image = my_camera.grabImage()
# open a view, setting the view to the size of the captured image
my_view = Viewer(my_image.width, my_image.height, "ETSBot Look")
# display the image on the screen
my_view.displayImage(my_image)
# wait for 5 seconds, so we can see the image
waitTime(0)
Can someone please help me with this problem?
Thanks in advance!
-Saurish Srivastava
Custom Library: https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/downloads/
UPDATE: It does not have to just use this type of code. You can give me an example with a different software. Just tell me how to use it properly so I don't mess up.
Adding the following in your code should save the image in the array my_image as picture.jpg
cv2.imwrite('picture.jpg', my_image)
For details on configuring raspberry pi-http://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/

Image Recognition - pyautogui

I open up Calculator from window. I use the snipping tool to copy an image of the number 7 button. I paste the image into the paint software and save it as a png file and save it in a directory on my desktop.
I open up the calculator, use this code to locate where the image is on the screen. However the code return a blank space when normally it should return the position of the image on the screen. The first time I ran it, it gave me a coordination but the second time, it just shows me a blank space and I have been trying to figure out why. I kept doing it over and over, re-copied, re-saved the image and rerun the code and it's still the same result, blank. Was wondering what could be the reason.
>>> import pyautogui
>>> pyautogui.locateOnScreen('C:\\Users\\js\\Desktop\\jsPython\\seven2.png')
Maybe you should check your path string.For example, this code runs fine:
import pyautogui
print(pyautogui.locateOnScreen("C:\Python27\source\pyautogui\images\startIcon.png"))
I think you've made a typo in your path string.
Even better solution is to use absolute path.For example :
import pyautogui,os
print(pyautogui.locateOnScreen(os.path.abspath("images\startIcon.png")))

Unable to show an image using python PIL Image.show

I'm using the Python Imaging Library and I am unable to open an image successfully in Windows Live Photo Gallery. There is a message that shows up saying "There are no photos or videos selected" instead of the image.
This is what I've tried:
import Image
img = Image.open(r"C:\Users\User\Pictures\image.jpg")
img.show()
This is pretty much the same as in the PIL handbook tutorial, so I'm not sure where I'm going wrong.
The documentation says:
On Windows, it [show()] saves the image to a temporary BMP file, and uses the standard BMP display utility to show it.
Problem is that your program exits immediately somehow, the temporary file is deleted upon exit and Windows etc. cannot find it. As a temporary solution, try adding:
import time
# Your code as above
time.sleep(30)
This will make the program wait 30 seconds before exiting. If you prefer, you could make it wait the user to press a key.
EDIT: it seems like you are experiencing problems with temporary files. As a workaround, save the image somewhere on the disk using, say, img.save("C:\Users\User\Pictures\test.jpg") and open it with your favorite image viewer. Whenever you want to show the processed image, call save again and reload the picture in the image viewer.

How do I display and close an image with Python?

I would like to display an image with Python and close it after user enters the name of the image in terminal. I use PIL to display image, here is the code:
im = Image.open("image.jpg")
im.show()
My application display this image, but user task is to recognize object on image and write answer in terminal. If answer entered is correct user should get another image. Problem with PIL is that I can't close the image and with research the only solution was to kill the process of image viewer, but this is not really reliable and elegant.
Are there any other libraries for displaying images that have methods like .show() and .close() ?
Just open any image viewer/editor in a separate process and kill it once user has answered your question e.g.
from PIL import Image
import subprocess
p = subprocess.Popen(["display", "/tmp/test.png"])
raw_input("Give a name for image:")
p.kill()
A little late to the party, but (as a disgruntled data scientist who really can't be bothered to learn gui programming for the sake of displaying an image) I can probably speak for several other folks who would like to see an easier solution for this. I figured out a little work around by expanding Anurag's solution:
Make a second python script (let's call it 'imviewer.py'):
from skimage.viewer import ImageViewer
from skimage.io import imread
img = imread('image.png') #path to IMG
view = ImageViewer(img)
view.show()
Then in your main script do as Anurag suggested:
import subprocess
p = subprocess.Popen('python imviewer.py')
#your code
p.kill()
You can make the main script save the image you want to open with 'imviewer.py' temporarily, then overwrite it with the next image etc.
Hope this helps someone with this issue!
Terminal is meant to deal with linear command flow - meaning it asks a question, user answers, and then it can ask a different question. What you are trying to do here is for terminal to do two things, show an image and at the same time ask user a question. To do this you can do two of either things:
Multiprocessing
You can start a new thread/process and make PIL show the image using that thread, and meanwhile in the first thread/process ask a user a question. Then after the user answers, you can close the other thread/process. You can take a look at Python's threading module (link) for more information on how you can do that.
GUI
Instead of making your user interface in terminal, make a simple GUI application using whatever framework you are comfortable. I personally like PyQt4. Qt is very powerful GUI development toolkit and PyQt4 is a wrapper for it. If you make a GUI, then what you are tyring to do is rather trivial.
Not all GUIs are difficult to use.
Here is a single-line solution using PySimpleGUI. Normally I wouldn't write it as a single line, but since it's a one-off, perhaps doesn't need adding to, then it's OK to do.
import PySimpleGUI as sg
sg.Window('My window').Layout([[ sg.Image('PySimpleGUI.png') ]]).Read()
Might be an overkill, but for me the easiest and most robust solution was just to use matplotlib as it properly keeps track of the figures it creates, e.g. :
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
imgplot = plt.imshow(mpimg.imread('animal.png'))
plt.ion()
plt.show()
animal_name = raw_input("What is the name?: ")
plt.close()

Categories

Resources