0

I am new to python. I would like to warp an image such that it fills the closed path (closed path and imported image is shown in the image below). The given closed path has a shape of truncated pie. The outer diameter of truncated pie is 15 units and inner diameter of it is 5 units, and the angle of that pie is 60 degrees. I've added the external shape as an example. Is it possible to fill the image within any kind of closed path. Can you please suggest python libraries to be used or it'd be great if you can share some code. I was also trying to work with corner detection algorithms and making them to stick with map function, but I can't. Two images

The image I require

Prem Rishi
  • 19
  • 7
  • what did you find with Google ? – furas Apr 12 '20 at 19:03
  • i couldn't find anything for this. The only thing which could be useful is `cv2.remap` like in [How to warp an image using deformed mesh](https://stackoverflow.com/questions/53907633/how-to-warp-an-image-using-deformed-mesh). – furas Apr 12 '20 at 19:34
  • Other ideas: for deformation of top and bottom sides you could use sinus (or cosinus) to calculate new positions for pixels. And maybe later you could try to some deformation for left and right side. Eventually maybe there is function in OpenGL for mapping on this type of shape - like mapping on sphere in [Texture wrapping an entire sphere in PyOpenGL](https://stackoverflow.com/questions/47913978/texture-wrapping-an-entire-sphere-in-pyopengl) – furas Apr 12 '20 at 19:34

1 Answers1

6

You can do that in Python Wand, which is based upon Imagemagick.

Input:

enter image description here

from wand.image import Image
from wand.display import display    

with Image(filename='taj_mahal.jpg') as img:
    print(img.size)
    img.virtual_pixel = 'white'
    img.distort('arc', (60,180))
    img.save(filename='taj_mahal_arc.jpg')
    display(img)


Result:

enter image description here

The first argument in the arc is the arc angular spread. The second argument is the rotation on the circle where the arc is generated. See https://imagemagick.org/Usage/distorts/#arc

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Thanks fmw42 , actually I added the arc image just for an example. is it possible to make the image to take the shape of any closed path(complex paths too). – Prem Rishi Apr 13 '20 at 16:03
  • No, not that I know, unless you can set up a grid and pick corresponding control points. Then you can warp via a polynomial warp process. Imagemagick and Python Wand support that. – fmw42 Apr 13 '20 at 18:35
  • I literally don't have any idea about polynomial wrap and other technical words, sorry if I'm irritating. I kindly request you to help me by sharing code snippets if it is merely possible with the "Imagemagick and Python Wand". I've been buzzing around this problem for more than 2 weeks. – Prem Rishi Apr 14 '20 at 08:48
  • See https://imagemagick.org/Usage/distorts/#polynomial. Also look that up in image processing text books. Or Google it. – fmw42 Apr 14 '20 at 17:26