07.07.2023 Views

Implementing-cryptography-using-python

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6 ■ Using Cryptography with Images 195

Working with large images

While we are on the topic of images and the ability to hide data inside them,

I want to introduce you to the FITS image type. FITS stand for Flexible Image

Transport System; it is an open standard that defines a digital file format that

is used for the transmission, processing, and storage of data within a single

file. FITS is typically used for digital files produced in astronomical research

or satellite data. The amount of data inside the file can be astronomical, pun

intended. The data inside the file can be represented in several dimensional

formats. The important aspect to understand about how this format works is

that there is human-readable text stored in the header portion of the file format.

The header details may list any parameters that could tell where the image was

taken, which satellite or telescope produced it, and any number of pieces of

metadata that the author wants to include; the only limit to the header metadata

is that the label must be eight characters or less. A FITS file consists of one or

more Header + Data Units; these are known as HDUs. The first HDU that is

created is called the primary HDU or primary array. The primary HDU can

be empty or contain an N-dimensional array of pixels such as a 1-D spectrum,

a 2-D image, or a 3-D data cube. The best way to get an understanding of the

FITS standard is to dive in with some Python code; in order to follow along

with the following code, you will need to include the astropy library. Another

library that you can use is PyFITS. The .io FITS module is identical to PyFITS

except by name. PyFITS was ported into astropy. You can do this by typing pip

install astropy. Let's create a FITS file that stores some random numpy array in

a file named random_array.fits. The output of the file will give you the default

header information along with the contents of the data.

import numpy as np

from astropy.io import fits

file_name = "chapter6/fits/random_array.fits"

hdu = fits.PrimaryHDU()

hdu.data = np.random.random((128,128))

# Note that setting the data automatically populates the header with

basic information:

hdu.writeto(file_name, overwrite=True)

data = fits.getdata(file_name)

header = fits.getheader(file_name)

print(header)

print()

print(data)

Now that you understand the components that make up a FITS file, we can

explore FITS for images where you might want to keep some of the header

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!