祂的诞生
- 编辑csdn文章时发现上传的图片尺寸不一样的话,直接贴在文章里会导致参差不齐,严重影响美观,而且图片宽度比显示器宽度大的话会直接全屏宽显示,有点影响阅读性,再有就是白底的图片会与文章底色重合,会区分不开图片和文字,所以········
效果展示
实现原理
- 使用python脚本将图片批量修改为相同的尺寸,增加投影样式,保存为png格式,画布宽为1920,画布改为透明状态。
核心代码
- 主要实现方式如下
def process_image(input_path, output_folder): ··· # 将图片宽度改为1200,高度随比例改变 target_width = 1200 w_percent = target_width / float(img.size[0]) target_height = int(float(img.size[1]) * float(w_percent)) img = img.resize((target_width, target_height), Image.LANCZOS) # 或者使用 Image.BICUBIC # 将画布大小改为:宽度=图片宽度+720 高度=图片高度+150 canvas_width = target_width + 720 canvas_height = target_height + 150 canvas = Image.new('RGBA', (canvas_width, canvas_height), (0, 0, 0, 0)) # 计算图片在画布中的位置 img_position = ((canvas_width - target_width) // 2, (canvas_height - target_height) // 2) # 移动img,相对于img2,x和y分别增加5 img_new_position = (img_position[0] + 5, img_position[1] + 5) # 创建一张与原图相同的图片,作为img2 img2 = img.copy() # 创建一个带有透明度通道的版本 img_with_alpha = img.convert("RGBA") # 设置透明度(这里设置为半透明,你可以根据需要调整) alpha = 80 # 0 表示完全透明,255 表示完全不透明 img_with_alpha.putalpha(alpha) # 将图像的亮度调整为最低 enhancer = ImageEnhance.Brightness(img_with_alpha) dark_img = enhancer.enhance(0.0) # 0.0 表示最低亮度 # 将图片合成到画布中间位置 canvas.paste(dark_img, img_new_position) # 添加高斯模糊效果 canvas = canvas.filter(ImageFilter.GaussianBlur(radius=10)) canvas.paste(img2, img_position)
脚本自取处
-
点我点我(GitHub)
-
点我点我(123网盘)
-
使用说明:
1、python官网下载python3并安装
2、打开cmd使用命令:pip install pillow
3、将脚本放到图片所在目录,双击运行即可支持的格式:‘.png’, ‘.jpg’, ‘.jpeg’