Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect image data and type after TIFF compression #5843

Closed
XrioBtw opened this issue Nov 19, 2021 · 2 comments · Fixed by #5848
Closed

Incorrect image data and type after TIFF compression #5843

XrioBtw opened this issue Nov 19, 2021 · 2 comments · Fixed by #5848
Labels

Comments

@XrioBtw
Copy link

XrioBtw commented Nov 19, 2021

When saving an image containing float data as a compressed TIFF the output image data and number type is incorrect (here the float32 becomes int32). The output is correct if I specify compression = None or if the input image data is of e.g. type uint8. If I specify compression = "something wrong" I get no error message, the compression defaults to "compression": "raw", but the output image data and type is still wrong.

import numpy as np
from PIL import Image
from tempfile import TemporaryFile

with TemporaryFile() as fp:
    Z = np.array([[np.pi, np.pi], [np.pi, np.pi]], dtype = np.float32)
    img = Image.fromarray(Z)
    img.save(fp, format = "TIFF", compression = "packbits")
    img = Image.open(fp)
    print(img.info)   # {'compression': 'packbits', 'dpi': (1, 1), 'resolution': (1, 1)}
    Z = np.asarray(img)
    print(Z)   # [[1078530011, 1078530011], [1078530011, 1078530011]]
    print(Z.dtype)   # int32
@radarhere radarhere added the TIFF label Nov 19, 2021
@radarhere
Copy link
Member

Just a note - it is libtiff that is converting an F mode image to I mode. When libtiff is not used, it stays in F mode.

Here is a demonstration without NumPy.

from PIL import Image, TiffImagePlugin

for libtiff in (True, False):
    TiffImagePlugin.WRITE_LIBTIFF = libtiff
    img = Image.new("F", (1, 1), 0.5)
    path = ("libtiff" if libtiff else "without_libtiff")+".tif"
    img.save(path)
    with Image.open(path) as reloaded:
        print(libtiff, reloaded.mode)

gives

True I
False F

@radarhere
Copy link
Member

I've created PR #5848 to resolve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants