I want to extract the least significant bit (LSB) of each pixel in a very big image (19,566x14,321) that is in TIFF format.
An example of such an image is here: Carina Nebula This is in JPG format, but the NASA site has the TIFF.
Is there an easy way to do this in C/C++ ImageMagick or open source library?
Many thanks for any suggestions and help!
Not sure what speed you expect but it could be as simple as this with Pillow:
from PIL import Image
import numpy as np
Image.MAX_IMAGE_PIXELS = 933120000
# Load image as PIL Image and make into Numpy array
im = Image.open('eta.jpg')
na = np.array(im)
# Mask LSBs and ensure small storage allocation of np.uint8
LSBs = (na & 1).astype(np.uint8)
LSBs.tofile('filename.bin')
Note that you could compress the data before writing. Here's the code for gzip
but you can change the word gzip
to lzma
or bz2
to try other methods:
# Generate some random data
LSBs = np.random.randint(2, size=(20_000, 14_000, 3), dtype=np.uint8).tobytes()
import gzip
compressed = gzip.compress(LSBs)
from pathlib import Path
Path('compressed.bin').write_bytes(compressed)
If you want to save the LSBs as a PNG, start with my original code and add this to the end:
# Mask LSBs and ensure small storage allocation of np.uint8
LSBs = (na & 1).astype(np.uint8)
# Make LSBs into PIL Image and save
pi = Image.fromarray(LSBs)
pi.save('LSBs.png')
You can also try replacing the final line with:
pi.save('LSBs.png', optimize=True)