r/Python • u/[deleted] • 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
u/thatrandomnpc It works on my machine Mar 25 '21 edited Mar 25 '21
You should show your boss how small automations can improve quality of work and inturn bring more productivity, work and revenue.
Here's my first automation story from back when I first started working. Not python at all, but somewhat relevant. Bit of a long story, here it goes..
It was an analytics, etl and data warehousing firm, and once in a month the support team would get data files from various sources and they had to do bit of excel work and verify data from many dashboards and compile KPIs to generate a report to present it to the higher ups. The person who's doing this would be busy for the better part of the day and they have been doing this every month for the past 5-6 years at the time. Wherever they find some strange numbers they do some preliminary analysis and raise bugs with the dev team. This would delay the reports to be sent until the bug is fixed.
This is how I actually came to know about this whole process when a bug was assigned to me. When i checked the db everything seems normal and on futher investigation, I found the person had actually copied the wrong files into the wrong excel sheet. Eh this is human error I thought and closed the bug. Until the checked reports for the bugs raised, and found out it's a common occurrence.
Like the lazy person I am who doesn't want to go through the whole manual process to find bugs which doesn't exist. I spoke with my lead and developed a workflow in my free time, which would do all the manual stuff, mixing and matching and compiling and loaded the results into a table. Made a frontend dashboards where us devs can monitor those compiled KPIs. We eventually tested it properly and released it to the support team.
This saved a lot of unnecessary hiccups in generating the reports for the higher ups, reduced bugs raised and ended up increasing the data quality.
When I asked why this was not done in the past 5 years, the devs were busy with other active dev work and didn't get any request from the support team for automating it. The support team on the other hand were getting billed for the manual work they were doing.
And I was like ok ಠಿ_ಠ, did I actually reduce the billing the support team gets. Actually not, the support team were getting time to work on other support issues.
Anyhow most of the devs and support people were happy and the higher ups were happy they were getting reports on time ¯_(ツ)_/¯