Skip to content

Commit

Permalink
Merge pull request #290 from jekhokie/add-extend-for-annotation
Browse files Browse the repository at this point in the history
Add Extend for Annotation
  • Loading branch information
jekhokie authored Mar 17, 2021
2 parents 8bdbcf6 + a398c95 commit ef4d41c
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 18 deletions.
1 change: 1 addition & 0 deletions ansible/roles/common/templates/noaa-v2.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ PRUNE_OLDEST={{ delete_oldest_n }}
METEOR_RECEIVER={{ meteor_receiver }}
NOAA_CROP_TELEMETRY={{ noaa_crop_telemetry|lower }}
IMAGE_ANNOTATION_LOCATION={{ image_annotation_location }}
EXTEND_FOR_ANNOTATION={{ extend_for_annotation|lower }}
GROUND_STATION_LOCATION='{{ ground_station_location }}'
NOAA_DAY_ENHANCEMENTS='{{ noaa_daytime_enhancements }}'
NOAA_NIGHT_ENHANCEMENTS='{{ noaa_nighttime_enhancements }}'
Expand Down
4 changes: 4 additions & 0 deletions config/settings.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ delete_audio: false
# noaa_crop_telemetry - whether to crop the left/right telemetry in image captures
# image_annotation_location - where to place the annotation in images - valid options are:
# NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast
# extend_for_annotation - whether to create a black extension on the north/south location of
# the image to place the annotation into (vs. overlaying on the captured data)
# (note: this will ONLY work if the image_annotation_location is NOT one of [West|Center|East])
# produce_noaa_pristine_image - whether to produce a pristine image (unmodified) for larger
# composite-based use cases
# produce_noaa_pristine_histogram - whether to produce a histogram of the NOAA pristine image
Expand All @@ -98,6 +101,7 @@ flip_meteor_image: true
produce_spectrogram: true
noaa_crop_telemetry: false
image_annotation_location: 'NorthWest'
extend_for_annotation: false
produce_noaa_pristine_image: false
produce_noaa_pristine_histogram: false
produce_polar_az_el_graph: false
Expand Down
2 changes: 2 additions & 0 deletions config/settings_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"image_annotation_location": {
"type": "string"
},
"extend_for_annotation": { "type": "boolean" },
"produce_noaa_pristine_image": { "type": "boolean" },
"produce_noaa_pristine_histogram": { "type": "boolean" },
"produce_polar_az_el_graph": { "type": "boolean" },
Expand Down Expand Up @@ -180,6 +181,7 @@
"produce_spectrogram",
"noaa_crop_telemetry",
"image_annotation_location",
"extend_for_annotation",
"produce_noaa_pristine_image",
"produce_noaa_pristine_histogram",
"produce_polar_az_el_graph",
Expand Down
1 change: 1 addition & 0 deletions scripts/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fi
# binary helpers
CONVERT="/usr/bin/convert"
GMIC="/usr/bin/gmic"
IDENTIFY="/usr/bin/identify"
MEDET_ARM="/usr/bin/medet_arm"
METEOR_DEMOD="/usr/bin/meteor_demod"
PREDICT="/usr/bin/predict"
Expand Down
70 changes: 58 additions & 12 deletions scripts/image_processors/meteor_normalize_annotate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,70 @@ yml_config=$(echo "${yml_config}" | sed -e "s/\.\.\.$/gain: $gain\n.../")
# vars for image manipulation
tmp_dir="${NOAA_HOME}/tmp/annotation"
rendered_file="${tmp_dir}/index.html"
annotation="${tmp_dir}/annotation.png"

