Python Image Distortion

I am trying to apply a ripple effect to an image in python. I found Pillow im.transform (im.size, Image.MESH, .... is this possible? Maybe I need to upload the image using numpy and apply the algorithm. I also found this: http://www.pygame.org /project-Water+Ripples-1239-.html

ripple

another way manually, but I don't know any algorithm. this is my beginning. he does not do anything...

    #!/usr/bin/env python3

    from PIL import Image
    import sys
    import numpy
    import math

    im = Image.open(sys.argv[1])
    im.show()

    matrix = numpy.asarray(im)
    width = im.size[0]
    height = im.size[1]
    amplitude = ? # parameters
    frequency = ?
    matrix_dest = numpy.zeros((im.size[0],im.size[1],3))

    for x in range(0, width):
        for y in range(0, height):
            pass # รง_รง

    im2 = Image.fromarray(numpy.uint8(matrix_dest))
    im2.show()

EDIT:

( . , , ), scipi matplotlib.. , , . , R, G, B, . palettize , .

(Btw 3D-.)

im = Image.open(sys.argv[1])
im.show()

m = numpy.asarray(im)
m2 = numpy.zeros((im.size[0],im.size[1],3))
width = im.size[0]
height = im.size[1]

A = m.shape[0] / 3.0
w = 1.0 / m.shape[1]

shift = lambda x: A * numpy.sin(2.0*numpy.pi*x * w)

for i in range(m.shape[0]):
    print(int(shift(i)))
    m2[:,i] = numpy.roll(m[:,i], int(shift(i)))

im2 = Image.fromarray(numpy.uint8(m2))
im2.show()
+3
3

np.roll .

from scipy.misc import lena
import numpy as np
import matplotlib.pyplot as plt

img = lena()

A = img.shape[0] / 3.0
w = 2.0 / img.shape[1]

shift = lambda x: A * np.sin(2.0*np.pi*x * w)

for i in range(img.shape[0]):
    img[:,i] = np.roll(img[:,i], int(shift(i)))

plt.imshow(img, cmap=plt.cm.gray)
plt.show()

enter image description here

+7

- :

# import scipy
# import numpy as np
for x in range(cols):
    column = im[:,x]
    y = np.floor(sin(x)*10)+10
    kernel = np.zeros((20,1))
    kernel[y] = 1
    scipy.ndimage.filters.convolve(col,kernel,'nearest')

, . , . , .

+4

I had a similar problem when sometimes the colors seem to get messed up (getting some weird red lines) after applying sin while trying to suggest the suggested solutions here. Failed to solve the problem.

I understand that the original poster does not want any more dependencies, if possible, but for those who are not limited, here is an alternative sample solution provided by scikit docs:

http://scikit-image.org/docs/dev/auto_examples/transform/plot_piecewise_affine.html#sphx-glr-auto-examples-transform-plot-piecewise-affine-py

Copy from document above:

import numpy as np
import matplotlib.pyplot as plt
from skimage.transform import PiecewiseAffineTransform, warp
from skimage import data


image = data.astronaut()
rows, cols = image.shape[0], image.shape[1]

src_cols = np.linspace(0, cols, 20)
src_rows = np.linspace(0, rows, 10)
src_rows, src_cols = np.meshgrid(src_rows, src_cols)
src = np.dstack([src_cols.flat, src_rows.flat])[0]

# add sinusoidal oscillation to row coordinates
dst_rows = src[:, 1] - np.sin(np.linspace(0, 3 * np.pi, src.shape[0])) * 50
dst_cols = src[:, 0]
dst_rows *= 1.5
dst_rows -= 1.5 * 50
dst = np.vstack([dst_cols, dst_rows]).T


tform = PiecewiseAffineTransform()
tform.estimate(src, dst)

out_rows = image.shape[0] - 1.5 * 50
out_cols = cols
out = warp(image, tform, output_shape=(out_rows, out_cols))

fig, ax = plt.subplots()
ax.imshow(out)
ax.plot(tform.inverse(src)[:, 0], tform.inverse(src)[:, 1], '.b')
ax.axis((0, out_cols, out_rows, 0))
plt.show()
0
source

All Articles