Ross Wan's World!

Python, Ajax, PHP and Linux.

Posts Tagged ‘PIL’

The Python Challenge Lv.9

Posted by Ross Wan 于 2011/09/07

Lv.9

利用第8关得到的账号和密码成功进入第9关, 显示一幅河岸的图片, 图片上有若干黑点, 网页没有其它文字提示. 再看看网页的标题, “connect the dots”???难道是将图片上的黑点连接起来?

先别帮着动手, 看看网页的源代码吧, 毕竟以往的经验说明提示经常出现在网页源代码里:) 果然, 网页源代码的注释里出现两组数字—-first 和 second, 而且提示 “first+second”. 再回看标题”connect the dots”, 难道是将这些数字代表的坐标点连接起来?? 利用 PIL 的 ImageDraw 进行画图:

from PIL import Image, ImageDraw

first_points = (    146,399,163,403,170,393,169,391,166,386,170,381,170,371,170,355,169,346,167,335,170,329,170,320,170,310,171,301,173,290,178,289,182,287,188,286,190,286,192,291,194,296,195,305,194,307,191,312,190,316,190,321,192,331,193,338,196,341,197,346,199,352,198,360,197,366,197,373,196,380,197,383,196,387,192,389,191,392,190,396,189,400,194,401,201,402,208,403,213,402,216,401,219,397,219,393,216,390,215,385,215,379,213,373,213,365,212,360,210,353,210,347,212,338,213,329,214,319,215,311,215,306,216,296,218,290,221,283,225,282,233,284,238,287,243,290,250,291,255,294,261,293,265,291,271,291,273,289,278,287,279,285,281,280,284,278,284,276,287,277,289,283,291,286,294,291,296,295,299,300,301,304,304,320,305,327,306,332,307,341,306,349,303,354,301,364,301,371,297,375,292,384,291,386,302,393,324,391,333,387,328,375,329,367,329,353,330,341,331,328,336,319,338,310,341,304,341,285,341,278,343,269,344,262,346,259,346,251,349,259,349,264,349,273,349,280,349,288,349,295,349,298,354,293,356,286,354,279,352,268,352,257,351,249,350,234,351,211,352,197,354,185,353,171,351,154,348,147,342,137,339,132,330,122,327,120,314,116,304,117,293,118,284,118,281,122,275,128,265,129,257,131,244,133,239,134,228,136,221,137,214,138,209,135,201,132,192,130,184,131,175,129,170,131,159,134,157,134,160,130,170,125,176,114,176,102,173,103,172,108,171,111,163,115,156,116,149,117,142,116,136,115,129,115,124,115,120,115,115,117,113,120,109,122,102,122,100,121,95,121,89,115,87,110,82,109,84,118,89,123,93,129,100,130,108,132,110,133,110,136,107,138,105,140,95,138,86,141,79,149,77,155,81,162,90,165,97,167,99,171,109,171,107,161,111,156,113,170,115,185,118,208,117,223,121,239,128,251,133,259,136,266,139,276,143,290,148,310,151,332,155,348,156,353,153,366,149,379,147,394,146,399
)
second_points = (    156,141,165,135,169,131,176,130,187,134,191,140,191,146,186,150,179,155,175,157,168,157,163,157,159,157,158,164,159,175,159,181,157,191,154,197,153,205,153,210,152,212,147,215,146,218,143,220,132,220,125,217,119,209,116,196,115,185,114,172,114,167,112,161,109,165,107,170,99,171,97,167,89,164,81,162,77,155,81,148,87,140,96,138,105,141,110,136,111,126,113,129,118,117,128,114,137,115,146,114,155,115,158,121,157,128,156,134,157,136,156,136
)

if __name__ == '__main__':
    img = Image.new('RGBA', (500,500))
    draw = ImageDraw.Draw(img)
    draw.polygon(first_points)
    draw.polygon(second_points)
    img.save('good_ok.jpg')

打开生成的图片 good_ok.jpg, 明显看到那些点连接起来是一头牛, cow? 于是尝试打开http://www.pythonchallenge.com/pc/return/cow.html,显示:

hmm. it’s a male.

呵呵,原来是头公牛—-bull, 于是成功得到下一关的网址: http://www.pythonchallenge.com/pc/return/bull.html

Have fun~~

Advertisements

Posted in Python | Tagged: , , , | 1 Comment »

The Python Challenge Lv.7