# generate annotation html and copy any assets
$SCRIPTS_DIR/tools/jinja2_to_file.py "${NOAA_HOME}/config/annotation/annotation.html.j2" "${yml_config}" "${rendered_file}"
find $NOAA_HOME/config/annotation/* -type f -not -name "*.j2" -exec cp {} "${tmp_dir}/" \;

# generate annotation png and crop to content
$WKHTMLTOIMG --enable-local-file-access --format png --quality 100 --transparent "file://${rendered_file}" "${tmp_dir}/annotation.png"
$CONVERT -format png "${tmp_dir}/annotation.png" -background none -flatten -trim +repage "${tmp_dir}/annotation.png"
$WKHTMLTOIMG --enable-local-file-access --format png --quality 100 --transparent "file://${rendered_file}" "${annotation}"
$CONVERT -format png "${annotation}" -background none -flatten -trim +repage "${annotation}"

# generate final image with annotation, doubling the annotation for account
# for the LRPT image sizes, keeping aspect ratio for original image
img_w=$($CONVERT "${tmp_dir}/annotation.png" -format "%w" info:)
img_h=$($CONVERT "${tmp_dir}/annotation.png" -format "%h" info:)
# resize the annotation appropriately, keeping aspect ratio
img_w=$($CONVERT "${annotation}" -format "%w" info:)
img_h=$($CONVERT "${annotation}" -format "%h" info:)
new_img_w=$((img_w * 2))
new_img_h=$((img_h * 2))
echo $new_img_w
echo $new_img_h
$CONVERT -format jpg "${INPUT_JPG}" \( "${tmp_dir}/annotation.png" -resize "${new_img_w}x${new_img_h}^" \) \
-gravity $IMAGE_ANNOTATION_LOCATION \
-geometry +10+10 \
-composite "${OUTPUT_JPG}"
$CONVERT "${annotation}" -resize "${new_img_w}x${new_img_h}^" "${annotation}"

# extend the image if the user specified and didn't use
# one of [West|Center|East] for the annotation location
# TODO: DRY this up - this is the same code as in the NOAA script
annotation_location=$(echo $IMAGE_ANNOTATION_LOCATION | tr '[:upper:]' '[:lower:]')
extend_annotation=0
if [ "${EXTEND_FOR_ANNOTATION}" == "true" ]; then
if [[ "${annotation_location}" =~ ^(west|center|east)$ ]]; then
log "You specified extending the annotation, but your annotation location $annotation_location does not support it" "WARN"
else
extend_annotation=1
fi
fi

# generate the final image with annotation
if [ $extend_annotation -eq 1 ]; then
# calculate expansion height needed to fit annotation
annotation_h=$($IDENTIFY -format "%h" "${annotation}")
img_expand_px=$(($annotation_h + 20))
out_file=$(basename $OUTPUT_JPG)
tmp_out="${NOAA_HOME}/tmp/${out_file%%.*}-tmp.jpg"

# create pixels north or south depending on annotation location
gravity_var="South"
if [[ "${annotation_location}" =~ ^(northwest|north|northeast)$ ]]; then
gravity_var="North"
fi

$CONVERT -quality 100 \
-format jpg "${INPUT_JPG}" \
-gravity "${gravity_var}" \
-background black \
-splice "0x${img_expand_px}" "${tmp_out}"

# generate final image with annotation
$CONVERT -format jpg "${tmp_out}" "${annotation}" \
-gravity $IMAGE_ANNOTATION_LOCATION \
-geometry +0+10 \
-composite "${OUTPUT_JPG}"

# clean up
rm "${tmp_out}"
else
$CONVERT -format jpg "${INPUT_JPG}" "${annotation}" \
-gravity $IMAGE_ANNOTATION_LOCATION \
-geometry +10+10 \
-composite "${OUTPUT_JPG}"
fi

# clean up the annotation
rm "${annotation}"
55 changes: 49 additions & 6 deletions scripts/image_processors/noaa_normalize_annotate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,52 @@ find $NOAA_HOME/config/annotation/* -type f -not -name "*.j2" -exec cp {} "${tmp
$WKHTMLTOIMG --enable-local-file-access --format png --quality 100 --transparent "file://${rendered_file}" "${tmp_dir}/annotation.png"
$CONVERT -format png "${tmp_dir}/annotation.png" -background none -flatten -trim +repage "${tmp_dir}/annotation.png"

# generate final image with annotation
$CONVERT -quality $QUALITY \
-format jpg "${INPUT_JPG}" "${tmp_dir}/annotation.png" \
-gravity $IMAGE_ANNOTATION_LOCATION \
-geometry +10+10 \
-composite "${OUTPUT_JPG}"
# extend the image if the user specified and didn't use
# one of [West|Center|East] for the annotation location
annotation_location=$(echo $IMAGE_ANNOTATION_LOCATION | tr '[:upper:]' '[:lower:]')
extend_annotation=0
if [ "${EXTEND_FOR_ANNOTATION}" == "true" ]; then
if [[ "${annotation_location}" =~ ^(west|center|east)$ ]]; then
log "You specified extending the annotation, but your annotation location $annotation_location does not support it" "WARN"
else
extend_annotation=1
fi
fi

# generate the final image with annotation
if [ $extend_annotation -eq 1 ]; then
# calculate expansion height needed to fit annotation
annotation_h=$($IDENTIFY -format "%h" "${tmp_dir}/annotation.png")
img_expand_px=$(($annotation_h + 20))
out_file=$(basename $OUTPUT_JPG)
tmp_out="${NOAA_HOME}/tmp/${out_file%%.*}-tmp.jpg"

# create pixels north or south depending on annotation location
gravity_var="South"
if [[ "${annotation_location}" =~ ^(northwest|north|northeast)$ ]]; then
gravity_var="North"
fi

$CONVERT -quality 100 \
-format jpg "${INPUT_JPG}" \
-gravity "${gravity_var}" \
-background black \
-splice "0x${img_expand_px}" "${tmp_out}"

# generate final image with annotation
$CONVERT -quality $QUALITY \
-format jpg "${tmp_out}" "${tmp_dir}/annotation.png" \
-gravity $IMAGE_ANNOTATION_LOCATION \
-geometry +0+10 \
-composite "${OUTPUT_JPG}"

# clean up
rm "${tmp_out}"
else
# generate final image with annotation
$CONVERT -quality $QUALITY \
-format jpg "${INPUT_JPG}" "${tmp_dir}/annotation.png" \
-gravity $IMAGE_ANNOTATION_LOCATION \
-geometry +10+10 \
-composite "${OUTPUT_JPG}"
fi

0 comments on commit ef4d41c

Please sign in to comment.