2 years ago

#56474

test-img

Dooly

Pycairo and opencv-python

from PIL import Image
import numpy as np
from cv2 import VideoWriter, VideoWriter_fourcc
import math
import cairo
from tqdm import tqdm
import time

def DrawPng():
    WIDTH, HEIGHT = 3840, 2160

    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
    ctx = cairo.Context(surface)

    ctx.scale(WIDTH, HEIGHT)  # Normalizing the canvas

    pat = cairo.LinearGradient(0.0, 0.0, 0.0, 1.0)
    pat.add_color_stop_rgba(1, 0.7, 0, 0, 0.5)  # First stop, 50% opacity
    pat.add_color_stop_rgba(0, 0.9, 0.7, 0.2, 1)  # Last stop, 100% opacity

    ctx.rectangle(0, 0, 1, 1)  # Rectangle(x0, y0, x1, y1)
    ctx.set_source(pat)
    ctx.fill()

    ctx.translate(0.1, 0.1)  # Changing the current transformation matrix

    ctx.move_to(0, 0)
    # Arc(cx, cy, radius, start_angle, stop_angle)
    ctx.arc(0.2, 0.1, 0.1, -math.pi / 2, 0)
    ctx.line_to(0.5, 0.1)  # Line to (x,y)
    # Curve(x1, y1, x2, y2, x3, y3)
    ctx.curve_to(0.5, 0.2, 0.5, 0.4, 0.2, 0.8)
    ctx.close_path()

    ctx.set_source_rgb(0.3, 0.2, 0.5)  # Solid color
    ctx.set_line_width(0.02)
    ctx.stroke()

    surface.write_to_png("./Image/example.png")  # Output to PNG

DrawPng()

img = Image.open("./Image/example.png")
imgArray = np.array(img)

width = img.size[0]
height = img.size[1]
FPS = 30
second = 5
fourcc = VideoWriter_fourcc(*'MP42')
video = VideoWriter('./Video/cairo.avi', fourcc, float(FPS), (width, height))


for i in tqdm(range(FPS*second)):
    frame = imgArray
    video.write(frame)

img.close()
video.release()

I'm planning to making video using opencv and Pycairo. I created a png file using Pycairo and the png file created by Pycairo is fine. But

for i in tqdm(range(FPS*second)):
    frame = imgArray
    video.write(frame)

above code does not worked. Precisely, for loop worked, but result is not what I expected.

When I use another png image(downloaded from Internet) this code works perfectly. and I convert png image to jpeg, it works perfectly. I can't understand this situation...

python

opencv

pycairo

0 Answers

Your Answer

Accepted video resources