<div style='--en-codeblock:true;--en-blockId:gXFFS2Sgu6D;--en-meta:{"title":"","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'>pip3 install pillow
</div>
<div style='--en-codeblock:true;--en-blockId:fbc_Se5xhMz;--en-meta:{"title":"","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'>from PIL import Image
# 打开一张图片
img = Image.open("example.jpg")
# 显示图片(需要在 Jupyter notebook 或者有 GUI 支持的环境中)
img.show()
</div>
<div style='--en-codeblock:true;--en-blockId:zge6QallfbW;--en-meta:{"title":"","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'># 将图片以另一种格式保存(例如转换为 JPEG 格式)
img.save("example_converted.jpg", "JPEG")
</div>
<div style='--en-codeblock:true;--en-blockId:kpc6RarZ0Ny;--en-meta:{"title":"","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'>width, height = img.size
print(f"Image dimensions: {width}x{height}")
</div>
<div style='--en-codeblock:true;--en-blockId:_sBBRaDKXIt;--en-meta:{"title":"","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'># 缩放图像至指定大小
resized_img = img.resize((new_width, new_height))
resized_img.save("example_resized.jpg")
</div>
<div style='--en-codeblock:true;--en-blockId:WowxROzxw_Z;--en-meta:{"title":"","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'># 顺时针旋转图像 40 度
rotated_img = img.rotate(40)
rotated_img.save("example_rotated.jpg")
</div>
<div style='--en-codeblock:true;--en-blockId:9386QCELp6O;--en-meta:{"title":"","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'># 裁剪图像的一部分,前面两个为左上角坐标,后面两个是右下角坐标
box = (left, upper, right, lower)
cropped_img = img.crop(box)
cropped_img.save("example_cropped.jpg")
</div>
<div style='--en-codeblock:true;--en-blockId:95BySaTeypg;--en-meta:{"title":"","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'># 将图像转换为灰度图像
gray_img = img.convert("L")
gray_img.save("example_gray.jpg")
</div>
<div style='--en-codeblock:true;--en-blockId:vJkQQGEVH3y;--en-meta:{"title":"","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'># 应用 PIL 内置滤镜
filtered_img = img.filter(ImageFilter.BLUR)
filtered_img.save("example_blurred.jpg")
</div>
<div style='--en-codeblock:true;--en-blockId:3dsiT-hDxkz;--en-meta:{"title":"","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'>import os
for filename in os.listdir("images_folder"):
if filename.endswith(".png"):
img = Image.open(os.path.join("images_folder", filename))
new_filename = filename.replace(".png", ".jpg")
img.save(os.path.join("output_folder", new_filename), "JPEG")
</div>
<div style='--en-codeblock:true;--en-blockId:0aJITex3yqv;--en-meta:{"title":"","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'># 获取图像的像素数据并进行修改
pixels = img.load()
for x in range(img.width):
for y in range(img.height):
r, g, b = pixels[x, y]
# 假设将所有像素颜色变淡一半
pixels[x, y] = (r // 2, g // 2, b // 2)
# 修改后的像素存回原图
img.save("example_modified.jpg")
</div>
<div style='--en-codeblock:true;--en-blockId:ibO2Re09MIh;--en-meta:{"title":"","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'>from PIL import Image
# 打开图像文件
image = Image.open("image.jpg")
# 生成缩略图
thumbnail_size = (200, 200)
image.thumbnail(thumbnail_size)
# 保存缩略图
image.save("thumbnail.jpg")</div>
<div style='--en-codeblock:true;--en-blockId:wgf4RKLq0La;--en-meta:{"title":"","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'>from PIL import Image, ImageDraw, ImageFont
# 打开图像文件
image = Image.open("image.jpg")
# 创建绘图对象
draw = ImageDraw.Draw(image)
# 添加水印文本
text = "Watermark"
font = ImageFont.truetype("arial.ttf", 36)
text_size = draw.textsize(text, font)
text_position = (image.width - text_size[0], image.height - text_size[1])
draw.text(text_position, text, fill=(255, 255, 255), font=font)
# 保存带水印的图像
image.save("watermarked_image.jpg")</div>
💬 评论交流
这个技巧很实用,感谢分享!