Posted by Ross Wan 于 2011/08/31

Lv.7


打开网页,显示的是一张河岸的图片, 特别的是, 图片中央有一细条”灰色带”.将图片另存为”oxygen.png”, 用图片编辑器打开, 放大到最大的倍数.可以看到,那色带是一长方形的灰度色带(left: 0px, top: 43px, right: 607px, bottom: 51px), 它又是由大小为7px * 9px的小长方形灰度块(除了第1块是 5px * 9px)组成的. 灰度的值是0~255, 正好跟 Ascii 相对应, 思路出来了, 下面交给 Python 来处理:

from PIL import Image

if __name__ == '__main__':
    img = Image.open('oxygen.png')
    for p in range(4, 607, 7):
        print(chr(int(sum(img.getpixel((p,43))[:3])/3)), end='')
    print()


注意, 处理图片需要用到 PIL 库, Python 3的 Windows 编译版可以到以下网址下载: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil


运行结果:

smart guy, you made it. the next level is [105, 110, 116, 101, 103, 114, 105, 116, 121]


可知要对 “[105, 110, 116, 101, 103, 114, 105, 116, 121]”进行映射就得到答案了;现将上面的代码改写,一次性的得到答案:)

from PIL import Image
import re

if __name__ == '__main__':
    img = Image.open('oxygen.png')
    map_string = ''.join(map(lambda p: chr(int(sum(img.getpixel((p, 43))[:3])/3)), range(4, 607, 7)))
    #print(map_string)
    print('\nKey Words: ', end='')
    for i in re.finditer(r'\d+', map_string):
        print(chr(int(i.group())), end='')
    else:
        print()


显示结果:

Key Words: integrity

可知下一关的网址是: http://www.pythonchallenge.com/pc/def/integrity.html

Have fun :)

Posted in Python | Tagged: , , , | Leave a Comment »

PIL: 批量生成缩略图

Posted by Ross Wan 于 2011/08/29

很久之前, 在 Python 2.5 下写了一个基于 PIL 的批量生成缩略图的脚本, 现将其修改移植到 Python 3下, 其中作了不少修改和完善:

#!bin/pyhton3
# coding=utf8

import os, sys, getopt, functools
from PIL import Image

# 源图片路径
SRC = os.getcwd()
# 目标保存图片的路径
DEST = os.getcwd()
# 缩略图保存的文件夹名称, 最终缩略图的保存路径为 sys.path.join(DEST, THUMB_DIR).
THUMB_DIR = '缩略图'
# 缩放比例
RATE = 0.15
# 缩略图的命名, 在原文件名前加上 THUMB_PREFFIX, 如"thumb_"
THUMB_PREFFIX = ''
# 是否递归搜索子文件夹
IS_RECURSIVE = True

def coroutine(func):
    """ 用于协程的装饰器 """
    @functools.wraps('func')
    def start(*args, **kwargs):
        g = func(*args, **kwargs)
        next(g)
        return g
    return start

@coroutine
def thumb(save_path, rate, thumb_preffix=''):
    img_exts = ('.jpg', '.jepg', '.gif', '.png', '.bmp')
    saved_filenames = set()
    repeatd_times = 0
    while 1:
        img_path = (yield)
        img_ext = os.path.splitext(img_path)[1].lower()
        if not img_ext in img_exts:
            continue
        print(img_path)
        save_filename = '%s%s%s' % (thumb_preffix, os.path.splitext(os.path.split(img_path)[1])[0], img_ext)
        if save_filename in saved_filenames:
            repeatd_times += 1
            save_filename = '%s_%s%s' % (os.path.splitext(save_filename)[0], repeatd_times, img_ext)
        img = Image.open(img_path)
        img.thumbnail((int(img.size[0]*rate), int(img.size[1]*rate)), 1)
        img.save(os.path.join(save_path, save_filename))
        saved_filenames.add(save_filename)
        print('%s %s' % ('Thumb', save_filename))

def process(src, func, is_recursive=True):
    files = []
    dirs = []
    for f in os.listdir(src):
        path = os.path.join(src, f)
        if os.path.isfile(path):
            files.append(path)
        elif is_recursive and os.path.isdir(path) and f.lower() not in ('缩略图', 'thumb', 'thumbs', 'thumbnail', 'thumbnails'):
            dirs.append(path)
    for path in files:
        func.send(path)
    for path in dirs:
        process(path, func, is_recursive)

