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

148

u/gandalfx Mar 24 '21

Now be honest, how long did it take you to write those 12 lines? :D

I mean, it's still going to be a lot faster than doing it manually but I've been guilty of pretending that it'll take "no time at all" only to sit there for hours perfecting a tiny script.

xkcd: 1319, 974 and probably others

72

u/[deleted] Mar 24 '21

It took 20 mins to work it all out in terms of what I needed to do and how to execute it - I had never used the PIL / Pillow library before! But worked it all out thanks to the documentation :)

86

u/SnowdenIsALegend Mar 24 '21

She said be honest. Come on man, credit StackOverflow, everyone knows no one reads documentation.

0

u/[deleted] Mar 24 '21

[deleted]

6

u/flipmcf Mar 24 '21

Almost as good as the DateTime docs.

9

u/evkan Mar 24 '21

true. I read git commits.

46

u/Dustin- Mar 24 '21

The real trick is learning how to make code that you're happy to throw away after you're done with it. Whip something up to do a quick job, do the job, get rid of the code. Doesn't matter how sloppy or efficient it is as long as it works.

25

u/djamp42 Mar 24 '21

I save everything..1 because if want to do the same task in the future I can go back and look what I did. 2) To see how far I've advanced and what I would of done differently.

12

u/[deleted] Mar 24 '21

What I've discovered as I've progressed is that for your case 1, it's actually slower for me to look at old code to see what I did. Usually the new use case is sufficiently different that it takes me longer to understand the old code and find the places it would need modification than it would take me to just write new code for the new use case.

Remember that looking at old code also involves those "hidden costs" of finding it, deciding where to write the new code (modify the old code in place or copy it into a new file/repo?), etc.

So now I take a sort of scorched earth policy in that I'll delete things not in use even if they seem useful and might come up again. If it's something really worth keeping, for some sort of historical reason or something, I'll make a branch called "terminal/some_old_implementation" and push it to the server just before making the commit to delete the code.

6

u/pycepticus from pprint import pprint as print Mar 24 '21

My cure for this approach was learning how to comment my code. It's an extra few hours depending on how large the project I wrote was, but being able to go back years later and not have to "human compile" my code has saved me rewriting large parts of similar projects in the future.

2

u/clestrada12 Mar 24 '21

Same. I love looking at old code I’ve done. I still have the first programs.

1

u/Numnumchips Mar 25 '21

This! I work where we mainly write safety critical cpp. So python is mostly used for quick tasks. In the beginning I often spend a lot of time perfecting these scripts just to later realize I only used them once.

0

u/[deleted] Mar 24 '21

How long does it take to Google the question is how long he has to code lol

2

u/gandalfx Mar 25 '21

You sound like someone who has never gotten stuck on something that turns out to be less trivial than it should. I.e. someone who has never written code.

0

u/[deleted] Mar 24 '21

[deleted]

19

u/[deleted] Mar 24 '21

I agree - but no one was willing to look into it. In fact, most of the team probably don't know what python is or even care... but it scored me brownie points!

0

u/[deleted] Mar 24 '21

[deleted]

5

u/[deleted] Mar 24 '21

Oh I 100% agree. Part of my job of coordinating the work that comes in is to spot patterns and I just have an inkling this is going to become a regular thing as they are doing the first batch of headshot uploads for avatars. 745 users in this batch, only 9000 more to go. Locating the headshots is slowing them down - i'll maybe not offer to automate that

2

u/CopOnTheRun Mar 24 '21

I get where you're coming from, but I've never used pillow before and I treat new libraries like going on dates. I like to get to know them for a couple hours before putting them to work, even if they're only working for a fraction of a second.

0

u/[deleted] Mar 24 '21

[deleted]

3

u/taughta Mar 24 '21

If you run it quick and dirty remember to play it safe and use co... coding standards for readability.

2

u/Arrowtica Mar 24 '21

I still say this particular project was a win. One team of people said a week of work. Even if one guy spent the entire day on it, it was time saved.