Solved

How to generate square thumbnail using perl

Posted on 2016-07-14
13
104 Views
Last Modified: 2016-08-07
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
0
Comment
Question by:cpeters5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 17

Expert Comment

by:gelonida
ID: 41711456
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
 

Author Comment

by:cpeters5
ID: 41711466
I use python 3.3.  Apparently,  pip cant find a compatible version of PIL. Also tried python 3.5 with no luck.
0
 
LVL 17

Accepted Solution

by:
gelonida earned 500 total points
ID: 41711485
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cpeters5
ID: 41711493
Thank yo gelonida. Managed to install pillow.  But your code produced ImportError: No module name PIL
0
 

Author Comment

by:cpeters5
ID: 41711514
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
 
LVL 17

Expert Comment

by:gelonida
ID: 41711555
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
 

Author Comment

by:cpeters5
ID: 41711570
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
 

Author Comment

by:cpeters5
ID: 41711575
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
 
LVL 17

Expert Comment

by:gelonida
ID: 41713319
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
 

Author Comment

by:cpeters5
ID: 41715719
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
 
LVL 17

Expert Comment

by:gelonida
ID: 41717859
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
 

Author Comment

by:cpeters5
ID: 41722139
Thanks gelonida.  Your suggestion works!
0
 
LVL 17

Expert Comment

by:gelonida
ID: 41725978
just out of curiosity (and for others reading this thread lateron):

Which suggestion did work out for you?
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question