将已解码的jpeg绘画到开罗表面

问题描述 投票:4回答:2

我正在尝试将解码的jpeg绘制到开罗表面上...但是我有点卡住,我不知道如何从[

前进
import cairo
import Image

path_to_jpeg = "/home/seif/Pictures/prw.jpg"
surface = cairo.PDFSurface ("out.pdf", 1000, 1000)
ctx = cairo.Context (surface)

image = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1000, 1000)
dt = Image.open(path_to_jpeg)
dimage = dt.load()

任何帮助将不胜感激...

python jpeg decode cairo
2个回答
3
投票

这应该可以解决问题。首先必须将图像转换为png,这似乎是创建表面的唯一格式。多数民众赞成在下面的代码的大部分。我建议您查看this question,它对我创建下面的代码有很大帮助。

import Image, StringIO
from cairo import PDFSurface, Context, ImageSurface

pdf = PDFSurface("out.pdf", 1000, 1000)
cr = Context(pdf)
im = Image.open("/home/seif/Pictures/prw.jpg")
buffer = StringIO.StringIO()
im.save(buffer, format="PNG")
buffer.seek(0)
cr.save()
cr.set_source_surface(ImageSurface.create_from_png(buffer))
cr.paint()

2
投票

如果使用cairocffi而不是pycairo(该API兼容),则cairocffi.pixbuf模块可与GDK-PixBuf集成以将各种图像格式加载到cairo中。

https://cairocffi.readthedocs.io/en/stable/pixbuf.html

示例:

from cairocffi import ImageSurface, pixbuf

def get_image(image_data: bytes) -> ImageSurface:
    return pixbuf.decode_to_image_surface(image_data)[0]

def load_image(image_file_path: str) -> ImageSurface:
    with open(str(image_file_path), 'rb') as file:
        return get_image(file.read())
© www.soinside.com 2019 - 2024. All rights reserved.