Solved

How to generate square thumbnail using perl

Posted on 2016-07-14
13
68 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
  • 7
  • 6
13 Comments
 
LVL 16

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 16

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
 

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 16

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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 16

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 16

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 16

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Installing Flask app on IIS7 10 265
Replace  text in a file 2 83
os.getenviron does not work when running script from crontab 10 46
looping 11 27
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now