How do I save particular frames of video into folders using python? - python

I have got a code that does motion detection. It takes video as an input and then when an object comes to the scene, background changes and code publishes "object is seen" as text on that detected frame.
My question is that how do I save frames "when objects are seen" into folders( both for colorful and gray images)? I understand that I can write that frames using " cv2.imwrite("frame%d.jpg" % count, resizedFrame)" but unfortunately it does not work.
How do I save those detected frames to "colorful" and "gray" folders?

You have to read the image first by cv2.imread() and then cv2.imwrite()
here is a small example
import
cv2
# read image as grey scale
grey_img=cv2.imread('/home/img/python.png',cv2.IMREAD_GRAYSCALE)
# save image
status=cv2.imwrite('/home/img/python_grey.png',grey_img)
print("Image written to file-system : ",status)
If you're don't have the folder path then use this
status=cv2.imwrite('python_grey.png',grey_img)
it will save the photo in default folder in which you save your .py file
if you want to save different images here is the code
import cv2
import time
# for number of images you need different names so set name automatically
name=str(time.time())+str(.png))
# read image as grey scale
grey_img=cv2.imread('/home/img/python.png',cv2.IMREAD_GRAYSCALE)
# save image
status=cv2.imwrite('%s'%name,grey_img)
print("Image written to file-system : ",status)

Related

How to find a file/ data from a given data set in python- opencv image processing project?

I have a data set of images in an image processing project. I want to input an image and scan through the data set to recognize the given image. What module/ library/ approach( eg: ML) should I use to identify my image in my python- opencv code?
To find exactly the same image, you don't need any kind of ML. The image is just an array of pixels, so you can check if the array of the input image equals that of an image in your dataset.
import glob
import cv2
import numpy as np
# Read in source image (the one you want to match to others in the dataset)
source = cv2.imread('test.jpg')
# Make a list of all the images in the dataset (I assume they are images in a directory)
filelist = glob.glob(r'C:\Users\...\Images\*.JPG')
# Loop through the images, read them in and check if an image is equal to your source
for file in filelist:
img = cv2.imread(file)
if np.array_equal(source, img):
print("%s is the same image as source" %(file))
break

Wrong displayed image name in image viewer python 3.5

When i open an image in image viewer the displayed image name is wrong (not the same as loaded image). Orginal image = 'image.PNG', name in image viewer='tmpy4uvijg0.BMP' (the new name always changeds, see in image below)
from PIL import Image
imName='image.PNG'
try:
with Image.open(imName) as im:
print(imName)
im.show()
except IOError:
pass
image.png
new image
What do i wrong? Why is the name not the same?
It is because the show method save the image to a temporary file, as say in the documentation:
Displays this image. This method is mainly intended for
debugging purposes.
On Unix platforms, this method saves the image to a temporary
PPM file, and calls the xv utility.
On Windows, it saves the image to a temporary BMP file, and uses
the standard BMP display utility to show it (usually Paint).
:param title: Optional title to use for the image window,
where possible.
:param command: command used to show the image
You can try to change the title by passing a string in parameter to show.

Python saving multiframe TIFF as multiple JPEG

I have a TIFF file with multiple frames. I found that if I open the TIFF file and call the seek(1) I can seek to the second frame. This is working, except when I go to save the image as a jpg, it only saves the first frame and not my current frame.
How can I save multiple frames to different JPG files?
from PIL import Image
im = Image.open('test.tiff')
im.save('test.jpeg')
I need to do something like...
from PIL import Image
im = Image.open('test.tiff')
im.seek(1)
im.save('test.jpeg')
and have it save the second frame and not the first.

Django: Download Image from URL, Resize it, append "small" to the end of the filename

I'm looking for a way to download an 640x640 image from a URL, resize the image to 180x180 and append the word small to the end of the resized image filename.
For example, the image is located at this link
http://0height.com/wp-content/uploads/2013/07/18-japanese-food-instagram-1.jpg
Once resized, I would like to append the world small to the end of the filename like so:
18-japanese-food-instagram-1small.jpeg
How can this be done? Also will the downloaded image be saved to memory or will it save to the actual drive? If it does save to the drive, is it possible to delete the original image and keep the resized version?
Why don't you try urllib?
import urllib
urllib.urlretrieve("http://0height.com/wp-content/uploads/2013/07/18-japanese-food-instagram-1.jpg", "18-japanese-food-instagram-1.jpg")
Then, to resize this you can use PIL or another library
import Image
im1 = Image.open("18-japanese-food-instagram-1.jpg")
im_small = im1.resize((width, height), Image.ANTIALIAS)
im_small.save("18-japanese-food-instagram-1_small.jpg")
References:
http://www.daniweb.com/software-development/python/code/216637/resize-an-image-python
Downloading a picture via urllib and python

python Image pixel editing

I'm working on a program where I open an image file(jpg), edit some of the pixels, and save the image with a new file name. It seems, however, that even when I am not editing any pixels, they are still being altered. This is a quick sample I wrote up. All I am doing is opening an image and saving it with a different name.
import Image
img1 = Image.open('image.jpg')
print img1.getpixel((0,0))
img1.save('testimage.jpg')
img2 = Image.open('testimage.jpg')
print img2.getpixel((0,0))
The output of the first print statement is (253,254,248) and the output of the second is (253,251,255). Why are the Green and Blue values changing if I am doing absolutely nothing to the image?
JPEGs are lossy: saving a JPEG may result in quality loss and thus changed pixels. If you want to edit without (further) losing quality, save as .png or some other lossless format instead.

Categories