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

38

u/[deleted] Mar 24 '21

We're all windows unfortunately!

51

u/jonasbxl Mar 24 '21

I am not saying it would have been easier or faster, but just in case you don't know about it: check out WSL https://docs.microsoft.com/en-us/windows/wsl/install-win10

15

u/ninj0rc Mar 24 '21

Just wanna say thanks for the link. I was unaware that this thing existed.

10

u/Rieux_n_Tarrou Mar 24 '21

After WSL2 came out, I've decided finally that my next laptop will be Windows (even though I really enjoy my macbook pro right now). Windows is better than Mac from the perspective of apps. And Linux is better than Mac from the perspective of software dev. So WSL is the best of both worlds IMO

0

u/[deleted] Mar 25 '21

[deleted]

1

u/Rieux_n_Tarrou Mar 25 '21

Oh interesting. I don't know much about security, but IIRC, you choose what distro you want on WSL, so do you pick Kali?

1

u/jonasbxl Mar 25 '21

It's really been a great addition to Windows. I've moved a lot of my dev tools to WSL, including Python actually - I never liked Anaconda too much and I find it easier to just manage my environments with virtualenv and virtualenvwrapper. You can still use VS Code from Windows; a shortcut to quickly open a directory from WSL is code . . That way VS Code uses Python (and virtual envs) installed on WSL.

11

u/riffito Mar 24 '21

IrfanView to the rescue! (and you do it all from the GUI with its batch-converter)

9

u/[deleted] Mar 24 '21

I guess you can install those programs on windows too using scoop or choco

0

u/[deleted] Mar 24 '21

[deleted]

4

u/khalidpro2 Mar 24 '21

installing choco is just one command that you need to paste and run in powershell

5

u/diamondketo Mar 24 '21

That's not the point. The point is people need to stop subjugating others to their own method when the OP is making a one off script.

One off scripts are just that, one off. It doesn't matter what method you do. If it gets the job done correctly in a reasonable time, do that. OP has the best judgement on which route OP should take given the knowledge and resource OP has at the time.

4

u/khalidpro2 Mar 24 '21

I totally agree with that, I just wanted to clarify that choco installation isn't that difficult

1

u/[deleted] Mar 25 '21

Yes I totally agree, but as a programmer you're mindset should always be can we do it better

1

u/diamondketo Mar 25 '21

Sure, but as a software developer my mindset is can I get the deliverable to someone in time or make progress on the software cycle.

1

u/[deleted] Mar 25 '21

My ans is just an alternative solution. I'm, by no means, criticizing that you should do this in my way.

I'm just informing that you can also solve it this way. That's it. Nothing more. Not trying to insinuate anything else

7

u/[deleted] Mar 24 '21

The same can be said about writing python scripts, you need to which library, which functions and what parameters.

The idea behind my comments is todo with Unix philosophy.

Also the command line ans is 5x faster than python version

-1

u/shinitakunai Mar 24 '21

Fortunately!

1

u/[deleted] Mar 25 '21

Hello windows, I am dad.