Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 705609681
  • Loading branch information
agutkin committed Dec 12, 2024
1 parent fb1c727 commit bc83319
Show file tree
Hide file tree
Showing 4 changed files with 437 additions and 0 deletions.
69 changes: 69 additions & 0 deletions protoscribe/glyphs/helpers/raster_to_vector.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright 2024 The Protoscribe Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/bin/bash
#
# Converts input image in (PNG/JPG/etc.) format to SVG.
#
# Please note: You can *also* run this utility *directly* on the input SVGs.
#
# Dependencies:
# -------------
# sudo apt-get install imagemagick potrace

# From https://stackoverflow.com/questions/56696496/how-to-convert-jpg-or-png-image-to-svg-and-save-it:
#
# Following converts PNG file to PGM format, removes image transparency, outputs
# the result image to the standard input of `mkbitmap` that transforms the input
# with highpass filtering and thresholding into a suitable for the `potrace`
# program format, that finally generates SVG file.
#
# You can play around with highpass filtering (-f) and thresholding (-t) values
# until you have the final look that you want.

while getopts 'i:o:' OPTION ; do
case "$OPTION" in
i)
INPUT_FILE="$OPTARG"
;;
o)
OUTPUT_FILE="$OPTARG"
;;
?)
echo "Usage: $(basename \$0) -i INPUT -o OUTPUT" >&2
exit 1
;;
esac
done

if [ ! -f "${INPUT_FILE}" ] ; then
echo "Invalid input file!"
exit 1
fi
if [ -z "${OUTPUT_FILE}" ] ; then
echo "Output file not specified!"
exit 1
fi

# This is not trivial as the background is not necessarily white.
TMPFILE_PGM=$(mktemp --suffix .pgm /tmp/raster2vector.XXXXXX)
convert -verbose "${INPUT_FILE}" \
-fuzz 15% -flatten -alpha remove -background white ${TMPFILE_PGM} \
|| { echo "convert failed!" >&2; exit 1; }
TMPFILE_BMP=$(mktemp --suffix .bmp /tmp/raster2vector.XXXXXX)
mkbitmap -f 32 -t 0.4 ${TMPFILE_PGM} -o ${TMPFILE_BMP} \
|| { echo "mkbitmap failed!" >&2; exit 1; }
potrace --svg ${TMPFILE_BMP} -o "${OUTPUT_FILE}" \
|| { echo "potrace failed!" >&2; exit 1; }
rm -f ${TMPFILE_PGM} ${TMPFILE_BMP}
53 changes: 53 additions & 0 deletions protoscribe/glyphs/helpers/vector_to_raster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2024 The Protoscribe Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/bin/bash
#
# Converts SVGs to PNGs.
#
# Dependencies:
# -------------
# sudo apt-get install librsvg2-bin

WIDTH=256px
HEIGHT=256px
BACKGROUND=
while getopts 'i:o:h:w:b' OPTION ; do
case "$OPTION" in
i)
INPUT_FILE="$OPTARG"
;;
o)
OUTPUT_FILE="$OPTARG"
;;
w)
WIDTH="$OPTARG"
;;
h)
HEIGHT="$OPTARG"
;;
b)
BACKGROUND="-b white"
;;
?)
echo "Usage: $(basename \$0) -i INPUT -o OUTPUT" >&2
exit 1
;;
esac
done

rsvg-convert \
--width=${WIDTH} --height=${HEIGHT} \
--keep-aspect-ratio ${BACKGROUND} ${INPUT_FILE} > ${OUTPUT_FILE} \
|| { echo "rsvg-convert failed!" >&2; exit 1; }
66 changes: 66 additions & 0 deletions protoscribe/glyphs/make_text.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2024 The Protoscribe Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/bin/bash

# Usage, e.g.:
#
# protoscribe/glyphs/make_text.sh \
# -t X,X,I,I,CLOTH,SHIRT \
# -p /var/tmp/22_shirts.png \
# -d # If you want to display the result with Eye of GNOME (eog) viewer.

while getopts 'b:t:p:s:S:d' OPTION
do
case "${OPTION}" in
b)
BCKGRND="${OPTARG}"
;;
t)
TERMS="${OPTARG}"
;;
p)
PNG="${OPTARG}"
;;
s)
SVG="${OPTARG}"
;;
S)
SVG_FOR_STROKES="${OPTARG}"
;;
d)
DISPLAY_PNG=true
;;
esac
done
if [ "${BCKGRND}" == "" ]
then
BCKGRND="ivory"
fi

SRC_DIR=protoscribe
"python ${SRC_DIR}/glyphs/make_text"_main.py \
--random_resize \
--random_pad \
--random_rotate \
--extra_pad=50 \
--concepts="${TERMS}" \
--background_color="${BCKGRND}" \
--svg_output="${SVG}" \
--svg_for_strokes_output="${SVG_FOR_STROKES}" \
--output="${PNG}"
if [ "${DISPLAY_PNG}" == "true" ]
then
eog "${PNG}"
fi
Loading

0 comments on commit bc83319

Please sign in to comment.