I was playing around with the OpenCV library, and tried to open a small image, with the following code:
import cv2
img = cv2.imread(r'penguin.jpeg')
print(img)
To basically take a look at the array of pixels in the image, however the print simply returns None.
Both my .py file and the image are in my desktop, so I believe the problem is not the path.
I am also aware of some issues with imread() and JPEG images, however I get the same result with the PNG version of this image.
This had been working fine up until today, so I am kinda clueless.
Can anyone tell me what might be happening or what I might be doing less correctly?
Thank you so much in advance!
Consulting the OpenCV-Python Tutorials we are warned that
Even if the image path is wrong, it won’t throw any error, but print img will give you None
where print img is the Python 2 analog of your print(img).
The code you have written is correct and having replicated your problem, I print an array representation of a test penguin.jpeg image locally.
As commented by Rashid Ladjouzi, the path is probably incorrect especially given that you mention the script worked previously. I would test this with the following code which should return True:
import os
print(r'penguin.jpeg' in os.listdir("."))
Related
I am calling GDAL warp using the python distribution on a NITF file and it simply outputs all zero values which creates an empty black image. The command I'm calling is
import osgeo.gdal as gdal
gdal.Warp("out.ntf", "inp.ntf")
I've tried using Translate as sort of a test to make sure GDAL as a whole is functioning and it seems to output properly. The image data is all correct and displays as expected. Any thoughts as to what could be going wrong?
One thing that's important is to close the Dataset, depending a little on how you run it (script, repl, notebook etc).
This Python interface to the command-line utilities returns an opened Dataset, so you can explicitly close it with.
import osgeo.gdal as gdal
ds = gdal.Warp("out.ntf", "inp.ntf")
ds = None
That will cause for example anything in the GDAL-cache to be properly flushed to disk.
I am trying to use cv2.VideoCapture() on a MacBook in Python to read an *.avi video. but it is not working.
vid = cv2.VideoCapture('filename.avi')
vid.grab() # This gives me false.
also when I try to count the number of frames I usually get 0.0. However, when I use vid = cv2.VideoCapture(0) then vid.grab() I get True, I tried to figure out why I am getting this issue but with no luck.
On the other hand I tried using the Imageio module and used vid =imageio.get-reader('filename.avi') then counted the number of frames and I got 32 the correct results. However, I can't use Imageio because I want to the OpenCV Adaptive_Thresholding function and this function requires 8-bits 1-Channel and the output when using imageio is not, so, I get an error.
Does anyone know how to solve/work around this issue?
Thanks
I have followed the advanced ski-image panoramic image tutorial (1) and gotten it to work properly. However, trying to use UAV images (Adobe Buttes Flight 1 Raw) downloaded from (2), results in an interesting problem when using the skimage ImageCollection. Printing the problem images gives me some sort of wrapper:
"PIL.MpoImagePlugin.MpoImageFile image mode=RGB size=4000x3000 at 0x1EB09B86D30"
If I use io.imread to read a problem image it appears to work, printing a shape of "(2,)". However, attempting to print the individually read image gives a type error:
"unorderable types: int() >= MpoImageFile()"
followed by a system error:
"method-wrapper 'le' of MpoImageFile object at 0x000001EB09F20CC0 returned a result with an error set"
I'm really at a loss here. I'm relatively new to python and don't understand why the programs not working. The images are a bit large (5.65 mb) but my main program handles (slowly) the 'good' images.
I've tried the following solutions to no avail:
1) uninstalling pillow, installing both libjpeg & libz (the names seem to have changed) then reinstalling pillow github.com/scikit-image/scikit-image/issues/2000.
2) I'm not using a GPU, parallel processing, or tensorflow github.com/scikit-image/scikit-image/issues/2000,
3) I've also made sure my Anaconda version is fully updated
This is the minimalist example I made to demonstrate the issue. It should run in any jupyter notebook.
import numpy as np
from skimage.color import rgb2gray
from skimage import io
imgs = io.ImageCollection('test\*')
"""
print(imgs[0]) # Looks okay, numpy array
print(imgs[1]) # wrapper
print(imgs[2]) # Looks okay, numpy array
"""
for i in range(6):
print(np.shape(imgs[i]))
individual = io.imread('test\DJI_0002.jpg')
print(np.shape(individual))
#print(individual)
After some more testing this issue goes away when the images are resized at 50%. Is there a limit to the image size scimage can read? This is still not an acceptable solution, I would greatly prefer not having to resize all images that get pieced together.
I've got a pretty strange issue. I have several tif images of astronomical objects. I'm trying to use opencv's python bindings to process them. Upon reading the image file, it appears that segments of the images are swapped or rotated. I've stripped it down to the bare minimum, and it still reproduces:
img = cv2.imread('image.tif', 0)
cv2.imwrite('image_unaltered.tif', img)
I've uploaded some samples to imgur, to show the effect. The images aren't super clear, that's the nature of preprocessed astronomical images, but you can see it:
First set:
http://imgur.com/vXzRQvS
http://imgur.com/wig99KR
Second set:
http://imgur.com/pf7tnPz
http://imgur.com/xGn9C77
The same rotated/swapped images appear if I use cv2.imShow(...) as well, so I believe it's something when I read the file. Furthermore, it persists if I save as jpg as well. Opening the original in Photoshop shows the correct image. I'm using opencv 2.4.10, on Linux Mint 17.1. If it matters, the original tifs were created with FITS liberator on windows.
Any idea what's happening here?
I want to find which file is last modified.
I am modifying or updating the 'png' image on each request.
For example: piechart.png is an image; I am modifying this same image to create a new image (having same image name 'piechart.png'). I will be saving & modifying 5 images: (piechart1.png,piechart2.png,... ,piechart5.png).
How can I find which image was modified last ?
Well, this doesn't really answer your question, but it come close.
What you can do, is first download the PIL module, or the Python Imaging Library.
After that, get it installed, and then do the following:
from PIL import Image
Jpeg = Image.open("Yourfile.jpg")
print(Jpeg.info, Jpeg.format, Jpeg.mode, Jpeg.size)
This gives you some of the properties of the photo.
The same thing you can do for PNG images.
For more information about using the PIL module and getting your desired results you can go here:
http://python.developpez.com/cours/pilhandbook/php/image.php
Hope this helps!