How to generate square thumbnail using perl

I have a large amount of pictures needed to be thumbnailed. Preferable using either perl or python.  The thumbnails created should be square without distorting the pictures.
Anyone care to share your experience?  I am using  Windows 10, python 3.5
Thanks
cpeters5Asked:
Who is Participating?
 
gelonidaConnect With a Mentor Commented:
You have to install PILLOW and not PIL.
PIL is no more maintained. Pillow is a fork of Pillow, that is still being maintained and that also works for python 3.
you have to install pillow, (but in the python code you import PIL)

so try pip install pillow
Normally pip should automatically find the correct version, sometimes it fails though. you could try then

pip install pillow==3.3.0

and if this still fails try to download the executable installer directly from the pypi page




In your first post you said,  you use python 3.5 and your second reply you mention python 3.3
It doesn't matter you should find installers for both.
Do you have the 32 bit version or the 64 bit version of python?
You have to match version and bit width if you download / install manually.

So try one of the following installers: (taken from the link of my initial post)
Pillow-3.3.0.win32-py3.3.exe
Pillow-3.3.0.win32-py3.5.exe
Pillow-3.3.0.win-amd64-py3.3.exe
Pillow-3.3.0.win-amd64-py3.5.exe


Please tell me whether you managed to install with pip or with the binary installer or not at all.

If you still fail, then please tell me the exact error message
0
 
gelonidaCommented:
one of Python's  "standard" libraries for image manipulation is Pillow, which is based on PIL

https://pypi.python.org/pypi/Pillow/3.3.0

One of the first examples in PIL(low)'s documentation about the Image object is the creation of thumbnails.

https://pillow.readthedocs.io/en/3.3.x/https://pillow.readthedocs.io/en/3.3.x/


can yoy try whether that yields satisfying results for you?

from PIL import Image
import glob, os

size = 128, 128

for infile in glob.glob("*.jpg"):
    file, ext = os.path.splitext(infile)
    im = Image.open(infile)
    im.thumbnail(size)
    im.save(file + ".thumbnail", "JPEG")

Open in new window

0
 
cpeters5Author Commented:
I use python 3.3.  Apparently,  pip cant find a compatible version of PIL. Also tried python 3.5 with no luck.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
cpeters5Author Commented:
Thank yo gelonida. Managed to install pillow.  But your code produced ImportError: No module name PIL
0
 
cpeters5Author Commented:
I tried the code again from command line and thumbnails were produced without errors (they were not square though.)  When run the code from file, I got ImportError again.  The path were set to c:\python3.3\, c:\python33\scripts and c:\python33\lib\site-packages\django\bin
0
 
gelonidaCommented:
Do you have multiple different python versions or virtualenvs installed?

it almost seems that you installed pillow for one python, but start the python script with another interpreter.


How do you start the python file?

Do you just type
script.py

Open in new window

or do you type
python script.py

Open in new window

?

What is the output of following commands if typed in a console?
where python
where pip
assoc .py

Open in new window

0
 
cpeters5Author Commented:
BTW, I found an example using image crop to crop output to a square.  However, I still not able to run from a file. Always get this ImprtError:   No module image found.
0
 
cpeters5Author Commented:
You are right.  I have python 3.3, 3.4 and 3.5 installed.  Incidentally, the path for python 3.5 was also defined.  I removed it and now the path only contains python 3.3.   Still import error after fixing the path.  But when I ran  "python mycode.py",  the error went away.  

where python ==> c:\python33\python.exe
where pip ==> c:\python33\Scripts\pip.exe
assoc .py ===> .py=Python.File
0
 
gelonidaCommented:
Windows has it's very special way to determine which program shall open files of a certain type and up to my knowledge it does unfortunaly not involve checking the search path.

try to type the command
ftype Python.File

Open in new window


if you start a file (click on it / type it as first item in a command line) windows
checks the file suffix and determines the file type which is in our case "Python.File", then it will check which command is linked to this file type (to be determined with ftype)
However I remember vaguely, that sometimes theres even another mechanism, which overrides this settigns and is somewhere hidden in the registry

I assume, it will point to a 'generic' python launcher (something like C:\windows\py.exe, but I don't have a windows PC at hand to check ), which will NOT check the file path,  but there's an easy way to find out which python is called an your PC.

just create a small python file with following contents


import sys, time
print("My python is %r" % sys.executable)
time.sleep(2)

Open in new window


now start this python script from the cmd prompt or just double click on it and look at its output.

Now change the script slightly. If you're lucki this might allow you to force a certain python version for a given script
#!/usr/bin/env python3.3
# above line MUST be the very first line and should contain the version of python under which you'd like to run your script
# I'm not using windows a lot and never tried, but I think if you installed at least one ptyhon3 version the 'generic' launcher
# should be able to parse this line and launch the appropriate python to run your script
 import sys, time
print("My python is %r" % sys.executable)
time.sleep(2)

Open in new window

0
 
cpeters5Author Commented:
Thank you gelonida.  
Here is the output when I ran the firs test:

   V:\Orchids\Python\thumbnail>test.py
   My python is 'C:\\Python35\\python.exe'

   V:\Orchids\Python\thumbnail>

Open in new window


After  I added a line "import PIL"

V:\Orchids\Python\thumbnail>test.py
Traceback (most recent call last):
  File "V:\Orchids\Python\thumbnail\test.py", line 2, in <module>
    import PIL
ImportError: No module named 'PIL'

V:\Orchids\Python\thumbnail>

Open in new window


This problem seems to relate to PIL.  My other scripts run without preceeding with python.  Only this particular script that requires PIL that does.
0
 
gelonidaCommented:
well as I suspected:

if you start a python file (double click or just typing filename.py  in a cmd window), then your window is configured to start python3.5. (Probably because it's the last version, that you installed)

You however installed pillow for python 3.3.


I'm not sure whether you tried some of the other things that I suggested. (please reread my previous post)

Did you check the output of following command in a cmd window?
ftype Python.File

Open in new window

Please tell me what's the output.


Did you try to change the very first line in my test script  script as I suggested to
#!/usr/bin/env python3.3

Open in new window


Did it then display another python version?

If yes, then you can add this line to your script to force it using the right python version.

If not, then just download the pillow installer for python 3.5 and install it.
0
 
cpeters5Author Commented:
Thanks gelonida.  Your suggestion works!
0
 
gelonidaCommented:
just out of curiosity (and for others reading this thread lateron):

Which suggestion did work out for you?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.