I want read 100 colour images and use them for further processing. Suppose one image size is 256x 256 by reading it in python openCV its size is (256,256,3). I now want to read 100 images and after reading I have to get the size as (100,256,256,3).
You could do something like this, supposing that your images are named like 0.png to 99.png:
import numpy as np
result=np.empty((100,256,256,3))
for i in range (100):
result[i,:,:,:] = cv2.imread('{}.png'.format(i),1)
## your image names
#fnames = sorted(glob.glob("images/*.png"))
## read and stack
img = np.stack([cv2.imread(fname) for fname in fnames])
Related
Let's assume I have multiple .jpg files in a directory and want to make an .mp4 file out of them - how can I do that but with an individual duration for each .jpg?
E.g. the first image should be shown for 3 seconds, the second one should be visible for 7 seconds, ...
Is it possible to achieve something like that with exisiting libraries?
Or should I just include the images multiple times into the directory, depending on their duration (which would end up making this inaccurate if I want an image to be shown less than a second)?
Thanks in advance!
Maybe you should try using OpenCV
import cv2
import numpy as np
import glob
img_array = []
for filename in glob.glob('C:/New folder/Images/*.jpg'):
img = cv2.imread(filename)
height, width, layers = img.shape
size = (width,height)
img_array.append(img)
out = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()
source: https://theailearner.com/2018/10/15/creating-video-from-images-using-opencv-python/
I need to create 30 * 30 px small images from splitting a large image. I need these parts separately saved in separate files after being split up like so:
Here you go:
import cv2
img = cv2.imread('image.png')
for r in range(0,img.shape[0],30):
for c in range(0,img.shape[1],30):
cv2.imwrite(f"img{r}_{c}.png",img[r:r+30, c:c+30,:])
Try use image_slicer. (8 x 8 from your description)
import image_slicer
image_slicer.slice('C:\\files\\cars.png', 64)
This' how the outputs looked like.
I have two GIF files, and I want to combine them horizontally to be shown beside each other, and they play together.
They have equal frames.
I tried a lot online for a solution, but didn't find something which is supporting GIF. I think the imageio package supports gif, but I can't find a way to use it to combine two together
Simply, I want something like this example
Any ideas of doing so ?
I would code something like this :
import imageio
import numpy as np
#Create reader object for the gif
gif1 = imageio.get_reader('file1.gif')
gif2 = imageio.get_reader('file2.gif')
#If they don't have the same number of frame take the shorter
number_of_frames = min(gif1.get_length(), gif2.get_length())
#Create writer object
new_gif = imageio.get_writer('output.gif')
for frame_number in range(number_of_frames):
img1 = gif1.get_next_data()
img2 = gif2.get_next_data()
#here is the magic
new_image = np.hstack((img1, img2))
new_gif.append_data(new_image)
gif1.close()
gif2.close()
new_gif.close()
So the magic trick is to use the hstack numpy function. It will basically stack them horizontally. This only work if the two gif are the same dimensions.
I am doing subtitle extraction from videos in python.I have used opencv in python to do this.I have divided it into frames and for each frame as image which will be stored in my disk, i am doing ocr on it.But I dont want to perform ocr on the entire image.I just want the subtitle part.I manually cropped the image with these values 278:360 as my image size was 360:640.But the image size varies for different video files.Now my question is how to crop the subtitle part alone programatically.Please do answer.Thanks in advance
textImage = image[278:360,:]
You can take the last third of the image height, if you are sure that the subtitles will be there.
For instance, for the following image:
Proceed as follows:
read the image into a numpy array :
In my example, I am using imread from skimage.io, but you can use opencv:
from skimage.io import imread
img = imread('http://cdn.wccftech.com/wp-content/uploads/2017/05/subtitle-of-a-blu-ray-movie.jpg')
img.shape # >>> (383, 703, 3)
Get the bottom third of the image (which contains the subtitle):
The idea is to divide the height of the image by 3 and take the bottom third of the image:
crop_position = int(img.shape[0]/3)
subtitle_img = img[img.[0] - crop_position:,:,:]
The resulting subtitle_img looks like this:
In my case I use only one library and regular operations on arrays:
import matplotlib.image as mpimg
image= mpimg.imread('someImage.jpg')
#Example for bottom half of an image, but you can replace this with your parameter
crop_position = image.shape[0] // 2
half_imagage = image[image.shape[0] - crop_position:,:]
And it returns a nice image:
I use PIL.Image library to open a tif file and convert it to numpy array:
PIL.ImageFile.LOAD_TRUNCATED_IMAGES = True # because my tif file is big
im = PIL.Image.open('1.tif')
arr = numpy.asarray(im)
I found weird result, so I did the following test:
im1 = PIL.ImageFile.fromarray(arr,"CMYK") # tif file is CMYK value
im1.save('new.tif')
It turned out that the new tif file doesn't look like the original tif file. Suppose the 1.tif looks like A, then new.tif looks like several A tiled together, each of A is smaller, but the total size is the same. I don't know where I do it wrong.
Update:
Both x and y direction, the image is repeated 4 times, so together is 16 times. But the total size of the new tif remains the same. Any ideas?