Skip to content

Instantly share code, notes, and snippets.

@NadyaNayme
Created April 18, 2018 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NadyaNayme/fd9cda2e659728f487289145ada0e3dc to your computer and use it in GitHub Desktop.
Save NadyaNayme/fd9cda2e659728f487289145ada0e3dc to your computer and use it in GitHub Desktop.
Autocrop
~~I haven't added proper error handling for if the fwidth or fheight are smaller than the image - but there is some rudimentary error handling for cropping if we have room to move the image. What I mean by that is if we can move down and have no room to grow at the top we'll move the entire image down. If you try to crop a 500x500 square out of a 350x350 image it will behave unexpectedly.~~
My code is here, with comments:
https://github.com/Kyokou/autocrop/blob/master/autocrop/autocrop.py
Changes:
- `-s` or `--size` to pass one number to both width and height
- Crop-to-size instead of resize-to-fit
- Renamed a few variables to make them make more sense for myself
- EDIT: Won't strictly enforce height/width if height/width exceeds image size but will change fwidth/fheight to that images width/height.
Bad changes:
- I suck at Python
- The constraints I've added with `while` loops probably have a more elegant implementation
- ~~Haven't ran the code past tests other than just using it on my images~~ Code passes all tests except `test_image_files_overwritten_if_no_output_dir`. Change in how size constraints are handled may be causing it to fail asserting shape of `king.jpg` but I'm not sure what shape is returning so maybe I'm wrong.
Edit:
Known Bug:
- Size constraints are ignored if the face detected is larger than the size constraints (file will be the size of the detected face with no padding)
eg: If the detected face is 565x498 in size within a 6800x4800 image but you put `-s 400` as your size constraints your image will be 565x498 because the face doesn't shrink to fit the restraints.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment