Python 图片格式转换

图片格式转换可以利用各种软件

今天介绍一下如何使用 Python 实现各种图片格式的转换

1. SVG 转其他格式

读取 SVG 格式文件,需要安装 svglib

pip install svglib

SVG 图片保存为其他格式图片需要用到 reportlab

pip install reportlab
1.1 读取 SVG 图片
from svglib.svglib import svg2rlg

drawing = svg2rlg("circos.svg")
1.2 SVG 转 PNG
from reportlab.graphics import renderPM
from svglib.svglib import svg2rlg

drawing = svg2rlg("circos.svg")
renderPM.drawToFile(drawing, "circos.png", fmt="PNG")
1.3 SVG 转 PDF
from reportlab.graphics import renderPDF
from svglib.svglib import svg2rlg

drawing = svg2rlg("circos.svg")
renderPDF.drawToFile(drawing, "circos.pdf")
1.4 SVG 转其他格式
renderPM.drawToFile(
    d,
    fn,
    fmt='GIF',
    dpi=72,
    bg=16777215,
    configPIL=None,
    showBoundary=<reportlab.rl_config._unset_ object at 0x106458070>,
)
可以通过设置 fmt 来选择输出格式,
fmt 支持:
'GIF', 'TIFF','TIFFP','TIFFL','TIF','TIFF1' 'PNG','BMP', 'PPM', 'JPG','JPEG'

2. PNG 转其他格式

读取 PNG 图片 使用到了 Pillow

pip install Pillow
2.1 PNG 转 JPG
from PIL import Image

img = Image.open('circos.png')
img.save(r'pil_circos.jpg')
2.2 PNG 转 SVG
def toSVG(infile, outfile):
    image = Image.open(infile).convert('RGBA')
    data = image.load()
    width, height = image.size
    out = open(outfile, "w")
    out.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
    out.write('<svg id="svg2" xmlns="http://www.w3.org/2000/svg" version="1.1" \
                width="%(x)i" height="%(y)i" viewBox="0 0 %(x)i %(y)i">\n' % \
              {'x': width, 'y': height})
    
    for y in range(height):
        for x in range(width):
            rgba = data[x, y]
            rgb = '#%02x%02x%02x' % rgba[:3]
            if rgba[3] > 0:
                out.write('<rect width="1" height="1" x="%i" y="%i" fill="%s" \
                    fill-opacity="%.2f" />\n' % (x, y, rgb, rgba[3]/255.0))
    out.write('</svg>\n')
    out.close()
    
toSVG('heart.jpeg', 'heart.svg')
2.3 PNG 转 PDF
from PIL import Image

img = Image.open('circos.png')
img.convert('RGB')
img.save('pil_circos.pdf')
2.4 多个 PNG 合并为 PDF
path = 'png file path'
img_list = [Image.open(os.path.join(path, f)).convert('RGB') for f in os.listdir(path) 
         if f.lower().endswith('png')]
img = img_list.pop(0)
img.save('pil_circos.pdf', resolution=10.0, save_all=True, append_images=img_list)

这种方法会损失分辨率

3. JPG

JPG 或者说 JPEG 的转换与 PNG 格式相同,上面的代码可以复用。

4. 合并多个 PDF 文件

使用到 PyPDF2

pip install PyPDF2

使用

from PyPDF2 import PdfFileMerger
import os

path = 'path of pdf file'
pdf_list = [f for f in os.listdir(path) if f.lower().endswith('pdf')]

pdf_merge = PdfFileMerger()
for f in pdf_list:
    pdf_merge.append(f)

pdf_merge.write('merge_pdf.pdf')

可以将每张图片先转换为单个 PDF 文件,然后合并 PDF 文件,避免分辨率损失。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

名本无名

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值