Link to home
Start Free TrialLog in
Avatar of cpeters5
cpeters5

asked on

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
Avatar of gelonida
gelonida
Flag of France image

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

Avatar of cpeters5
cpeters5

ASKER

I use python 3.3.  Apparently,  pip cant find a compatible version of PIL. Also tried python 3.5 with no luck.
ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank yo gelonida. Managed to install pillow.  But your code produced ImportError: No module name PIL
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
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

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.
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
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

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.
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.
Thanks gelonida.  Your suggestion works!
just out of curiosity (and for others reading this thread lateron):

Which suggestion did work out for you?