r/Python • • Mar 24 '21

Discussion Python made me look like a WIZARD

Today in work a request comes in for 745 headshots that need to be processed to fit 150x150px for an external platform. One team says it'll take them at least a week to do, the other team says they don't want it as it'll take even longer.

"They need them all black and white! It'll take some time to convert them! They're not even all the same size! UGH this just can't be done in the 2 days they've requested this!!!"

12 lines of code, the Pillow library and exactly 8.856 seconds of running said code... It was done. Panic over. Client happy.

I work in a design team but I'm a job coordinator (so I sort the jobs and make sure the right designers get them). They were amazed it was done so quickly without using Photoshop.

They asked how. I gave them the answer.

Python baby 😎

EDIT

For those wanting to know the code, here it is:

from PIL import Image
import os

for f in os.listdir('Input'):
    if f.endswith('.jpg'):
        im = Image.new('RGB', (150, 150), (255,255,255))
        im2 = Image.open('Input/{}'.format(f))
        im2 = im2.convert("L")
        width, height = im2.size
        im.paste(im2, (int((150 - width) / 2), 0))
        fn, fext = os.path.splitext(f)
        im.save('Output/{}.jpg'.format(fn))

The images given were either 150x150 already (but in colour) or 120x150. So had to cover all bases with this. If it could be made more efficient, let me know!

2.2k Upvotes

278 comments sorted by

View all comments

Show parent comments

57

u/killersquirel11 Mar 24 '21

It'd also be approachable with imagemagick and some bash fu

16

u/riffito Mar 24 '21

With IrfanView on Windows (or Wine) you could do it from a GUI.

IrfanView... the only software that I never changed for anything else since 1997.

3

u/jobomat Mar 25 '21

Yeah! I really like IrfanView. Small footprint, rocket fast, no BS. Only thing I'm missing is OpenEXR support.

1

u/riffito Mar 25 '21

OpenEXR

Among the official plugings, there is a "Exr.dll" one, and an old internet post from 2009 mentions a user being glad it supports OpenEXR (albeit with some issues at the time).

Make sure you have them installed!

2

u/jobomat Mar 25 '21

Thank you for the reply! I know about the EXR plugin. But last time I tried it, I was not convinced. If I remember correctly it didn't work with multi channel EXRs (which is what I need the most) and so I searched for other solutions (and found DJV which is also awesome).

13

u/lolinux Mar 24 '21

This was exactly my thought! However, since OP is attracted to Python, I can only cheer! You usually use the tools you know and trust best for the task you have at hand!

8

u/killersquirel11 Mar 24 '21

Yep. Definitely not knocking OP at all - whatever code solves the problem is the right code in a situation like this.

It's neat to see the ways that different people will go about solving a problem.

3

u/HughBothwell Apr 01 '21

Yes, ImageMagick makes it easy. From the Windows command-line, in the dir containing the files,

md thumbs
cd thumbs
magick ../*.jpg -colorspace gray -background black -gravity center -extent 150x150 %[filename:original].jpg

creates a new subdirectory, processes all the jpg files, and stores the new grayscale/resized images (with the same name) in the new dir.

1

u/Broken_hopes Mar 24 '21

yoink bash fu is mine now