Added experimental smart crop feature

This commit is contained in:
dylan araps 2016-01-01 09:33:08 +11:00
parent 07538e0bf1
commit 567ba97539
2 changed files with 84 additions and 32 deletions

View File

@ -17,7 +17,7 @@ for as many people as possible.
![1](https://sr.ht/xL9f.png) ![1](https://sr.ht/xL9f.png)
## Dependencies ## Dependencies <!-- {{{ -->
These are the script's required dependencies These are the script's required dependencies
@ -30,8 +30,10 @@ These are the script's optional dependencies:
- Display Wallpaper: feh - Display Wallpaper: feh
- Current Song: mpc - Current Song: mpc
<!-- }}} -->
## Usage
## Usage <!-- {{{ -->
The script now supports dynamic image sizing and padding, The script now supports dynamic image sizing and padding,
<br\> they're enabled by default and there's a variable you <br\> they're enabled by default and there's a variable you
@ -77,15 +79,15 @@ Text Colors:
--infocol num Change the color of the info --infocol num Change the color of the info
Text Formatting: Text Formatting:
--underline on/off Enable/Disable title underline" --underline on/off Enable/Disable title underline
--underline_char char Character to use when underlineing title" --underline_char char Character to use when underlineing title
--linewrap on/off Enable/Disable line wrapping" --linewrap on/off Enable/Disable line wrapping
--bold on/off Enable/Disable bold text" --bold on/off Enable/Disable bold text
Color Blocks: Color Blocks:
--printcols start end Range of colors to print as blocks --printcols start end Range of colors to print as blocks
--blockwidth num Width of color blocks" --blockwidth num Width of color blocks
--color_blocks on/off Enable/Disable the color blocks" --color_blocks on/off Enable/Disable the color blocks
Image: Image:
--image Image to display with the script --image Image to display with the script
@ -94,6 +96,10 @@ Image:
--fontwidth Used to automatically size the image --fontwidth Used to automatically size the image
--size px Change the size of the image --size px Change the size of the image
--smart_crop on/off Smart crop images with plain color backgrounds
--smart_crop_mode Which mode to use with smart crop
Takes the values: fit, fill
--img_auto on/off Enable/Disable automatic i mage sizing
--cropoffset value Change the crop offset. Possible values: --cropoffset value Change the crop offset. Possible values:
northwest, north, northeast, west, center northwest, north, northeast, west, center
east, southwest, south, southeast east, southwest, south, southeast
@ -106,7 +112,7 @@ Image:
--gap num Gap between image and text right side --gap num Gap between image and text right side
--images on/off Enable/Disable all images" --images on/off Enable/Disable all images"
--wall on/off Enable/Disable the wallpaper function" --wall on/off Enable/Disable the wallpaper function
and fallback to \$img and fallback to \$img
--clean Remove all cropped images --clean Remove all cropped images
@ -114,14 +120,24 @@ Other:
--help Print this text and exit --help Print this text and exit
``` ```
<!-- }}} -->
## TODO
## TODO <!-- {{{ -->
Here's what's on my todo list Here's what's on my todo list
- Add an easy way to define info prefixes at launch. - Add an easy way to define info prefixes at launch.
- Finish adding an option to smart crop images with plain color backgrounds.
<br/>What this means is that your solid bg waifu wallpaper will be cropped around
<br/>your waifu no matter where she is in the image.
- Cleanup - Cleanup
- Add options to bold other text in the script (info, underline, colons) - Add options to bold other text in the script (info, underline, colons)
- Fix linewrapping flag with tmux
- Fix issues with multiline prompts
<!-- }}} -->
### Smart Crop mode comparison
![fit](http://imgur.com/MI0UtSV)
![fill](http://imgur.com/LvxYwki)

View File

@ -41,7 +41,7 @@ title_song="Song"
linewrap="on" linewrap="on"
# Set to "", comment this line or use the flag "--bold on/off" # Set to "", comment this line or use the flag "--bold on/off"
# to disable bold text. # to disable all bold text.
bold="on" bold="on"
# Whether or not to underline the title. # Whether or not to underline the title.
@ -93,10 +93,22 @@ wall="on"
img="$HOME/Pictures/avatars/gon.png" img="$HOME/Pictures/avatars/gon.png"
# Image size is based on terminal size # Image size is based on terminal size
# Using the flag "--size" sets this to 0. # Using the flag "--size" sets this to off.
img_auto=1 img_auto="on"
# Image size to use if img_auto=0 # This is experimental and needs testing. ( Off by default )
# Smart crop images with plain color backgrounds.
# What this means is that your solid bg waifu wallpaper will be cropped around
# your waifu no matter where she is in the image.
smart_crop="off"
# Smart crop has two modes, fit and fill.
# Fit: Fit the whole character into the crop.
# Fill: Fit a portrait of the character into the crop.
# Fit gives the best results when the character isn't cut off at the sides.
smart_crop_mode="fit"
# Image size to use if img_auto="off"
# Also configureable at launch with "--size" # Also configureable at launch with "--size"
size=128 size=128
@ -306,6 +318,10 @@ usage () {
printf '%s\n' " images: (wallpaper, \$img)" printf '%s\n' " images: (wallpaper, \$img)"
printf '%s\n' " --fontwidth px Used to automatically size the image" printf '%s\n' " --fontwidth px Used to automatically size the image"
printf '%s\n' " --size px Change the size of the image" printf '%s\n' " --size px Change the size of the image"
printf '%s\n' " --smart_crop on/off Smart crop images with plain color backgrounds"
printf '%s\n' " --smart_crop_mode Which mode to use with smart crop"
printf '%s\n' " Takes the values: fit, fill"
printf '%s\n' " --img_auto on/off Enable/Disable automatic i mage sizing"
printf '%s\n' " --cropoffset value Change the crop offset. Possible values:" printf '%s\n' " --cropoffset value Change the crop offset. Possible values:"
printf '%s\n' " northwest, north, northeast, west, center" printf '%s\n' " northwest, north, northeast, west, center"
printf '%s\n' " east, southwest, south, southeast" printf '%s\n' " east, southwest, south, southeast"
@ -374,7 +390,10 @@ for argument in "$@"; do
# Image # Image
--image) wall="off"; img="$2" ;; --image) wall="off"; img="$2" ;;
--fontwidth) fontwidth="$2" ;; --fontwidth) fontwidth="$2" ;;
--size) img_auto=0 imgsize="$2" ;; --size) img_auto="off" imgsize="$2" ;;
--smart_crop) smart_crop="$2" ;;
--smart_crop_mode) smart_crop_mode="$2" ;;
--img_auto) img_auto="off" ;;
--cropoffset) crop_offset="$2" ;; --cropoffset) crop_offset="$2" ;;
--padding) padding="$2" ;; --padding) padding="$2" ;;
--xoffset) xoffset="$2" ;; --xoffset) xoffset="$2" ;;
@ -402,7 +421,7 @@ done
# If the script was called with --noimg, disable images and padding # If the script was called with --noimg, disable images and padding
if [ $images == "on" ]; then if [ $images == "on" ]; then
# Check to see if auto=1 # Check to see if auto=1
if [ $img_auto -eq 1 ]; then if [ $img_auto == "on" ]; then
# Image size is half of the terminal # Image size is half of the terminal
imgsize=$(($(tput cols) * fontwidth / 2)) imgsize=$(($(tput cols) * fontwidth / 2))
@ -412,8 +431,8 @@ if [ $images == "on" ]; then
# If wall=on, Get image to display from current wallpaper. # If wall=on, Get image to display from current wallpaper.
# (only works with feh) # (only works with feh)
[ $wall == "on" ] && \ # [ $wall == "on" ] && \
img=$(awk '/feh/ {printf $3}' "$HOME/.fehbg" | sed -e "s/'//g") # img=$(awk '/feh/ {printf $3}' "$HOME/.fehbg" | sed -e "s/'//g")
# Get name of image and prefix it with it's crop offset # Get name of image and prefix it with it's crop offset
imgname="$crop_offset-${img##*/}" imgname="$crop_offset-${img##*/}"
@ -440,17 +459,34 @@ if [ $images == "on" ]; then
size=${size[1]} size=${size[1]}
fi fi
# Crop the image and save it to the $imgtempdir # Crop the image, resize it and save it to $imgtempdir.
# By default we crop a square in the center of the image which is if [ $smart_crop == "on" ]; then
# "image height x image height". # Fixes transparent images having a white bg after -extent
# We then resize it to the image size specified above. c=$(convert "$img" -colorspace srgb -format "%[pixel:p{0,0}]" info:)
# (default 128x128 px, uses var $height)
# This way we get a full image crop with the speed benefit if [ $smart_crop_mode == "fit" ]; then
# of a tiny image. convert \
-trim +repage "$img" \
-gravity south \
-background "$c" \
-extent "$size"x"$size" \
-resize "$imgsize"x"$imgsize" \
"$imgtempdir/$imgname"
else
convert \
-trim +repage "$img" \
-resize "$imgsize"x"$imgsize"^ \
-background "$c" \
-extent "$imgsize"x"$imgsize" \
"$imgtempdir/$imgname"
fi
else
convert \ convert \
-crop "$size"x"$size"+0+0 \ -crop "$size"x"$size"+0+0 \
-gravity $crop_offset "$img" \ -gravity $crop_offset "$img" \
-resize "$imgsize"x"$imgsize" "$imgtempdir/$imgname" -resize "$imgsize"x"$imgsize" \
"$imgtempdir/$imgname"
fi
fi fi
# The final image # The final image