file-type

Bash脚本批量调整压缩包内图像尺寸并转换格式

ZIP文件

下载需积分: 9 | 3KB | 更新于2024-12-13 | 46 浏览量 | 1 下载量 举报 收藏
download 立即下载
Bash脚本工具名为resize_zippedimage,设计用于处理存档文件中的图像。该脚本能够调整图像大小,并且将存档格式转换为ZIP格式。它特别适用于处理图像文件的批量处理任务,如批量转换和压缩。以下是基于标题、描述和标签中提供的信息,对这个脚本及其功能的详细说明。 1. Bash脚本功能: 该脚本提供了批量调整图像大小并转换存档格式的能力。它主要处理以下类型的图像文件: - JPEG/JPG:一种常见的图像文件格式,支持有损压缩; - PNG:一种无损压缩的图像格式,支持透明度和更高色深的图像; - ZIP/RAR/7Z/CBZ:这些是压缩存档文件格式,其中ZIP和RAR是最常见的压缩格式,7Z是一种高压缩比的格式,CBZ是漫画书专用的ZIP格式。 2. 用法和参数: 脚本名为resize_zippedimage.sh,使用命令行参数来指定需要处理的文件。其使用格式为: ```bash resize_zippedimage.sh <files> ``` 如果在执行时不提供任何参数,脚本会默认使用当前目录下所有ZIP、RAR、7Z和CBZ格式的文件。参数可以是特定文件名,也可以是通配符,例如: ```bash resize_zippedimage.sh *.zip resize_zippedimage.sh example.cbz ``` 3. 文件处理逻辑: - **目标文件**:脚本可以处理包含图像的ZIP、RAR、7Z或CBZ存档文件。 - **调整大图像尺寸**:对于存档中高度超过2400像素的图像,脚本会进行尺寸调整,使其高度降低。 - **PNG转JPG**:脚本会将存档中的PNG图像转换成JPG格式,以减少文件大小和加快加载速度。 - **优化JPG文件**:对于JPG图像文件,脚本使用--quality 90重新编码,以尽可能减小文件大小,同时保持相对较好的图像质量。 - **写入新的存档文件**:处理后的图像会被重新打包成ZIP格式的存档文件。 - **备份原始存档**:原始的存档文件会被移动到名为org的目录中,以便于保留备份。 4. 环境支持: 该脚本设计为在Linux环境下运行,但可能同样可以在MacOS系统上运行。由于脚本是用Bash编写的,它依赖于Linux或类Unix环境的命令行工具和库。 5. 技术细节: 脚本的实现可能涉及到多种技术组件: - 文件处理:使用命令行工具如unzip、unrar和7z来解压存档文件。 - 图像处理:利用ImageMagick等图像处理库来调整图像大小和转换图像格式。 - 文件压缩:使用zip工具来重新打包处理后的文件。 6. 示例代码片段(假设部分): ```bash #!/bin/bash # 检查参数是否为空,如果为空则默认为*.zip *.rar *.7z *.cbz if [ $# -eq 0 ]; then files=(*.{zip,rar,7z,cab}) else files=("$@") fi # 遍历所有指定的存档文件 for file in "${files[@]}"; do # 检查文件是否存在 if [ ! -f "$file" ]; then echo "文件不存在: $file" continue fi # 创建org目录用于存放原始存档 mkdir -p org mv "$file" org/ # 解压存档 unzip -o org/"$file" -d tempdir # 处理图像并重新打包 # 这里可能需要结合ImageMagick和其他工具来调整图像大小、转换格式等 # 并使用zip命令打包为新的压缩文件 # 将新的压缩文件移动到原存档位置 mv tempdir/new_compressed_file.zip "$file" done echo "所有处理完成。" ``` 脚本的具体实现会依据实际的编程细节和所依赖的工具库而有所不同,上述代码仅为一个大致的框架示例。实际脚本的实现可能会更为复杂,特别是涉及到具体的图像处理和文件压缩操作。

相关推荐

filetype

int convert_image_with_letterbox(image_buffer_t* src_image, image_buffer_t* dst_image, letterbox_t* letterbox, char color) { int ret = 0; int allow_slight_change = 1; int src_w = src_image->width; int src_h = src_image->height; int dst_w = dst_image->width; int dst_h = dst_image->height; int resize_w = dst_w; int resize_h = dst_h; int padding_w = 0; int padding_h = 0; int _left_offset = 0; int _top_offset = 0; float scale = 1.0; image_rect_t src_box; src_box.left = 0; src_box.top = 0; src_box.right = src_image->width - 1; src_box.bottom = src_image->height - 1; image_rect_t dst_box; dst_box.left = 0; dst_box.top = 0; dst_box.right = dst_image->width - 1; dst_box.bottom = dst_image->height - 1; float _scale_w = (float)dst_w / src_w; float _scale_h = (float)dst_h / src_h; if(_scale_w < _scale_h) { scale = _scale_w; resize_h = (int) src_h*scale; } else { scale = _scale_h; resize_w = (int) src_w*scale; } // slight change image size for align if (allow_slight_change == 1 && (resize_w % 4 != 0)) { resize_w -= resize_w % 4; } if (allow_slight_change == 1 && (resize_h % 2 != 0)) { resize_h -= resize_h % 2; } // padding padding_h = dst_h - resize_h; padding_w = dst_w - resize_w; // center if (_scale_w < _scale_h) { dst_box.top = padding_h / 2; if (dst_box.top % 2 != 0) { dst_box.top -= dst_box.top % 2; if (dst_box.top < 0) { dst_box.top = 0; } } dst_box.bottom = dst_box.top + resize_h - 1; _top_offset = dst_box.top; } else { dst_box.left = padding_w / 2; if (dst_box.left % 2 != 0) { dst_box.left -= dst_box.left % 2; if (dst_box.left < 0) { dst_box.left = 0; } } dst_box.right = dst_box.left + resize_w - 1; _left_offset = dst_box.left; } // LOGI("scale=%f dst_box=(%d %d %d %d) allow_slight_change=%d _left_offset=%d _top_offset=%d padding_w=%d padding_h=%d\n", // scale, dst_box.left, dst_box.top, dst_box.right, dst_box.bottom, allow_slight_change, // _left_offset, _top_offset, padding_w, padding_h); //set offset and scale if(letterbox != NULL){ letterbox->scale = scale; letterbox->x_pad = _left_offset; letterbox->y_pad = _top_offset; } // alloc memory buffer for dst image, // remember to free if (dst_image->virt_addr == NULL && dst_image->fd <= 0) { int dst_size = get_image_size(dst_image); dst_image->virt_addr = (uint8_t *)malloc(dst_size); if (dst_image->virt_addr == NULL) { // LOGE("malloc size %d error\n", dst_size); return -1; } } ret = convert_image(src_image, dst_image, &src_box, &dst_box, color); return ret; }这是对应的函数

LunaKnight
  • 粉丝: 44
上传资源 快速赚钱