一种新的Python图像生成处理库Snowy


#1

Python开发者肯定解决过图片处理问题吧,在Web应用场景中,用户上传图片后,服务端往往需要对图片缩小、切割等等操作,一定用过PILPillow吧。最近发现一个叫做Snowy开源库,感觉功能很强大。

Snowy is a tiny module for manipulating and generating images.

有这些特点:

  • Small and flat API (free functions only).
  • Written purely in Python 3.
  • Accelerated with numba.
  • Configurable boundaries (wrap modes).

特别是:Snowy does not define a special class for images. Instead, images are always three-dimensional numpy arrays in row-major order.
原因呢: Snowy’s algorithms require images to be row-major three-dimensional float64 numpy arrays, with color channels living in the trailing dimension.

示例

模糊化处理

import snowy

source = snowy.load('poodle.png')
source = snowy.resize(source, height=200)
blurry = snowy.blur(source, radius=4.0)
snowy.save(snowy.hstack([source, blurry]), 'diptych.png')

diptych

先放大再虚化

parrot = snowy.load('parrot.png')
height, width = parrot.shape[:2]
nearest = snowy.resize(parrot, width * 6, filter=snowy.NEAREST) 
mitchell = snowy.resize(parrot, width * 6)
snowy.show(snowy.hstack([nearest, mitchell]))

diptych-parrot

旋转和拼接

gibbons = snowy.load('gibbons.jpg')
rotated = snowy.rotate(gibbons, 180)
flipped = snowy.vflip(gibbons)
triptych = snowy.hstack([gibbons, rotated, flipped],
    border_width=4, border_value=[0.5,0,0])

切割

    sunrise = snowy.load('sunrise.exr')
    cropped_sunrise = sunrise[:100,:,:]
    snowy.show(cropped_sunrise / 50.0) # darken the image

阴影

    # Extend the 100x100 snowflake image on 4 sides to give room for blur.
    shadow = np.zeros([150, 150, 4])
    shadow[25:-25,25:-25,:] = icon

    # Invert the colors but not the alpha.
    white = shadow.copy()
    white[:,:,:3] = 1.0 - white[:,:,:3]

    # Blur the shadow, then "strengthen" it.
    shadow = snowy.blur(shadow, radius=10.0)
    shadow = snowy.compose(shadow, shadow)
    shadow = snowy.compose(shadow, shadow)
    shadow = snowy.compose(shadow, shadow)

    # Compose the white flake onto its shadow.
    dropshadow = snowy.compose(shadow, white)

dropshadow

是不是很强大,不过似乎得掌握点numpy知识。