Repository: python-pillow/Pillow

Issue:
'TiffImageFile' object has no attribute 'use_load_libtiff'
Hello,

When trying to save a TiffImage to BytesIO() object I get 'TiffImageFile' object has no attribute 'use_load_libtiff' error.

```
    image.save(buffer, format=image_format)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1960, in save
    self.load()
  File "/usr/lib/python3/dist-packages/PIL/TiffImagePlugin.py", line 1078, in load
    if self.use_load_libtiff:
AttributeError: 'TiffImageFile' object has no attribute 'use_load_libtiff'
```

* OS: Debian Stretch / MacOS something (both fails)
* Python: 3.5 or 3.6
* Pillow: 5.3.0 or 5.4.1

Changing Pillow TiffImagePlugin.py file like this, workarounds the issue

```python
    def load(self):
        #if self.use_load_libtiff:
        #    return self._load_libtiff()
        return super(TiffImageFile, self).load()
```

But that's obviously not suitable, in the meanwhile I dirty-patched my code like this

```python
            if isinstance(image, TiffImagePlugin.TiffImageFile):
                if not hasattr(image, "use_load_libtiff"):
                    image.use_load_libtiff = True
```

But this is still a crappy workaround. I really think there's something wrong in Pillow, load() method can be called on TiffImageFile instances without having use_load_libtiff attribute being set.

Thanks in advance,


Discussion:
The following code works for me with Pillow 5.4.1 -

```python
from PIL import Image
import io
fp = io.BytesIO()
im = Image.open("Tests/images/hopper.tif")
im.save(fp, 'TIFF')

# Just to demonstrate that the contents are a correct TIFF
with open('check_contents.tiff', 'wb') as fp2:
	fp2.write(fp.getvalue())
```

So could you provide a self-contained code sample that demonstrate the problem?
Feel free to comment, and this can be re-opened.
I can trigger the issue when trying to Pickle a TIFF Image (needed to save temporary session user data along with other data). The relevant stack trace:

```
6]:   File "/.venv/lib/python3.7/site-packages/PIL/Image.py", line 707, in __getstate__
6]:     self.getpalette(),
6]:   File "/.venv/lib/python3.7/site-packages/PIL/Image.py", line 1324, in getpalette
6]:     self.load()
6]:   File "/.venv/lib/python3.7/site-packages/PIL/TiffImagePlugin.py", line 1076, in load
6]:     if self.use_load_libtiff:
6]: AttributeError: 'TiffImageFile' object has no attribute 'use_load_libtiff'
```
I fixed it by changing TiffImagePlugin.py:

```python
    def load(self):
        if getattr(self, 'use_load_libtiff', False):
            return self._load_libtiff()
        return super(TiffImageFile, self).load()
```
@mattpepin could you provide a self-contained code example to demonstrate the problem? So a simple block of code picking the TIFF? Also, what version of Pillow are you using?
> @mattpepin could you provide a self-contained code example to demonstrate the problem? So a simple block of code picking the TIFF? Also, what version of Pillow are you using?

I'll try to generate a small sample later this afternoon with latest Pillow. Our production server runs version 6.0.
Got it:

```python
import pickle
from PIL import Image

with open('/tmp/image.tif', 'rb') as f:
    img = Image.open(f)
    s = pickle.dumps(img)
    img2 = pickle.loads(s)
    img2.load()
```
I have same problems. Object TiffimageFile has not attributes `_n_frames` and  `_is_animated`  after using asyncio function `run_in_executor` (this function use pickle for execution in sub processes). And method numpy.array(image) is broken
@matemax while you may very well have the same root cause to your problem, could we have a self-contained code example, just so we will know when we have resolved your issue?
@radarhere [example](https://github.com/python-pillow/Pillow/issues/3690#issuecomment-566215322) of @mattpepin reproduces the problem.
I reproduced the problem.
I use pickle to serialize Pillow's Image object and when use pickle.loads functions decode the object, the Image object not work correct and arise the error as title is.
But I find something special the tiff file (the image I used) has empty image extend information, no exif no device information...

other informations:
```
Pillow version: 6.0.0
Python version: 3.7.2
platform: win10
```

Resolving patch:
diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py
--- a/src/PIL/TiffImagePlugin.py
+++ b/src/PIL/TiffImagePlugin.py
@@ -1066,7 +1066,7 @@ def tell(self):
         return self.__frame
 
     def load(self):
-        if self.use_load_libtiff:
+        if self.tile and self.use_load_libtiff:
             return self._load_libtiff()
         return super().load()
 
@@ -1094,12 +1094,7 @@ def _load_libtiff(self):
         """ Overload method triggered when we detect a compressed tiff
             Calls out to libtiff """
 
-        if self.tile is None:
-            raise OSError("cannot load this image")
-
-        pixel = Image.Image.load(self)
-        if not self.tile:
-            return pixel
+        Image.Image.load(self)
 
         self.load_prepare()
 


Test patch:
diff --git a/Tests/test_pickle.py b/Tests/test_pickle.py
--- a/Tests/test_pickle.py
+++ b/Tests/test_pickle.py
@@ -49,6 +49,7 @@ def helper_pickle_string(
         pytest.param(
             "Tests/images/hopper.webp", None, marks=skip_unless_feature("webp")
         ),
+        ("Tests/images/hopper.tif", None),
         ("Tests/images/test-card.png", None),
         ("Tests/images/zero_bb.png", None),
         ("Tests/images/zero_bb_scale2.png", None),