How to read RGB image (JPG) without additional module in python - python

I am trying to read image.jpg (RGB) into an array in python without any additional module but it doesn't work?
pic = open('image.jpg')
array=[]
with open(p, 'rb') as inf:
jpgdata = inf.read()
values=jpgdata.split()
array=array.append(values[:][:])
print (array)
Can anyone help me how to read an image 3 bands (RGB) in python without using external module?

A JPEG image is not just a series of pixels, unlike some other formats like BMP.
In order to get the pixel data from a JPEG image you need to decompress it, which involves reading its header data, then rebuilding the data from 8x8px blocks which contain information regarding the brightness and color (YCbCr).
You need to:
Build the Huffman tree and revert the blocks
Invert the discrete cosine transform with the given parameters
Revert the YCbCr into RGB
Place each block into its corresponding location in the image
Building a simple decoder from scratch is certainly possible, but it's not going to be done in a few lines.

Related

How to get true raw data input from webcam? pref with python

Interested in getting the raw data that is streamed from a webcam..before it is transformed into binary -> pixels or whatever the sequence is.
From my understanding, libraries like opencv won't help with this.
Edit: kiss
in order to see the image output from a webcam using python you need to convert it from raw image data (the pixels) into a image that can be displayed one way you can do it is by using matplotlib more info here https://matplotlib.org/stable/tutorials/introductory/images.html
this will display the image but will not convert it into other image formats such as .png .jpeg ext. but the data you are getting is probably pixel data which is the rawest image data you will get

Loading and saving raw images

I'm looking to be able to read in pixel values as captured in a raw NEF image, process the data for noise removal, and then save the new values back into the raw image format maintaining all the metadata for later use. I've seen dcraw can read in raw format and output the Bayer pattern data as a tiff or other image but I can't save it back to my NEF. I've also been attempting to read in and save the image with simple python file open or numpy memmap but have no clue how to handle the binary data. Any help would be appreciated. Thanks!

Extract tiles from tiled TIFF and store in numpy array

My overall goal is to crop several regions from an input mirax (.mrxs) slide image to JPEG output files.
Here is what one of these images looks like:
Note that the darker grey area is part of the image, and the regions I ultimately wish to extract in JPEG format are the 3 black square regions.
Now, for the specifics:
I'm able to extract the color channels from the mirax image into 3 separate TIFF files using vips on the command line:
vips extract_band INPUT.mrxs OUTPUT.tiff[tile,compression=jpeg] C --n 1
Where C corresponds to the channel number (0-2), and each output file is about 250 MB in size.
The next job is to somehow recognize and extract the regions of interest from the images, so I turned to several python imaging libraries, and this is where I encountered difficulties.
When I try to load any of the TIFFs using OpenCV using:
i = cv2.imread('/home/user/input_img.tiff',cv2.IMREAD_ANYDEPTH)
I get an error error: (-211) The total matrix size does not fit to "size_t" type in function setSize
I managed to get a little more traction with Pillow, by doing:
from PIL import Image
tiff = Image.open('/home/user/input_img.tiff')
print len(tiff.tile)
print tiff.tile[0]
print tiff.info
which outputs:
636633
('jpeg', (0, 0, 128, 128), 8, ('L', ''))
{'compression': 'jpeg', 'dpi': (25.4, 25.4)}
However, beyond loading the image, I can't seem to perform any useful operations; for example doing tiff.tostring() results in a MemoryError (I do this in an attempt to convert the PIL object to a numpy array) I'm not sure this operation is even valid given the existence of tiles.
From my limited understanding, these TIFFs store the image data in 'tiles' (of which the above image contains 636633) in a JPEG-compressed format.
It's not clear to me, however, how would one would extract these tiles for use as regular JPEG images, or even whether the sequence of steps in the above process I outlined is a potentially useful way of accomplishing the overall goal of extracting the ROIs from the mirax image.
If I'm on the right track, then some guidance would be appreciated, or, if there's another way to accomplish my goal using vips/openslide without python I would be interested in hearing ideas. Additionally, more information about how I could deal with or understand the TIFF files I described would also be helpful.
The ideal situations would include:
1) Some kind of autocropping feature in vips/openslide which can generate JPEGs from either the TIFFs or original mirax image, along the lines of what the following command does, but without generated tens of thousands of images:
vips dzsave CMU-1.mrxs[autocrop] pyramid
2) Being able to extract tiles from the TIFFs and store the data corresponding to the image region as a numpy array in order to detect the 3 ROIs using OpenCV or another methd.
I would use the vips Python binding, it's very like PIL but can handle these huge images. Try something like:
from gi.repository import Vips
slide = Vips.Image.new_from_file(sys.argv[1])
tile = slide.extract_area(left, top, width, height)
tile.write_to_file(sys.argv[2])
You can also extract areas on the command-line, of course:
$ vips extract_area INPUT.mrxs OUTPUT.tiff left top width height
Though that will be a little slower than a loop in Python. You can use crop as a synonym for extract_area.
openslide attaches a lot of metadata to the image describing the layout and position of the various subimages. Try:
$ vipsheader -a myslide.mrxs
And have a look through the output. You might be able to calculate the position of your subimages from that. I would also ask on the openslide mailing list, they are very expert and very helpful.
One more thing you could try: get a low-res overview, corner-detect on that, then extract the tiles from the high-res image. To get a low-res version of your slide, try:
$ vips copy myslide.mrxs[level=7] overview.tif
Level 7 is downsampled by 2 ** 7, so 128x.

Using gdcm (Grassroots DICOM) to Decompress DICOM Image Data

Is it possible to use the Python wrappers for GDCM to decode the image data in a DICOM file?
I have a byte array / string with the bytes of the image data in a DICOM file (i.e. the contents of tag 7fe0,0010 ("Pixel Data")) and I want to decode the image to something raw RGB or greyscale.
I am thinking of something along the lines of this but working with just the image data and not a path to the actual DICOM file itself.
You can read the examples, there is one that shows how one can take an input compressed DICOM and convert it to an uncompressed one. See the code online here:
Decompress Image
If you are a big fan of NumPy, checkout:
This module add support for converting a gdcm.Image to a numpy array.
This is sort of a low level example, which shows how to retrieve that actual raw buffer of the image.
A much nicer class for handling Transfer Syntax conversion would be to use gdcm.ImageChangeTransferSyntax class (allow decompression of icon)
gdcm::ImageChangeTransferSyntax Class Reference
If you do not mind reading a little C++, you can trivially convert the following code from C++ to Python:
Compress Image
Pay attention that this example is actually compressing the image rather than decompressing it.
Finally if you really only have access to the data values contains in the Pixel Data attribute, then you should really have a look at (C# syntax should be close to Python syntax):
Decompress JPEG File
This example shows how one can decompress a JPEG Lossless, Nonhierarchical, First- Order Prediction file. The JPEG data is read from file but it should work if the data is already in memory for your case.

Create image file from array

Essentially my problem is just finding an easy way to create an image file from an array.
My problem is unparsing CUPS raster files into images. The CUPS RGB raster file header is 1800 bytes. If I input the width and height I can read the raster array contained in the file correctly into Photoshop in Mac order, with interleaved 16 bit data 00RRGGBB. I have written a utility which extracts the width and height from the header.
I'd like to write another command-line utility which takes the width, height and file-name as inputs, truncates the first 1800 bytes off the raster file, and creates a Tiff or BMP or whatever is easiest to write image with the array that is contained in the rest - any well-known image format will do.
program should be C or Python, run under Mac, Linux.
For Python, PIL is the tool for this task. Use the putdata() (search the link for putdata) method on image objects to put the pixels from a list into an image.
You can try GDAL,which supports many image formats.You can use RasterIO(...) method for reading image data.

Categories