I currently have a .h5 file containing grayscale imagery. I need to convert it to a .jpg.
Does anybody have any experience with this?
Note: I could possible convert the h5 file to a numpy array and then use an external library like pypng to convert that to a png. But I am wondering if there is a more efficient way to convert to an image, and preferrably a .jpg.
Key ingredients:
h5py to read the h5 file.
Determine the format of your image and use PIL.
Let us suppose it's RGB format (https://support.hdfgroup.org/products/java/hdfview/UsersGuide/ug06imageview.html)
Suppose your image is located at Photos/Image 1 then you can do.
import h5py
import numpy as np
from PIL import Image
hdf = h5py.File("Sample.h5",'r')
array = hdf["Photos/Image 1"][:]
img = Image.fromarray(array.astype('uint8'), 'RGB')
img.save("yourimage.thumbnail", "JPEG")
img.show()
Related
My main aim is to produce 1D array out of each image in the 'dengue' folder. For which I used below code to read the file using both PIL and GLOB.
from PIL import Image
import glob
image_list = []
for filename in glob.glob('./dengue/*.tiff'):
im=Image.open(filename)
image_list.append(im)
OUTPUT IS -
UnidentifiedImageError: cannot identify image file './dengue/image_2016-09-18.tiff
How to resolve this? The same error showed up for numerous other images.
Or is there any other way I can read each of these tiff images to produce 1D array out of them? Thank you so much for your time.
Use one of the following tools to see the difference between a TIFF file you can read and one you cannot:
exiftool UNHAPPY.TIF
or, with tiffinfo from libtiff:
tiffinfo UNHAPPY.TIF
or, with ImageMagick:
magick identify -verbose UNHAPPY.TIF
My guess would be you have an unsupported compression or pixel type.
I have a NumPy array img_array of dimension (h,w,3) of one image, which is the result of some function. I want to convert this NumPy array directly into grayscale.
Possible Solution:
Save the img_array as image using cv2.imwrite(path). Then read again with cv2.imread(path, cv2.GRAYSCALE)
However, I am looking for something like this :
def convert_array_to_grayscale_array(img_array):
do something...
return grayscare_version
I have already tried cv2.imread(img_array, CV2.GRAYSCALE), but it is throwing error of img_array must be a file pathname.
I think saving a separate image will consume more space disk. Is there any better way to do that with or without using the OpenCV library function.
scikit-image has color conversion functions: https://scikit-image.org/docs/dev/auto_examples/color_exposure/plot_rgb_to_gray.html
from skimage.color import rgb2gray
grayscale = rgb2gray(img_array)
I'm trying to compare two images, one a .png and the other a .jpg. So I need to convert the .png file to a .jpg to get closer values for SSIM. Below is the code that I've tried, but I'm getting this error:
AttributeError: 'tuple' object has no attribute 'dtype'
image2 = imread(thisPath + caption)
image2 = io.imsave("jpgtest.jpg", (76, 59))
image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
image2 = resize(image2, (76, 59))
imshow("is it a jpg", image2)
cv2.waitKey()
Before demonstrating how to convert an image from .png to .jpg format, I want to point out that you should be consistent on the library that you use. Currently, you're mixing scikit-image with opencv. It's best to choose one library and stick with it instead of reading in an image with scikit and then converting to grayscale with opencv.
To convert a .png to .jpg image using OpenCV, you can use cv2.imwrite. Note with .jpg or .jpeg format, to maintain the highest quality, you must specify the quality value from [0..100] (default value is 95). Simply do this:
import cv2
# Load .png image
image = cv2.imread('image.png')
# Save .jpg image
cv2.imwrite('image.jpg', image, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
The function skimage.io.imsave expects you to give it a filename and an array that you want to save under that filename. For example:
skimage.io.imsave("image.jpg", image)
where image is a numpy array.
You are using it incorrectly here:
image2 = io.imsave("jpgtest.jpg", (76, 59))
you are assigning the output of the imsave function to image2 and I don't think that is what you want to do.
You probably don't need to convert the image to JPG because the skimage library already handles all of this conversion by itself. You usually only load the images with imread (does not matter whether they are PNG or JPG, because they are represented in a numpy array) and then perform all the necessary computations.
Python script to convert all .png in the folder into .jpg
import cv2 as cv
import glob
import os
import re
png_file_paths = glob.glob(r"*.png")
for i, png_file_path in enumerate(png_file_paths):
jpg_file_path = png_file_path[:-3] + "jpg";
# Load .png image
image = cv.imread(png_file_path)
# Save .jpg image
cv.imwrite(jpg_file_path, image, [int(cv.IMWRITE_JPEG_QUALITY), 100])
pass
Simply use opencv's cvtColor. Assuming image read using cv2.imread(); image color channels arranged as BGR.
To convert from PNG to JPG
jpg_img = cv2.cvtColor(png_img, cv2.COLOR_RGBA2BGR)
To convert from JPG to PNG
png_img = cv2.cvtColor(jpg_img, cv2.COLOR_BGR2BGRA)
I'm trying to extract the images (and its label and such) from an RGB-D dataset called NYUV2 dataset. (I downloaded the labelled dataset)
It's a matlab file so I tried using hdf5 to read it but I don't know how to proceed from here. How do I save the images and its corresponding labels and depths into a different folder??
Here's the script that I used and its corresponding output.
import numpy as np
import h5py
f = h5py.File('nyu_depth_v2_labeled.mat','r')
k = list(f.keys())
print(k)
Output is
['#refs#', '#subsystem#', 'accelData', 'depths', 'images', 'instances', 'labels', 'names', 'namesToIds', 'rawDepthFilenames', 'rawDepths', 'rawRgbFilenames', 'sceneTypes', 'scenes']
I hope this helps.
I suppose you are using the PIL package The function fromarray expects the "mode of the image" see https://pillow.readthedocs.io/en/3.1.x/handbook/concepts.html#concept-modes
I suppose your image is in RGB. I believe the image souhld be under group 'images' and dataset image_name
Therefore
import h5py
import numpy as np
from PIL import Image
hdf = h5py.File('nyu_depth_v2_labeled.mat','r')
array = np.array(list(hdf.get("images/image_name")))
img = Image.fromarray(array.astype('uint8'), 'RGB')
img.show()
You can also look at another answer I gave to know how to save images
Images saved as HDF5 arent colored
To view the content of the h5 file, download HDFview, it will help navigate through it.
I am using xlsxwriter to insert image into excel in python code.
Now, I had image data(numpy array) after opencv process. I would like insert this image data to excel. But xlswriter only support io.BytesIO stream.
question: I don't know how to convert numpy array to io.BytesIO with jpg format.
I have trying numpy.tostring but without jpg format.
the code working well in below:
_f = open('test.jpg') # I would like to insert test.jpg
worksheet.insert_image('E2', 'abc.jpg', {'image_data': _f.read()})
Anybody can help me ? thank you so much.
Convert the numpy arrays to PIL Image structure, then use BytesIO to store the encoded image.
img_crop_pil = Image.fromarray(numpy_image)
byte_io = BytesIO()
img_crop_pil.save(byte_io, format="JPG")
jpg_buffer = byte_io.getvalue()
byte_io.close()
Use the cv2.imencode function to convert a numpy array to jpg format.