How do i save an image as a tiff in python?

How do i save an image as a tiff in python?

Problem Formulation

You create images in the Tag Image File Format (TIFF). You want to add custom metadata to the image such as the location or other context information important for post-processing. How can you accomplish this?

Solution

  • Install and import the library tiffile.
  • Use the tiffile.imsave() function to store the file at a given location.
  • As arguments, use the filename as the first positional argument, the image as the second positional argument.
  • Then add your custom metadata as a string for the keyword argument description.
  • You can now retrieve the metadata by calling the one-liner tifffile.TiffFile(filename).pages[0].tags["ImageDescription"].value.

Here’s an example that is a bit more readable:

import json
import numpy as np
import tifffile

image = np.random.randint(0, 255, size=(100, 100), dtype=np.uint8)
filename = 'your_file.tif'

# Create custom description
my_description = "I recorded this image on Mars"

# Write the file
tifffile.imsave(
    filename,
    image,
    description = my_description
)

# Read the file
frames = tifffile.TiffFile(filename)
page = frames.pages[0]

# Print file description
print(page.tags["ImageDescription"].value)

You can try this example in our interactive Jupyter Notebook in your browser to test if this is what you need:

How do i save an image as a tiff in python?

I hope you liked this short tutorial! If you want to boost your Python skills on autopilot, check out my free email academy:

We have cheat sheets! 😉

How do i save an image as a tiff in python?

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

1

New! Save questions or answers and organize your favorite content.
Learn more.

I am trying to convert a numpy array - containing the Red, Green, and Blue band obtained from the .TIFF file of the satellite image - into a simple .jpeg. The issue at hand is that the colours on the .jpeg are not like they should.

This is my code:

downloading_image = image * factor
im = Image.fromarray(downloading_image)
im.save("cloud_test_1.jpeg")

where image is a numpy array of shape (500,500,3) and factor is a float of 3.5/255

Here are the images of what I get and what I should be getting:

How do i save an image as a tiff in python?
How do i save an image as a tiff in python?

PolyGeo

64.2k27 gold badges99 silver badges314 bronze badges

asked Mar 25 at 18:49

4

Change the last part of your script as follows to take advantage of built in PIL functionality for brightening your image rather than converting the values in the array directly. I'm not sure exactly what your problem was before, but my best guess is that it was some sort of rendering issue. In any case, this should help you move forward.

## this is your original plot function
## factor 1/255 to scale between 0-1
## factor 3.5 to increase brightness
plot_image(image, factor=3.5 / 255, clip_range=(0, 1))
plt.show()


# read np array into PIL
im = Image.fromarray(image, mode="RGB")

# initialize image brightness enhancer
enhancer = ImageEnhance.Brightness(im)

#set factor
factor = 3.5  

# brightens the image by specified factor
im_output = enhancer.enhance(factor)

#plot original image
plt.imshow(im)
plt.show()

#plot brightened image
plt.imshow(im_output)
plt.show()

#write out both images
im.save("output_image.jpeg")
im_output.save('brightened-image.jpeg')

How do i save an image as a tiff in python?

answered Mar 25 at 19:57

How do i save an image as a tiff in python?

KartograafKartograaf

2,6345 silver badges23 bronze badges

6

How do I save an image as a TIFF?

Convert PDF to TIFF 2. Select File > Export To > Image > TIFF. 3. Select the desired folder and click “Save.” 4.

How do I create a TIF image?

Here's how:.
Locate the JPG file you wish to convert..
Right-click the file..
Select 'edit' or select 'open with' > 'paint'.
In the Paint image, click the 'file' menu and click 'save as'.
In the dropdown box, select TIFF..
Name the file and choose your location..

How do TIFF images work in Python?

To visualize a tiff file we need matplotlib and GDAL modules in python..
Import the module..
Count the number of bands..
Fetch all the raster bands from the tiff file..
Read the bands into NumPy arrays..
Pass the arrays into Matplotlib's imshow() to visualize..

How do I process a TIFF file in Python?

Approach:.
Step 1: Import the module..
Step 2: We can count how many bands..
Step 3: Find every raster band in the TIFF file..
Step 4: The bands are read to NumPy arrays..
Step 5: In the imshow() function of Matplotlib to display..