linux粘贴文本格式错乱:
vi/vim
:set paste
然后再 insert ,粘贴
centos安装pillow图像处理库:
引用:https://blog.csdn.net/newbieLCQ/article/details/125345335
linux赋予执行权限:
# 创建文件
touch comperss.py
# 赋予权限
chmod +x compress.py
png使用pillow报错,不知道为什么,, 后面使用的pngquant。。
# 安装pngquant
brew install pngquant
# 测试图片压缩,, quality 参数格式是min-max ,, 其中min:是最小质量值,max是最大质量值,,较低的min会更大的压缩和更低的质量,较高的max会保留更多的图像质量
pngquant --quality=10-30 --output output.png --force --verbose 222.png
import os
import subprocess
from PIL import Image, ImageFile
# 允许加载截断的图像文件
ImageFile.LOAD_TRUNCATED_IMAGES = True
def compress_image(input_path, output_path, target_size_kb, min_quality=1, max_quality=95):
target_size_bytes = target_size_kb * 1024
with Image.open(input_path) as img:
img_format = img.format
low = min_quality
high = max_quality
best_quality = low
while low <= high:
mid = (low + high) // 2
img.save(output_path, format=img_format, quality=mid, optimize=True)
output_size = os.path.getsize(output_path)
if output_size <= target_size_bytes:
best_quality = mid
low = mid + 1
else:
high = mid - 1
img.save(output_path, format=img_format, quality=best_quality, optimize=True)
return best_quality
def compress_image_in_directory(directory, target_size_kb=50):
for root, _, files in os.walk(directory):
for filename in files:
if filename.lower().endswith(('.jpg', '.jpeg', '.bmp', '.gif')):
input_path = os.path.join(root, filename)
output_path = os.path.join(root, f"{filename}")
final_quality = compress_image(input_path, output_path, target_size_kb)
print(f"{filename}_final quality is {final_quality}")
elif filename.lower().endswith('.png'):
input_path = os.path.join(root, filename)
output_path = os.path.join(root, f"f_{filename}")
final_quality = compress_image_png(input_path, output_path, target_size_kb)
print(f"{filename}_final quality is {final_quality}")
def compress_image_png(input_path, output_path, target_size_kb=50, min_quality=1, max_quality=95):
target_size_bytes = target_size_kb * 1024
with Image.open(input_path) as img:
img_format = img.format
if img_format.lower() == 'png':
success = compress_png_with_pngquant(input_path, output_path, target_size_kb)
# if success:
# return "pngquant"
# else:
# for compress_level in range(10, 0, -1):
# img.save(output_path, format=img_format, compress_level=compress_level)
# output_size = os.path.getsize(output_path)
# if output_size <= target_size_bytes:
# return compress_level
# img.save(output_path, format=img_format, optimize=True, compress_level=compress_level)
# return 1
return "success"
def compress_png_with_pngquant(input_path, output_path, target_size_kb):
target_size_bytes = target_size_kb * 1024
# quality设置压缩,, 为1:压缩到最小
command = ['pngquant', '--quality=1', '--output', output_path, '--force', '--', input_path]
try:
subprocess.run(command, check=True)
output_size = os.path.getsize(output_path)
return output_size <= target_size_bytes
except subprocess.CalledProcessError as e:
print(f"Error during pngquant compression: {e}")
return False
except FileNotFoundError as e:
print(f"pngquant not found: {e}")
return False
# 示例:指定目录路径
directory_path = '/Users/chenjie/code/python-learn/python410/img' # 替换为你的图片目录路径
compress_image_in_directory(directory_path)