Reading Image file in Opencv without ext - opencv

I am trying to download an image from URL, but it's difficult to get the complete file name and extension.
Currently, I save the downloaded image with any name and without an extension.
with open(path, "wb") as f:
f.write(request.content)
I then use opencv to read this image which has no extension and it works. How is this possible?

opencv tries magic numbers / signatures for known image formats:
https://en.wikipedia.org/wiki/Magic_number_(programming)
Implementation details:
https://github.com/opencv/opencv/blob/666be238d84c339993bf18c0798d27afd420d6be/modules/imgcodecs/src/loadsave.cpp#L209

Related

Convert Dicom Image to JPG

I am trying to convert a dicom file to JPG which I would then use to show as thumbnail for the dicom file. I am using the code below to convert the dicom file but When I execute it I get the following error
'write': no encode delegate for this image format `' # error/constitute.c/WriteImage/1272 (Magick::ImageMagickError)
I am using ruby version 2.7.0 and I have installed ImageMagick 7.1.0-4 and the supportive gems. How to solve this error?
require 'dicom'
require 'rmagick'
include DICOM
dcm = DObject.read("sample.dcm")
image = dcm.image(:frame => 5)
image.normalize.write("test.jpeg");
I had to reinstall ImageMagick-7.1.0-26 along with libgdcm-tools on my system and the above mentioned code worked fine. So Imagemagick needs these additional libraries (libjpeg62-dev, libtiff-dev, libpng-dev, libpng12-0 and libgdcm-tools) to work with these image types. I don't know if this is correct or not but that the problem I was facing has been resolved by following this approach. Here are some of the links which helped me in solving this
Install ImageMagick with JPG TIFF and PNG Delegates
How to configure and build with libgcdm for lossless jpeg conversion
and dicom support

jpg images without extension aren't displayed

from kivy.uix.image import Image
self.img = Image(source="image") # This works when image is an PNG image
self.img = Image(source="image.jpg") # This works when image.jpg is a JPG image
self.img = Image(source="image") # This doesn't work when image is a JPG image
I need to specify images without extention for the app to be generic (working with more image types). Can I achieve it somehow?
Kivy is using "imghdr" to determine the image type here, and as a fallback it uses the file extension here.
That explains why the image loads fine when it has a file extension, even though "imghdr" can't find the file type in the file's content.
I tested on a list of JPEG files, and each time "imghdr" was able to detect the file type each time. That is done here im imghdr. Notably, "imghdr" does not consider the file extension.
$ python
>>> import os, imghdr
... for f in os.listdir('.'):
... print('%s -- %s' % (f, imghdr.what(f)))
Maybe the JPEG file is missing the "JFIF" or "Exif" string that imghdr is looking for? You could use hexedit to see if one of those string is present at Byte 6 of the image file.

how can I load images in base64 string by url-loader in typescript + react (rails5 + webpacker)

example repo https://github.com/github0013/typescript_react
I need to load images in base64 string embeded into tag, but importing image files will raise this error.
TS2307: Cannot find module './Octocat.jpg'.
https://github.com/github0013/typescript_react/commit/6670f3ca74404007ad1ed6e4ab0d111f547ee75d
I am still new to typescript, webpack and react, and I am sure I am confused typescript's import and webpack's.
How do I enable url-loader for images? and is this even valid?
https://github.com/github0013/typescript_react/blob/master/config/webpack/environment.js
--
I did try require but it will just return image's path instead of base64 stirng.
// global.d.ts
declare function require(string): string;
solved..
webpacker 3.0.2 already had loaders for image files
https://github.com/rails/webpacker/tree/v3.0.2/package/loaders
https://github.com/rails/webpacker/blob/v3.0.2/package/loaders/file.js
environment.loaders, in rails' config/webpack/environment.js, is just a map, and jpeg png gif are in file loader.
https://github.com/github0013/typescript_react/commit/dc97c3aa9f17bc4f3b0210a426aac27092c5ae7f#diff-41713e6b116dc5cc2bbea556a85db322R3
so overriding file loader's test, then add own image loader will use url-loader for jpeg png gif
https://github.com/github0013/typescript_react/commit/dc97c3aa9f17bc4f3b0210a426aac27092c5ae7f
after adding the loader, regular image require in typescript will return base64 string.

Downloaded image file is corrupt

I'm making a simple Lua script to download images. I get the URL of the image, and then this is my code to download it:
content = http.request(imageurl)
file = io.open("E:\\Users\\Me\\Documents\\Lua\\IMGDownload\\output.jpg", "w")
file:write(content)
print("Wrote content")
I get a 4KB file, however it isn't what I want it to be.
For reference, here is the image that I want to download:
RealImage http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/bd/bd05e23129b5d03ecb3f933589ff1477fbff4e92_full.jpg
This is what I actually get:
Can anyone pinpoint me as to the cause?
You probably just need to open the file with "wb" mode to get Windows to open the file in binary mode and not do line-ending conversion on you.
Try io.open("E:\\Users\\Me\\Documents\\Lua\\IMGDownload\\output.jpg", "wb").

using imread of OpenCV failed when the image is Ok

I encountered a problem when I want to read an image using the OpenCV function imread().
The image is Ok and I can show it in the image display software.
But when I use the imdecode() to get the image data, the data returns NULL.
I will upload the image and the code and hope some one could help me
Mat img = imread(image_name);
if(!img.data) return -1;
The image's link is here: http://img3.douban.com/view/photo/raw/public/p2198361185.jpg
PS: The image_name is all right.
I guess OpenCV cannot decode this image. So is there any way to decode this image using OpenCV?, like add new decode library. By the way, I can read this image using other image library such as freeImage.
Your image is in .gif and it is not supported by OpenCV as of now.
Note OpenCV offers support for the image formats Windows bitmap (bmp),
portable image formats (pbm, pgm, ppm) and Sun raster (sr, ras). With
help of plugins (you need to specify to use them if you build yourself
the library, nevertheless in the packages we ship present by default)
you may also load image formats like JPEG (jpeg, jpg, jpe), JPEG 2000
(jp2 - codenamed in the CMake as Jasper), TIFF files (tiff, tif) and
portable network graphics (png). Furthermore, OpenEXR is also a
possibility.
Source - Click here
You can use something like this, to perform the conversion.
I was able to load your image using imread using this. Also, you can check out FreeImage.
You can also try to use the library gif2numpy. It converts a gif image to a numpy image which then can be loaded by OpenCV:
import cv2, gif2numpy
np_images, extensions, image_specs = gif2numpy.convert("yourgifimage.gif")
cv2.imshow("np_image", np_images[0])
cv2.waitKey()
The library can be found here: https://github.com/bunkahle/gif2numpy It is not dependent on PIL or pillow for this like imageio.
There are two methods to read an image in OpenCV, one is using Mat the other one using IplImage. I see you have used the former one. You can try with the second argument of imread also:
image = imread("image.jpg", CV_LOAD_IMAGE_COLOR); // Read the file
else use IplImage
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <opencv2/core/core.hpp>
IplImage* src = 0;
if( (src = cvLoadImage("filename.jpg",1)) == 0 )
{
printf("Cannot load file image %s\n", filename);
}
If they don't work please check if you have installed libjpeg, libtiff and other dependencies for reading an image in OpenCV.
Hope it would help.

Resources