Uploading a JPEG image via Django displays error - django

I'm running a Django app inside a virtualenv on Ubuntu with NGINX/FastCGI and trying to upload a JPG image to the Django admin app.
I'm getting this error: "Upload a valid image. The file you uploaded was either not an image or a corrupted image." I can upload a GIF image, so it's only JPEG that's causing the problem.
Here's what I've tried so far:
1) Install libjpeg62-dev before PIL
pip uninstall pil
sudo apt-get libjpeg62-dev
pip install pil
At the end of the PIL install, I see:
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version 1.1.7
platform linux2 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3]
--------------------------------------------------------------------
*** TKINTER support not available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- FREETYPE2 support available
*** LITTLECMS support not available
So it looks like JPEG support is there. I've also done the manual test to verify and it verified:
>>> from PIL import Image
>>> trial_image=Image.open("/path/to/my/image.png")
>>> trial_image.verify()
2) Similar to approach #1 above, but manually specifying the JPG_ROOT:
pip uninstall pil
sudo apt-get libjpeg62-dev
pip install --no-install pil
vi /path/to/virtualenv/build/PIL/setup.py (JPG_ROOT = libinclude("/usr/lib"))
pip install pil
3) Try my luck with Pillow (a PIL fork)
pip uninstall pil
sudo apt-get libjpeg62-dev
pip install pillow
I'm running Django under I've also trying stopping & starting the nginx service after each time installing PIL.
So far none of the above approaches have gotten me past the above error message in my Django app on Ubuntu. I can upload the same JPG image just fine on my local dev box (Mac OSX 10.6.5) running the same Django app, so I know it's something with my deployment on Ubuntu.
Any help would be appreciated!

Well what do you know, I decided to log out of the Django admin and log back in....suddenly it works fine! Sheesh. The last thing I tried was approach #2 above so if anyone is having this problem, that's your best solution.

You were close. But the library required was different.
The answer, helped me, is here https://stackoverflow.com/a/23445746/1478569

Related

Conda dependent packages(rdkit) will not install properly for use in web servers(importError:DLL load failed). Is there an alternative install method?

UPDATE 1: PIL and RDKIT produced import errors because the .dll files were not placed in /venv//Lib/site-packages
I removed both packages from environment and commented out their code from my project.
Next I updated the base base environment:
conda update conda #from base env
Then I upgraded pip and setuptools and reinstalled pillow:
python -m pip install pip --upgrade
pip install setuptools --upgrade
pip install pillow
I can now add my PIL content back to the project and it will work just fine on the production server, without rdkit installed.
RDKIT is only available as a conda install, it still does not install properly and breaks PIL in the process(rdkit depends on pillow). I have reinstalled from both the rdkit and conda-forge channels. Neither work.
What is the proper way to fix this problem? I feel like the hack around of moving .dll files is risky and not a good solution.
UPDATE 0: RDKIT will not load into any server
I created a basic django project similar to what you would find in the tutorial with no static files. The project successfully published to the Apache server describe below in the original post and in Windows 10 IIS. Next modified the models.py to import rdkit and both servers gave the import error and the site would not display.
Original post:
RDKIT is the only package not loading into our production server.
We successfully set up a Wampserver to run Apache and host our Django project . . .but have to comment out all the code associated with rdkit for it to work. This inhibits many required features.
There were no issues using rdkit in Django's test server.
Primary wsgi error:
from .rdBase import rdkitVersion as __version__\r, referer: http://localhost/APP/
ImportError: DLL load failed: The specified module could not be found.\r, referer: http://localhost/APP/
Relevant packages and version details:
Windows 10
Django 2.2
Python 3.7
conda 4.8.2
rdkit 2019.09.3 conda-forge
Wampserver 3.2
Apache 2.4.41
mod-wsgi 4.7.1
postgresql 10
The methods in this blog describe how we setup the production server. With one minor change, we did not alter the httpd_vhosts.conf file and only setup the standard localhost.
The error is reproducible by trying to import rdkit(or one of its methods) into any file needed to host a web application in the described environment.
We found this 2016 thread on the rdkit sourceforge and it sounds like someone else was having a similar problem on a Linux system. This is our first time setting up a server and we have not been successful at translating the recommended fix from linux to windows. There was no follow up to know if the recommendation was attempted let alone successful.
These 2012 slides lend us to believe someone was trying to overcome a similar issue in linux (slide 9)
How would a package need to be altered to allow it to load into a webserver?
Thank you, we greatly appreciate your time and assistance.

Cannot install pillow on Windows

I keep getting errors when I try to run pillow on x64 Windows
I have followed these instructions for installing pillow
I seem to able to
import PIL
but it fails when I try
from PIL import image
See screen shot
Can someone please help?
I've tried installing the 32 bit version, but that fails and trying to uninstall pillow also fails
Solved
All I needed to do was
pip install pillow
That simple

Pillow installation not recognized in Django through Apache

