• python 对图片增加边框,logo贴图,获取图片exif参数,填写图片文本内容


    完整代码

    1. # 找到个可以下载免费字体的网站https://font.chi删除我naz.com/mi删除我anfei.html
    2. from PIL import Image, ImageDraw, ImageFont
    3. import exifread
    4. def photo_exif(image_path):
    5. f = open(image_path, 'rb')
    6. tags = exifread.process_file(f)
    7. # 打印所有照片信息,会以键值对的方法保存
    8. # for tag in tags.keys():
    9. # print("Key: {0}, value {1}".format(tag, tags[tag]))
    10. # print(str(tags['EXIF FocalLength']) + 'mm', tags['EXIF ExposureTime'], 'ISO' + str(tags['EXIF ISOSpeedRatings']))
    11. return tags
    12. def add_logo_with_text(image_path, logo_path, logo_size, text1, text2, text3, font_path, font_size, font_color, border_size,
    13. border_color, output_path):
    14. # 打开原始图片
    15. image = Image.open(image_path).convert("RGB")
    16. width, height = image.size
    17. # 计算边框区域大小和位置
    18. font = ImageFont.truetype(font_path, font_size)
    19. text1_width, text1_height = font.getsize(text1)
    20. text2_width, text2_height = font.getsize(text2)
    21. text3_width, text3_height = font.getsize(text3)
    22. text_width = max(text1_width, text2_width, text3_width)
    23. text_height = text1_height + text2_height + text3_height
    24. border_width = logo_size[0] + text_width + border_size * 3
    25. border_height = max(logo_size[1], text_height) + border_size * 2
    26. border_position = ((width - border_width) // 2, height)
    27. # 打开logo图片并调整大小
    28. logo = Image.open(logo_path).resize(logo_size, Image.ANTIALIAS)
    29. # 创建新的图片
    30. new_width = width
    31. new_height = height + border_height
    32. new_image = Image.new("RGB", (new_width, new_height), "white")
    33. # 将原始图片复制到新图片的顶部
    34. new_image.paste(image, (0, 0, width, height))
    35. # 在新图片上绘制边框
    36. draw = ImageDraw.Draw(new_image)
    37. border_rect = (border_position[0], height, border_position[0] + border_width, height + border_height)
    38. draw.rectangle(border_rect, fill=None, outline=border_color, width=border_size)
    39. # 在边框区域内绘制logo图片
    40. logo_position = (border_position[0] + border_size, height + (border_height - logo_size[1]) // 2)
    41. new_image.paste(logo, logo_position)
    42. # 在边框区域内绘制文本
    43. text1_position = (border_position[0] + border_size * 2 + logo_size[0], height + (border_height - text_height) // 2)
    44. text2_position = (border_position[0] + border_size * 2 + logo_size[0], text1_position[1] + text1_height)
    45. text3_position = (border_position[0] + border_size * 2 + logo_size[0], text2_position[1] + text2_height)
    46. draw.text(text1_position, text1, font=font, fill=font_color)
    47. draw.text(text2_position, text2, font=font, fill=font_color)
    48. draw.text(text3_position, text3, font=font, fill=font_color)
    49. # 保存合成后的图片
    50. new_image.save(output_path)
    51. # 示例用法
    52. # 照片路径
    53. image_path = "DSC_1966.jpg"
    54. # logo图片路径
    55. logo_path = "2.png"
    56. # logo图片大小
    57. logo_size = (255, 255)
    58. # 图片信息
    59. tags = photo_exif(image_path)
    60. text1 = "Power For."+str(tags['Image Model'])+" "+"FL."+str(tags['EXIF FocalLength'])+"mm"+" "+"EB."+str(tags['EXIF ExposureTime'])+" "+"ISO."+str(tags['EXIF ISOSpeedRatings'])+" "+"WL."+str(tags['EXIF ExifImageWidth'])+" x "+str(tags['EXIF ExifImageLength'])
    61. text2 = "DtO." + str(tags['EXIF DateTimeOriginal']) + " " + "By.林俊杰裤子掉了"
    62. text3 = "尼康, 感动常在 ╰( ̄▽ ̄)╭"
    63. # 字体路径
    64. font_path = "siyuanyuanti.ttf"
    65. font_size = 55
    66. font_color = (0, 0, 0) # 黑色
    67. border_size = 55
    68. border_color = (255, 255, 255) # 白色
    69. # 输出照片 .后缀为png为无损图片 ,jpg为压缩后的图片
    70. output_path = "output_image.png"
    71. add_logo_with_text(image_path, logo_path, logo_size, text1, text2, text3, font_path, font_size, font_color, border_size,
    72. border_color, output_path)
    73. print("图片已保存至:", output_path)

    输出结果 

     效果图

    可自行写成tk界面化选择图片处理,及处理多张图片的功能

  • 相关阅读:
    [1Panel]开源,现代化,新一代的 Linux 服务器运维管理面板
    flutter3-weos手机OS系统|Flutter3.22+Getx仿ios桌面管理OA应用
    【Leetcode】120.三角形最小路径和
    【23真题】很少见!第6题有点新颖!
    JVM Heap Memory
    【数学建模】传染病模型笔记
    LeetCode笔记:Biweekly Contest 91
    Linux网络:HTTP 与 HTTPS
    Atlas2.2.0编译、安装及使用(集成ElasticSearch,导入Hive数据)
    1044 - Access denied for user ‘root‘@‘%‘ to database
  • 原文地址:https://blog.csdn.net/qq_26086231/article/details/133824670