1

I need to stitch 2 images together in Python. I used this solution (code below), and it stitches them fine, but I ran into a problem: My source TIFF images are ~100 KB each, but my output image is ~150 MB. Why is it happening, and how can I fix it? Thanks.

from PIL import Image

def merge_images(file1, file2):
    """Merge two images into one, displayed side by side
    :param file1: path to first image file
    :param file2: path to second image file
    :return: the merged Image object
    """
    image1 = Image.open(file1)
    image2 = Image.open(file2)

    (width1, height1) = image1.size
    (width2, height2) = image2.size

    result_width = width1 + width2
    result_height = max(height1, height2)

    result = Image.new('RGB', (result_width, result_height))
    result.paste(im=image1, box=(0, 0))
    result.paste(im=image2, box=(width1, 0))
    return result
InfiniteLoop
  • 387
  • 1
  • 3
  • 18
  • Possibly the input images were compressed, but you're saving the result in an uncompressed format. What are the actual dimensions of the images? – jasonharper Mar 11 '19 at 03:13

1 Answers1

1

Calling Image.save() without supplying additional arguments will save the image in TIFF format without using compression.

Have a look under Saving TIFF Images in the Pillow documentation for the different compression options available

https://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html?highlight=writer

From Saving TIFF Images

compression A string containing the desired compression method for the file. (valid only with libtiff installed) Valid compression methods are: None, "tiff_ccitt", "group3", "group4", "tiff_jpeg", "tiff_adobe_deflate", "tiff_thunderscan", "tiff_deflate", "tiff_sgilog", "tiff_sgilog24", "tiff_raw_16"

For example you could save the resultant image with:

result.save('result.tiff', compression="tiff_deflate")
R.Grew
  • 26
  • 3