赵灵越 2024-05-17 12:50 采纳率: 99.3%
浏览 1
已结题

sh文生图,怎么这个代码到处都是问题?不是存储权限!一毛钱关系都没有

#!/bin/bash

# 设置字体路径
font_path="/storage/emulated/0/字体/黑体.ttf"
# 设置保存图片的目录
image_directory="/storage/emulated/0/图片"

# 定义生成文本图片的函数
generate_text_images() {
  current_time=$(date +"%Y-%m-%d_%H:%M:%S")
  image_width=1920
  image_height=1080
  max_chars_per_line=30
  image_number=1

  mkdir -p "$image_directory"

  colors=("red" "green" "blue" "yellow" "orange")
  num_colors=${#colors[@]}

  cd "$image_directory" || exit

  while IFS= read -r line; do
    if [ ! -z "$line" ]; then
      truncated_text=$(echo "$line" | sed -E "s/(.{1,$max_chars_per_line}[。?!……;]+)\s*/\1\n/g")

      IFS=$'\n'
      for text_line in $truncated_text; do
        text_line=$(echo "$text_line" | sed -E "s/[。?!……;]+/\n/g")
        rand_index=$(($RANDOM % num_colors))
        bg_color=${colors[$rand_index]}
        text_color="white"

        text_length=${#text_line}
        scale_factor=$(bc <<< "scale=2;$image_width * 0.7 / ($text_length * 36)")

        convert -size "${image_width}x${image_height}" xc:black -font "$font_path" -pointsize "$(bc <<< "scale=0; 36 * $scale_factor")" -interword-spacing 10 -fill "$text_color" -gravity center -annotate 0 "$text_line" -extent "${image_width}x${image_height}" "${image_number}_文本_${bg_color}_${current_time}.jpg"

        echo "${image_number}.${text_line} ${bg_color}${current_time}"
        ((image_number++))
      done
    fi
  done
}

generate_text_images

# 打印
jk
convert: unable to open image '1_文本_blue_2024-05-17_12:42:31.jpg': Operation not permitted @ error/blob.c/OpenBlob/3596.
1.jk blue2024-05-17_12:42:31

我的代码没问题

#!/bin/bash

# 设置字体路径
font_path="/storage/emulated/0/字体/黑体.ttf"
# 设置保存图片的目录
image_directory="/storage/emulated/0/图片"

# 定义生成文本图片的函数
generate_text_images() {
  # 获取当前时间戳
  current_time=$(date +"%Y-%m-%d_%H:%M:%S")

  # 定义图片尺寸
  image_width=1920
  image_height=1080

  # 定义最大每行容纳字数
  max_chars_per_line=30

  # 设定图片编号
  image_number=1

  # 创建保存图片的目录
  mkdir -p "$image_directory"

  # 随机生成5种颜色背景与文字颜色
  colors=("red" "green" "blue" "yellow" "orange")
  num_colors=${#colors[@]}

  # 切换到图片目录下
  cd "$image_directory" || return

  # 遍历输入的文本行
  while IFS= read -r line; do
    if [[ ! -z "$line" ]]; then
      # 从标点符号处截断文本
      truncated_text=$(echo "$line" | sed -E "s/(.{1,$max_chars_per_line}[。?!……;]+)\s*/\1\n/g")

      # 按照换行符分割文本并处理每行文本
      IFS=$'\n'
      for text_line in $truncated_text; do
        # 替换标点符号为换行符
        text_line=$(echo "$text_line" | sed -E "s/[。?!……;]+/\n/g")

        # 随机选择背景颜色和文字颜色
        rand_index=$((RANDOM % num_colors))
        bg_color=${colors[$rand_index]}
        text_color="white"

        # 计算放大比例
        text_length=${#text_line}
        scale_factor=$(bc <<< "scale=2;$image_width * 0.7 / ($text_length * 36)")

        # 生成黑底白字图片
        convert -size "${image_width}x${image_height}" xc:black -font "$font_path" -pointsize "$(bc <<< "scale=0; 36 * $scale_factor")" -interword-spacing 10 -fill "$text_color" -gravity center -annotate 0 "$text_line" -extent "${image_width}x${image_height}" "${image_number}_文本_${bg_color}_${current_time}.jpg"

        echo "${image_number}.${text_line} ${bg_color}${current_time}"

        # 增加图片编号
        ((image_number++))
      done
    fi
  done
}

# 从标准输入读取多行文本,并调用生成文本图片的函数generate_text_images
generate_text_images
  • 写回答

3条回答 默认 最新

  • 赵灵越 2024-05-22 14:50
    关注
    
    #!/bin/bash
    # 设置字体路径
    font_path="/storage/emulated/0/字体/黑体.ttf"
    # 设置保存图片的目录
    image_directory="/storage/emulated/0/图片"
    # 定义生成文本图片的函数
    generate_text_images() {
      # 获取当前时间戳
      current_time=$(date +"%Y-%m-%d_%H:%M:%S")
      # 定义图片尺寸
      image_width=1920
      image_height=1080
      # 定义最大每行容纳字数
      max_chars_per_line=30
      # 设定图片编号
      image_number=1
      # 创建保存图片的目录
      mkdir -p "$image_directory"
      # 随机生成5种颜色背景与文字颜色
      colors=("red" "green" "blue" "yellow" "orange")
      num_colors=${#colors[@]}
      # 切换到图片目录下
      cd "$image_directory" || return
      # 遍历输入的文本行
      while IFS= read -r line; do
        if [[ ! -z "$line" ]]; then
          # 从标点符号处截断文本
          truncated_text=$(echo "$line" | sed -E "s/(.{1,$max_chars_per_line}[。?!……;]+)\s*/\1\n/g")
          # 按照换行符分割文本并处理每行文本
          IFS=$'\n'
          for text_line in $truncated_text; do
            # 替换标点符号为换行符
            text_line=$(echo "$text_line" | sed -E "s/[。?!……;]+/\n/g")
            # 随机选择背景颜色和文字颜色
            rand_index=$((RANDOM % num_colors))
            bg_color=${colors[$rand_index]}
            text_color="white"
            # 计算放大比例
            text_length=${#text_line}
            scale_factor=$(bc <<< "scale=2;$image_width * 0.7 / ($text_length * 36)")
            # 生成黑底白字图片
            convert -size "${image_width}x${image_height}" xc:black -font "$font_path" -pointsize "$(bc <<< "scale=0; 36 * $scale_factor")" -interword-spacing 10 -fill "$text_color" -gravity center -annotate 0 "$text_line" -extent "${image_width}x${image_height}" "${image_number}_文本_${bg_color}_${current_time}.jpg"
            echo "${image_number}.${text_line} ${bg_color}${current_time}"
            # 增加图片编号
            ((image_number++))
          done
        fi
      done
    }
    # 从标准输入读取多行文本,并调用生成文本图片的函数generate_text_images
    generate_text_images
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月30日
  • 已采纳回答 5月22日
  • 创建了问题 5月17日

悬赏问题

  • ¥20 ic卡dump文件校检码解密
  • ¥15 关于:接收到的数据不是有效的JSON格式
  • ¥15 apdl语言如何增加受力分析
  • ¥15 算法对比:学校优化算法与蚁群算法对比
  • ¥15 机电一体化系统设计说明书
  • ¥20 sgy数据提取地震波速,有人能回答吗小馋
  • ¥20 c#实现打开word的功能,并且需要安装成windows服务,word打不开怎么办
  • ¥15 python用ARIMA时间预测模型预测数据出错,急!
  • ¥30 为什么后端传给前端vue的河流json数据不在地图中显示出来
  • ¥50 关于弹性波动方程求解的问题: