Coder Social home page Coder Social logo

photo-maker's Introduction

前言

Photo-Maker 是一个用于生成和制作各种有趣的图片脚本, 使用 Python 编写,能运行在Python2 & Python 3 之上。但均依赖 PIL 库。请自行安装。除了 PIL 之外无需引入其他依赖。

目前引入的功能有:

  • 微信朋友圈心❤️型九宫格图
  • 蒙太奇(马赛克)照片
  • 拆分图 (可以将一张图拆分为M x N 张图)
  • ASCII 图 将一张图转化为ASCII码

使用方式

请执行 python photo-maker.py -h 查看帮助。

usage: photo-maker.py [-h] {heart,split,mosaic,ascii} ...

            A Dead Simple Photo Maker
===========================================================
______ _           _             ___  ___      _
| ___ \ |         | |            |  \/  |     | |
| |_/ / |__   ___ | |_ ___ ______| .  . | __ _| | _____ _ __
|  __/| '_ \ / _ \| __/ _ \______| |\/| |/ _` | |/ / _ \ '__|
| |   | | | | (_) | || (_) |     | |  | | (_| |   <  __/ |
\_|   |_| |_|\___/ \__\___/      \_|  |_/\__,_|_|\_\___|_|

The most commonly used photo-maker commands are:
    heart      Use multi image files to generate a heart picture
    split      Split one image into A x B multi grid pictures
    mosaic     Generate a mosaic photo by use tiles
    ascii      Convert an image to ascii text

positional arguments:
  {heart,split,mosaic,ascii}

optional arguments:
  -h, --help            show this help message and exit

Source Code: http://github.com/shellvon/photo-maker

Examples

所有的例子输出均放在了 examples 目录下,可自行查看。

微信❤️型九宫格

usage: photo-maker.py heart [-h] [-d DIR] [--autofill] [--padding PADDING]
                            [--size SIZE] [-s]
                            [-e EXTENSIONS [EXTENSIONS ...]] [--intermediate]
                            [-o OUTPUT]

微信心型九空格图片,一共是分享的9张,每一张也是9格图
如下图,需要填满的位置是X所在的位置:

Fig 1:              Fig 2:         Fig 3:
+---+---+---+   +---+---+---+   +---+---+---+
| 1 | 2 | 3 |   | 1 | 2 | 3 |   | 1 | 2 | 3 |
+-----------+   +-----------+   +-----------+
| 4 | 5 | x |   | x | 5 | x |   | x | 5 | 6 |
+-----------+   +-----------+   +-----------+
| 7 | x | x |   | x | x | x |   | x | x | 9 |
+-----------+   +-----------+   +-----------+

Fig 4:              Fig 5:         Fig 6:
+---+---+---+   +---+---+---+   +---+---+---+
| x | x | x |   | x | x | x |   | x | x | x |
+-----------+   +-----------+   +-----------+
| x | x | x |   | x | x | x |   | x | x | x |
+-----------+   +-----------+   +-----------+
| 7 | x | x |   | x | x | x |   | x | x | 9 |
+-----------+   +-----------+   +-----------+

Fig 7:              Fig 8:         Fig 9:
+---+---+---+   +---+---+---+   +---+---+---+
| 1 | 2 | x |   | x | x | x |   | x | 2 | 3 |
+-----------+   +-----------+   +-----------+
| 4 | 5 | 6 |   | x | x | x |   | 4 | 5 | 6 |
+-----------+   +-----------+   +-----------+
| 7 | 8 | 9 |   | 7 | x | 9 |   | 7 | 8 | 9 |
+-----------+   +-----------+   +-----------+
             ^^^
              |
              +----- 每一张图的间隔称之为 Padding

因此一共需要的图片张数是: (3 * 2) + 5 + (8 * 2) + 9 + (1 * 2) + 7 = 45 张图.

optional arguments:
  -h, --help            show this help message and exit
  -d DIR, --dir DIR     directory of the images, default is current dir
  --autofill            autofill pictures when picture is too few, default is
                        False
  --padding PADDING     padding size between each grid, default is 10
  --size SIZE           output image size,format is AxB, default size is:
                        1920x1920
  -s, --shuffle         shuffle the image file list
  -e EXTENSIONS [EXTENSIONS ...], --extensions EXTENSIONS [EXTENSIONS ...]
                        image file type, default is png,jpg,jpeg
  --intermediate        save intermediate file, default is False
  -o OUTPUT, --output OUTPUT
                        output image filename, default is collage.png

heart.png

Split 拆分图

相片马赛克

由于马赛克需要许多小图,因此此脚本默认提供了800+张emoji表情用于当tile,您可以自己提供自己的图片文件夹进行制作tile.需要注意的是文件不要太大,否则会很慢。

mosaic

ASCII Art

源图片来自: examples/sharp.png

examples/sharp.png

------------------------------------------------
--/qqQUUzx/|)))))))))))))))))))))))))))))(\jvUJU
--n$$kYf(1[_~<>>>>>>>>>>>>>>>>>>>>>>>>><<+?1)\vO
--n@Q\1]!::Ill!!!!!!!!!!!!!!!!!!!!!!!!!!lI;,I_{(
--nC({<:i>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Il}
--(|{_l<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>i
--{(}~<<<<<<<<<<<<<<<<~~~~<~<<<~~~~~<~<<<<<<<<<>
--{)}_~~~~~~~~~~~~~~~+:   ;~~~~_`   I~~~~~~~~~~<
--{({-+++++++++++++++_.  .~++++>   .~+++++++++++
--{({-_______________!   ,____-:   ;____________
--{(1]---------+~~~~+`   l~~~~~.   !~~~<?------?
--{|1{[????????"                        ]?????]}
--{|1{}}}[[]]]]~<<<~^   I<<<<~.   !<<<<<]]][}}}{
--{\){{{{{{{{{}}}}}}.  '][[[[_   `[}}}}{{{{{{{{{
--{\)1{{{{{{{1]]]]]!   :]]]][,   ;]]]]{{{{{{{{{1
--1\(11111111?                        ~111111111
--1/()1111111}++++!   ,++++_,   :+++++[111111111
--1/())))))))))))|,   _))))|.   ?)))))))))))))))
--1t|()))))))))))1   `))))(-   ^))))))))))))))))
--1t|())))))))))(i   >))))|"   ~)))))))))))))))(
--)t\((((((((((((}}}})(((()}}}})((((((((((((((((
--fx\|(((((((((((((((((((((((((((((((((((((((((|
--nkj\|||||||||||||||||||||||||||||||||||||||||\
--n$*c/\||||||||||||||||||||||||||||||||||||||\r

现在, 您可以自己指定grey-scale来输出你要的照片了.

examples/custom_grey_scale.png

photo-maker's People

Contributors

shellvon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

photo-maker's Issues

某些图片是倒置的,需要将图片旋转一定角度过来

一张JPG图片(PNG没有EXIF信息)的EXIF信息中包含了专门为数码相机的照片而定制的元数据,用于记录数码照片的拍摄参数、缩略图及其他属性。其中是否旋转可以用 Orientation 参数,它记录了图片拍摄的相机的旋转信息。
关于更细节的说明,可以参见: http://sylvana.net/jpegcrop/exif_orientation.html

一个简单的例子如图:
image

对于 PIL 库来说,可以使用

exif = img._getexif()
orientation = exif[274] if 274 in exif else 1 # 取出来,判断值,一共8个值。默认值就是1

携带有iTXt块信息的PNG图无法正确被Pickle

根据测试,一些图片在使用 multiprocessing 时无法正确的被 Pickle 。从而会提示如下类似的信息:

Exception in thread Thread-3:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 392, in _handle_results
    task = get()
TypeError: __new__() takes exactly 4 arguments (2 given)

测试代码如下:

def with_img(fname):
    im = Image.open(fname)
    return im


def test():
    p = multiprocessing.Pool()
    r = p.map(with_img, ['IMG_3475.PNG']) # PNG 需要携带有iTxt信息。
    p.close()
    p.join()
    print(r)

if __name__ == '__main__':
    test()

根据测试情况,这些失败的 PNG 均含有 iTXt ,在创建这个对象的失败了。

PngImagePlugin.py#L188-L195

@staticmethod
    def __new__(cls, text, lang, tkey):

期待4个参数,但是实际上只给了2个。我尝试将 lang,tkey 设置为可选参数代码则正常执行。

与我类似的情况,已经在 Pillow 中提到了相关 issues: Pillow/issues#1434

目前我还不知道怎么接近我的这问题,一种方式是不在multiprocessing之间传递Image。另一种方式是先去掉这些没有用的chunk信息(PNG 图片优化方式之一) 但是遗憾的是目前我没有找到不用保存就能直接修改Chunk 信息的方式。。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.