image: Simplify image handling.

This commit is contained in:
dylan araps
2017-12-14 10:06:16 +11:00
parent 8762403de9
commit 6c238a03b2
2 changed files with 27 additions and 92 deletions

View File

@@ -617,16 +617,6 @@ image_loop="off"
# Values: 'dir' # Values: 'dir'
thumbnail_dir="${XDG_CACHE_HOME:-${HOME}/.cache}/thumbnails/neofetch" thumbnail_dir="${XDG_CACHE_HOME:-${HOME}/.cache}/thumbnails/neofetch"
# Crop mode
#
# Default: 'normal'
# Values: 'normal', 'fit', 'fill'
# Flag: --crop_mode
#
# See this wiki page to learn about the fit and fill options.
# https://github.com/dylanaraps/neofetch/wiki/What-is-Waifu-Crop%3F
crop_mode="normal"
# Crop offset # Crop offset
# Note: Only affects 'normal' crop mode. # Note: Only affects 'normal' crop mode.
# #
@@ -637,20 +627,21 @@ crop_mode="normal"
crop_offset="center" crop_offset="center"
# Image size # Image size
# The image is half the terminal width by default. # NOTE: 'square' is the OLD behavior.
# NOTE: 'auto' keeps image aspect ratio and size where possible.
# #
# Default: 'auto' # Default: 'auto'
# Values: 'auto', '00px', '00%', 'none' # Values: 'auto', 'square'
# Flags: --image_size # Flags: --image_size
# --size # --size
image_size="auto" image_size="auto"
# Ggap between image and text # Gap between image and text
# #
# Default: '3' # Default: '4'
# Values: 'num', '-num' # Values: 'num', '-num'
# Flag: --gap # Flag: --gap
gap=3 gap=4
# Image offsets # Image offsets
# Only works with the w3m backend. # Only works with the w3m backend.

View File

@@ -2667,49 +2667,31 @@ get_image_size() {
font_width="$((term_width / columns))" font_width="$((term_width / columns))"
font_height="$((term_height / lines))" font_height="$((term_height / lines))"
# Get image size.
size="$(identify -format "%w %h" "$image")"
width="${size%% *}"
height="${size##* }"
case "$image_size" in case "$image_size" in
"auto") "square")
image_size="$((columns * font_width / 2))" width="$((columns * font_width / 2))"
term_height="$((term_height - term_height / 4))" height="$width"
((term_height < image_size)) && \
image_size="$term_height"
;; ;;
*"%") *)
percent="${image_size/\%}" while (( width >= (term_width / 2) ||
image_size="$((percent * term_width / 100))" height >= (term_height / 2) )); do
width="$((width * 10 / 15))"
(((percent * term_height / 50) < image_size)) && \ height="$((height * 10 / 15))"
image_size="$((percent * term_height / 100))"
;;
"none")
# Get image size so that we can do a better crop.
size="$(identify -format "%w %h" "$image")"
width="${size%% *}"
height="${size##* }"
crop_mode="none"
while (( "$width" >= ("$term_width" / 2) ||
"$height" >= "$term_height" )); do
width="$((width / 2))"
height="$((height / 2))"
done done
;; ;;
*) image_size="${image_size/px}" ;;
esac esac
width="${width:-$image_size}"
height="${height:-$image_size}"
text_padding="$((width / font_width + gap + xoffset/font_width))" text_padding="$((width / font_width + gap + xoffset/font_width))"
} }
make_thumbnail() { make_thumbnail() {
# Name the thumbnail using variables so we can # Name the thumbnail using variables so we can use it later.
# use it later.
image_name="$crop_mode-$crop_offset-$width-$height-${image##*/}" image_name="$crop_mode-$crop_offset-$width-$height-${image##*/}"
# Handle file extensions. # Handle file extensions.
@@ -2722,62 +2704,24 @@ make_thumbnail() {
# Create the thumbnail dir if it doesn't exist. # Create the thumbnail dir if it doesn't exist.
mkdir -p "$thumbnail_dir" mkdir -p "$thumbnail_dir"
# Check to see if the thumbnail exists before we do any cropping.
if [[ ! -f "$thumbnail_dir/$image_name" ]]; then if [[ ! -f "$thumbnail_dir/$image_name" ]]; then
# Get image size so that we can do a better crop. case "$image_size" in
if [[ -z "$size" ]]; then "square")
size="$(identify -format "%w %h" "$image")"
og_width="${size%% *}"
og_height="${size##* }"
# This checks to see if height is greater than width
# so we can do a better crop of portrait images.
size="$og_height"
((og_height > og_width)) && size="$og_width"
fi
case "$crop_mode" in
"fit")
c="$(convert "$image" \
-colorspace srgb \
-format "%[pixel:p{0,0}]" info:)"
convert \ convert \
-background none \
"$image" \ "$image" \
-trim +repage \ -strip \
-gravity south \ -quality 50 \
-background "$c" \ -gravity "$crop_offset" \
-extent "$size"x"$size" \
-scale "$width"x"$height" \
"$thumbnail_dir/$image_name"
;;
"fill")
convert \
-background none \ -background none \
"$image" \ -sample "$width"x"$height"^ \
-trim +repage \
-scale "$width"x"$height"^ \
-extent "$width"x"$height" \ -extent "$width"x"$height" \
"$thumbnail_dir/$image_name" "$thumbnail_dir/$image_name"
;; ;;
"none") cp "$image" "$thumbnail_dir/$image_name" ;; *) cp "$image" "$thumbnail_dir/$image_name" ;;
*)
convert \
-background none \
"$image" \
-gravity "$crop_offset" \
-crop "$size"x"$size"+0+0 \
-quality 95 \
-scale "$width"x"$height" \
"$thumbnail_dir/$image_name"
;;
esac esac
fi fi
# The final image.
image="$thumbnail_dir/$image_name" image="$thumbnail_dir/$image_name"
} }