sorryMike
sorryMike

Reputation: 789

Resize tiff image in Python

Tried resize my image (weight > 100 MB) using:

>>> from PIL import Image
>>> image = Image.open(path_to_the_file)
>>> new_image = image.resize((200, 200))

and received ValueError: tile cannot extend outside image.

The size of original image is

>>> image.size
>>> (4922, 3707)

The same error I received while doing thumbnails, rotate etc.

What I am doing wrong?

Edit: Checked image using ImageMagic:

$ identify file.tif
file.tif[0] TIFF 4922x3707 4922x3707+0+0 32-bit Grayscale Gray 31.23MB   0.000u 0:00.009
file.tif[1] TIFF 2461x1854 2461x1854+0+0 32-bit Grayscale Gray 31.23MB 0.000u 0:00.000
filetif[2] TIFF 1231x927 1231x927+0+0 32-bit Grayscale Gray 31.23MB 0.000u 0:00.000
file.tif[3] TIFF 616x464 616x464+0+0 32-bit Grayscale Gray 31.23MB 0.000u 0:00.000
file.tif[4] TIFF 308x232 308x232+0+0 32-bit Grayscale Gray 31.23MB 0.000u 0:00.000
file.tif[5] TIFF 154x116 154x116+0+0 32-bit Grayscale Gray 31.23MB 0.000u 0:00.000
identify: Unknown field with tag 33550 (0x830e) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/881.
identify: Unknown field with tag 33922 (0x8482) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/881.
identify: Unknown field with tag 34735 (0x87af) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/881.
identify: Unknown field with tag 34736 (0x87b0) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/881.

Upvotes: 2

Views: 9014

Answers (4)

Dianti Farhana
Dianti Farhana

Reputation: 11

I was using ggdalwarp like this :

gdal.Warp('output.tiff', 'input.tiff', xRes=0.5, yRes=0.5)

Upvotes: 1

F. Leone
F. Leone

Reputation: 595

The problem might be here, from the docs:

Note that the bilinear and bicubic filters in the current version of PIL are not well-suited for large downsampling ratios (e.g. when creating thumbnails). You should use ANTIALIAS unless speed is much more important than quality.

In this case, add at your code Image.ANTIALIAS

from PIL import Image
image = Image.open(path_to_the_file)
new_image = image.resize((200, 200) Image.ANTIALIAS)

Should now do the trick.

Upvotes: 1

GaryMBloom
GaryMBloom

Reputation: 5699

Try adding the Image.ANTIALIAS parameter:

new_image = image.resize((200, 200), Image.ANTIALIAS)

This worked for me in one of my previous projects.

Upvotes: 0

Richard Rublev
Richard Rublev

Reputation: 8170

You should install pytho-resize-image package.

One example

from PIL import Image

from resizeimage import resizeimage


with open('acajeb-image.jpeg', 'r+b') as f:
    with Image.open(f) as image:
        cover = resizeimage.resize_cover(image, [200, 200])
        cover.save('test-image-cover.jpeg', image.format)

You can install package with

pip install python-resize-image

Upvotes: 0

Related Questions