When deploying my project through Apache wsgi, using a virtualenv on Ubuntu Server VM, I get the following on error on /var/log/apache2/error.log:
ImportError: Photologue was unable to import Python Imaging Library. Please confirm it's installed and available on your current Python path.
Setup:
Django==1.6.6
django-photologue==2.3
Pillow==2.5.1
However, I have been able to run the project with the same configuration in Ubuntu Desktop VM (for development). For Ubuntu Server VM I followed the same procedure:
Installed dependent image libraries (libjpeg8-dev, etc) through apt-get
pip install pillow, in virtualenv, it showed --- JPEG support available
Ran this with no problems:
manage.py shell
from PIL import Image
im = Image.open("file.jpg")
im.show()
Used django's server with no problem, it showed the login screen:
manage.py runserver
For sanity's sake, I uninstalled pillow, running manage.py runserver showed the same import error. I reinstalled pillow and the error went away in the django server
Now, the issues arise when I try to access the website on the browser through apache. I go to the servername to get the actual django website and keep getting a 500 Internal Server Error on the browser and the import error on apache log.
The only thing I can think of is that in manage.py shell I can't do "import Image". However, photologue has a try/except import in which it will do "from PIL import Image" in case of an exception with "import Image", which covers that case.
I found the answer, posting it here since it was not entirely obvious.
This is a more subtle case of PIL not being able to link against the directories in apt-get. The answer from the ubuntu forums solves this issue.
sudo ln -s /lib/x86_64-linux-gnu/libz.so.1 /lib/
sudo ln - /usr/lib/x86_64-linux-gnu/libfreetype.so.6 /usr/lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so.62 /usr/lib/
However, I was not able to run the django proyect with pillow. I had to use PIL, which is annoying:
pip uninstall pillow
pip install PIL --allow-external PIL --allow-unverified PIL

Django: determine actually used pip packages

I got a Django project that was started without virtualenv. Now migrating to virtualenv and my requirements.txt created before is huge and not installable in the virtualenv (many errors as shown below). How can i generate a minimal list of required packages? Or is there some reference anywhere?
Could not find any downloads that satisfy the requirement PIL==1.1.7 (from -r requirements.txt (line 8))
Some externally hosted files were ignored (use --allow-external PIL to allow).
Cleaning up...
No distributions at all found for PIL==1.1.7 (from -r requirements.txt (line 8))
it's not only PIL that throws errors, if I comment it another package shows and i can't know what's really used for my application to work.
Thanks in advance!
You can run pip freeze (related to system python used before virtualenv), this give you list of installed packages;
Then filter that list using following:
1) INSTALLED_APPS in settings
2) also check all from and import statement (search through the project)
I've had the same problem and there are issues getting PIL to install using PIP as there's no PIL 1.7 in the default Python repos.
The easiest fix is this to add these options to the pip:
--allow-external PIL --allow-unverified PIL
For example:
pip install -r requirements.txt --allow-external PIL --allow-unverified PIL
The problem with this it is a potential security issue and you don't want to do this on a production server! :)
Your options are to use Pillow which is a fork of PIL:
https://pypi.python.org/pypi/Pillow
Comments from the Pillow author, and you should verify that it works with you code.
Or try PIL 1.1.6 which is the Python Repos:
https://pypi.python.org/pypi/PIL
Or create your own repo and include the PIL 1.1.7 sources.
Or, if your on a Linux system install PIL using your distro's package management tool and remove PIL from your requirements file, and then rebuild your virutalenv.
You can this on Debian based distros like this:
sudo apt-get install python-imaging
Red Hat distros like this:
sudo yum install python-imaging

IOError decoder zip not available

I'm trying to get up and running with sorl thumbnail but I'm getting an error "decoder zip not available".
I have read through a ton of similar pages saying that it is a PIL / Pilow issue.
I have tried re-installing pil via:
easy_install http://dist.plone.org/thirdparty/PIL-1.1.7.tar.gz
As well as:
pip uninstall PIL Pillow
pip install Pillow
pip install PIL
I read that Ubuntu uses a directory called /lib/x86_64-linux-gnu for x64 architectures. If you are using that architecture you need to create a symbolic link for that as well as for other shared libraries.
$ sudo ln -s /lib/x86_64-linux-gnu/libz.so.1 /lib/libz.so
Additional info:
OS: Ubuntu Precice
I have not tried installing the package zlib from source which some others have also mentioned worked for them.
I found two solutions for this that worked for me.
The first way that worked for me was to use a different imaging library all together. I installed pgmagic and that worked fine.
sudo apt-get install libgraphicsmagick++-dev
sudo apt-get install libboost-python1.40-dev
There is also other imaging libraries available and they are all listed in the sorl-thumbnail docs:
http://sorl-thumbnail.readthedocs.org/en/latest/requirements.html
The second way that worked for me (preferred) was literally a fresh install of everything where I removed both pillow and PIL and the re-installed them starting with pillow.
pip uninstall pillow
pip uninstall PIL
pip install pillow
pip install PIL