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

104

u/robin_888 Mar 24 '21

Wow, 9 seconds is really fast for over 700 files.

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.

Unbelievable. May I ask what kind of team that was?

Even without programming skills I would never think about doing this by hand. Personally I'd use the batch processor on XNView (or any other decent viewer), but even if I wouldn't know that I'd search for an semi-automatic solution. And if it's Paint and AutoHotkey!

PS: Even manually it's a man-day of work and not a freakin' week! (2 images per minute => 6-7 hours, probably faster)

46

u/[deleted] Mar 24 '21

It's a team that handle multiple design requests a day. So getting even a day to do this with no other work is impossible. They are constantly designing things. It's an in house design team for a law firm.

36

u/[deleted] Mar 24 '21

Shit, I work for a law firm too, it feels like a lot of them are complete technological backwaters. Peeps struggle to convert pdfs. I'm a tech wizard because I know how to paginate in Adobe.

21

u/kingsillypants Mar 24 '21

What is paginate in Adobe?

39

u/[deleted] Mar 24 '21

Bro, please you're triggering my PTSD. Adding page numbers to PDF documents in Acrobat.

10

u/proof_required Mar 24 '21

I do coding etc but never knew about this. You are a real tech wizard.