if __name__ == '__main__':
    src, dest, thumb_dir, rate, thumb_preffix, is_recursive = SRC, DEST, THUMB_DIR, RATE, THUMB_PREFFIX, IS_RECURSIVE
    opts, args = getopt.getopt(sys.argv[1:], 'r:d:p:s')
    print(opts, args)
    for k,v in opts:
        if k == '-r':
            rate = float(v)
        elif k == '-d':
            thumb_dir = v
        elif k == '-p':
            thumb_preffix = v
        elif k == '-s':
            is_recursive = False
    if args:
        args.append(os.getcwd())
        src, dest = args[:2]

    print(src, dest, thumb_dir, rate, thumb_preffix, is_recursive)
    sys.exit()
    save_path = os.path.join(dest, thumb_dir)
    if not os.path.exists(save_path):
        os.makedirs(save_path)

    my_thumb = thumb(save_path, rate, thumb_preffix)
    process(src, my_thumb, is_recursive)
    print('\nDone!')
    input()

脚本依然是基于 PIL 的, 但目前 PIL 的官方还未发布兼容 Python 3 的 Windows 编译版, 幸运的是, 网上可以下载到非官方的编译版本:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil

脚本默认递归搜索当前文件夹及子文件夹的图像文件, 然后将生成的缩略图文件放在当前文件夹的”缩略图”文件夹下.当然, 也可修改脚本的参数, 或者在命令行下指定参数:

python3 thumbnial_batch.py -r 0.5 -d ‘thumbs’ -p ‘thumb’ -s 源文件夹 目标文件夹

参数 -r: 指定压缩图片大小的比率;参数 -d: 指定保存缩略图的文件夹名称; 参数 -p: 指定生成的缩略图的文件名添加的前缀; 参数 -s: 只搜索源文件夹,不进行递归搜索子文件夹.
Have fun :>

Posted in Python | Tagged: , , , , | Leave a Comment »

第三方 Python 库的非官方 Windows 编译版

Posted by Ross Wan 于 2011/08/27

在 Python 3k 正式发布不久, 本人就开始过渡到 Python 3k 了. 对于 Python 3 不向后兼容,现在已经适应了, 唯一的问题是, 到目前为止, 还有不少第三方模块未兼容到 Python 3, 象 PIL 这个被誉为 Python 的”标准”图像处理库, 虽然有计划向 Python 3 兼容,但等了又等, 到现在还未完全落实—- 查看其 repository,发现它的最新开发版已经支持 Python 3了,只是有部分功能未完善.可惜,目前官方未发布其开发版本的 windows 编译版,幸运的是, 有一个 “Unofficial Windows Binaries for Python Extension“——虽然是非官方的, 但网上有很多人推荐, 里面有很多非常难找的第三方 Python 库的 Windows 编译版,当然包括了 PIL 1.1.7 For Windows

Have fun~~

Posted in Python | Tagged: , , , , | Leave a Comment »

Python: 简单的图片批量缩放脚本

Posted by Ross Wan 于 2008/11/24

今天,应一个朋友的要求 —- 她苦于要手工一个一个地制作缩略图,在网上又找不到合适的工具,大部分都不是免费的。于是,我即时用 Python 写了个图片缩小脚本给她。功能很单薄,不过已经满足了她的需求。

脚本使用了 PIL 图像处理库。下面是脚本的代码:

#!bin/python
# coding=utf-8

import os
import Image

# 字义缩放百分比
rate = 40

# 缩略图命名,在后面加上
resizedAddone = '_resized'

# 支持格式
suportFormat = ['bmp', 'gif', 'jpg', 'png']

def process(arg, dirs, files):
    for file in files:
        fileExt = os.path.splitext(file)[1].lower()[1:]
        if fileExt in suportFormat:
            img = Image.open(file)
            img.thumbnail((img.size[0] * rate / 100, img.size[1] * rate / 100))
            img.save(os.path.basename(file) + resizedAddone + str(rate) + '.' + fileExt)

if __name__ == '__main__':
    os.path.walk('.', process, '123')

脚本的使用方法很简单:将脚本放在图片所在目录下,双击脚本就会自动执行。脚本只会对当前目录下的图片生成缩略图(不会递归子目录)。修改脚本中的 rate,可以设置其它缩放比率。

Posted in Python | Tagged: , , , , , , | Leave a Comment »