【问题标题】:How would I warp text around an image's edges?我将如何扭曲图像边缘的文本?
【发布时间】:2022-01-02 12:15:54
【问题描述】:

我正在尝试创建一个边缘替换为文本的图像,类似于This Youtube video thumbnail,但来自源图像。我已经使用 OpenCV 来获取带有边缘的源图像的版本,并使用 Pillow 来实际编写文本,但是当涉及到实际自动操作文本以适应边缘时,我不确定从哪里开始。我到目前为止的代码是:

import cv2 as cv
from matplotlib import pyplot as plt
from PIL import Image, ImageFont, ImageDraw, ImageShow

font = ImageFont.truetype(r"C:\Users\X\Downloads\Montserrat\Montserrat-Light.ttf", 12)
text = ["text", "other text"]

img = cv.imread(r"C:\Users\X\Pictures\picture.jpg",0)
edges = cv.Canny(img,100,200)

img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
im_pil = Image.fromarray(edges)

此代码仅用于边缘检测并将检测到的边缘移动到 Pillow。

请帮忙

【问题讨论】:

  • 定期绘制文本,然后应用极坐标扭曲。 OpenCV 拥有这一切。

标签: python image opencv


【解决方案1】:

我不确定“边缘”是从哪里来的精明边缘检测器。

然而,圆形文本换行可以在使用 ImageMagick 的 Python/Wand 中非常简单地完成。或者可以在 Python/OpenCV 中使用 cv2.remap 和自定义转换映射来做到这一点。

输入:

1.蟒蛇魔杖

(根据输入大小自动确定输出大小)

from wand.image import Image
from wand.font import Font
from wand.display import display

with Image(filename='some_text.png') as img:
    img.background_color = 'white'
    img.virtual_pixel = 'white'
    # 360 degree arc, rotated 0 degrees
    img.distort('arc', (360,0))
    img.save(filename='some_text_arc.png')
    img.format = 'png'
    display(img)

结果:

2。 Python/OpenCV

import numpy as np
import cv2
import math

# read input
img = cv2.imread("some_text.png")
hin, win = img.shape[:2]
win2 = win / 2

# specify desired square output dimensions and center
hout = 100
wout = 100
xcent = wout / 2
ycent = hout / 2
hwout = max(hout,wout)
hwout2 = hwout / 2

# set up the x and y maps as float32
map_x = np.zeros((hout, wout), np.float32)
map_y = np.zeros((hout, wout), np.float32)

# create map with the arc distortion formula --- angle and radius
for y in range(hout):
    Y = (y - ycent)
    for x in range(wout):
        X = (x - xcent)
        XX = (math.atan2(Y,X)+math.pi/2)/(2*math.pi)
        XX = XX - int(XX+0.5)
        XX = XX * win + win2
        map_x[y, x] = XX
        map_y[y, x] = hwout2 - math.hypot(X,Y)

# do the remap  this is where the magic happens
result = cv2.remap(img, map_x, map_y, cv2.INTER_CUBIC, borderMode = cv2.BORDER_CONSTANT, borderValue=(255,255,255))

# save results
cv2.imwrite("some_text_arc.jpg", result)

# display images
cv2.imshow('img', img)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

【讨论】:

    【解决方案2】:

    OpenCV 和 PIL 都没有办法做到这一点,但您可以使用 ImageMagick。 How to warp an image to take shape of path with python?

    【讨论】:

    猜你喜欢
    • 2018-07-02
    • 1970-01-01
    • 2021-05-17
    • 2012-12-05
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多