Repository: python-pillow/Pillow

Issue:
PIL.UnidentifiedImageError: cannot identify image file (Custom installation in other directories)
<!--
Thank you for reporting an issue.

Follow these guidelines to ensure your issue is handled properly.

If you have a ...

1.  General question: consider asking the question on Stack Overflow
    with the python-imaging-library tag:

    * https://stackoverflow.com/questions/tagged/python-imaging-library

    Do not ask a question in both places.

    If you think you have found a bug or have an unexplained exception
    then file a bug report here.

2.  Bug report: include a self-contained, copy-pastable example that
    generates the issue if possible. Be concise with code posted.
    Guidelines on how to provide a good bug report:

    * https://stackoverflow.com/help/mcve

    Bug reports which follow these guidelines are easier to diagnose,
    and are often handled much more quickly.

3.  Feature request: do a quick search of existing issues
    to make sure this has not been asked before.

We know asking good questions takes effort, and we appreciate your time.
Thank you.
-->

### What did you do?

```bash
pip install -h |grep target
#  -t, --target <dir>        Install packages into <dir>. By default this
#                            multiple platforms supported by the target
#                            abis supported by the target interpreter.

mkdir -p packages
# remove the Pillow, which installed into system packages
pip uninstall Pillow
pip install -t packages Pillow
```

### What did you expect to happen?

It imports plugins correctly 

### What actually happened?

It can't import plugins, when I didn't install Pillow into system packages directories

### What are your OS, Python and Pillow versions?

* OS:  Debian
* Python: 3.8.0
* Pillow: All versions

<!--
Please include **code** that reproduces the issue and whenever possible, an **image** that demonstrates the issue. Please upload images to GitHub, not to third-party file hosting sites. If necessary, add the image to a zip or tar archive.

The best reproductions are self-contained scripts with minimal dependencies. If you are using a framework such as Plone, Django, or Buildout, try to replicate the issue just using Pillow.
-->

The real format of the image is webp, you need to import PIL.WebPImagePlugin. The image is as follows:

![3ae6c099102fba7a4773c3568edee0be](https://github.com/python-pillow/Pillow/assets/4394982/c108e027-c9d4-4943-a3d6-143053277c74)


```python
from packages.PIL import Image

with open('3ae6c099102fba7a4773c3568edee0be.jpg', 'rb') as fr:
    Image.open(fr)
```

```pytb
Traceback (most recent call last):
  File "test2.py", line 4, in <module>
    Image.open(fr)
  File "/app/packages/PIL/Image.py", line 3023, in open
    raise UnidentifiedImageError(
packages.PIL.UnidentifiedImageError: cannot identify image file <_io.BufferedReader name='3ae6c099102fba7a4773c3568edee0be.jpg'>
```



Discussion:

Resolving patch:
diff --git a/src/PIL/Image.py b/src/PIL/Image.py
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -355,10 +355,11 @@ def init():
     if _initialized >= 2:
         return 0
 
+    parent_name = __name__.rpartition(".")[0]
     for plugin in _plugins:
         try:
             logger.debug("Importing %s", plugin)
-            __import__(f"PIL.{plugin}", globals(), locals(), [])
+            __import__(f"{parent_name}.{plugin}", globals(), locals(), [])
         except ImportError as e:
             logger.debug("Image: failed to import %s: %s", plugin, e)
 


Test patch: