[Python] Straighten the object using python

Discussão em 'Python' iniciado por Stack, Agosto 15, 2025.

  1. Stack

    Stack Membro Participativo

    I need to warp and Liquify a lot of fishing baits in Photoshop. Super time consuming. Was thinking that it might be possible to do it with python. But no idea where to start.

    [​IMG]

    [​IMG]

    I have this version from ChatGPT:

    import cv2
    import numpy as np
    from skimage.morphology import skeletonize

    def straighten_bait(img_path, output_path=None):
    # Load image
    img = cv2.imread(img_path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

    # Skeletonize
    sk = skeletonize(mask // 255).astype(np.uint8)
    ys, xs = np.where(sk > 0)
    coords = np.array(list(zip(xs, ys)))

    # Sort skeleton from top to bottom
    coords = coords[np.argsort(coords[:,1])]

    # Prepare remap coordinates
    map_x = np.zeros((len(coords), img.shape[1]), dtype=np.float32)
    map_y = np.zeros_like(map_x)

    for i, (cx, cy) in enumerate(coords):
    # Map each column of the straightened image to the skeleton row
    map_x[i, :] = np.clip(np.arange(img.shape[1]), 0, img.shape[1]-1)
    map_y[i, :] = np.clip(cy, 0, img.shape[0]-1)

    # Remap to straighten
    straight = cv2.remap(img, map_x, map_y, interpolation=cv2.INTER_CUBIC,
    borderMode=cv2.BORDER_CONSTANT, borderValue=(255,255,255))

    if output_path is None:
    output_path = img_path.replace(".jpg", "_straight.png")
    cv2.imwrite(output_path, straight)
    print("Saved:", output_path)
    return output_path

    # Example usage
    straighten_bait("/Users/Desktop/curved.jpg")



    but the bate is thin and not very straight. it needs to keep it lengh and width.

    aprecciate any help

    Continue reading...

Compartilhe esta Página