您當前的位置:首頁 > 曲藝

拼圖還能這麼玩?

作者:由 godweiyang 發表于 曲藝時間:2022-04-23

這兩天將我所有微信好友的頭像弄出來了,一共5000多張。然後想著可以用它們來做些啥,最後用它們拼圖玩。

Mac微信的頭像儲存在:

~/Library/Containers/com。tencent。xinWeChat/Data/Library/Application\ Support/com。tencent。xinWeChat/2。0b4。0。9/c1a30fcf75eedba12764b4d4170b977e/Avatar

其中倒數第二個那一長串字串每個人會不同,根據自己情況進行修改。

用到的程式碼在文章最後,github上也開源了:

最終效果

直接上最終效果圖,首先是5000多張頭像拼接成一張圖片:

拼圖還能這麼玩?

如果看不清的話可以放大看看細節:

拼圖還能這麼玩?

然後是5000多張頭像拼成的楊超越:

拼圖還能這麼玩?

如果看不清的話可以放大看看細節:

拼圖還能這麼玩?

楊超越的原圖是這樣的:

拼圖還能這麼玩?

實現方法

實現方法很簡單,拼接的話就是把頭像依次貼上在一塊大畫布上的不同區域,就跟貼瓷磚一樣。

為了實現用不同頭像拼接出楊超越,就需要先將楊超越圖片進行分割,每一塊小區域尋找一個顏色最相近的頭像貼上上去。我直接將影象區域的RBG值求了加權平均,然後在頭像中尋找RGB均值相等的貼上上去。

程式碼放到文章最後了,執行命令如下:

python3 image_stitch。py -d [影象合集目錄] -i [待拼湊的圖片] -s [小圖最終的邊長] -r

其中

-d

表示影象目錄,你也可以放頭像或者其他的影象;

-i

是你想拼成的大圖路徑,如果不設定的話就是直接把影象拼在一起;

-s

表示小影象最終的邊長,實測設定為30效果最佳,圖片大小和質量都比較好;

-r

表示是否隨機排列圖片。

此外之前還寫過字元畫影片的生成方法,程式碼也開源在同一個github上了:

程式碼

import

argparse

import

os

import

random

import

math

from

collections

import

defaultdict

import

numpy

as

np

import

PIL。Image

as

Image

from

tqdm

import

tqdm

trange

def

generate1

dir

size

rand

):

print

f

“正在拼接尺寸:{size}。。。”

nums

=

len

os

listdir

dir

))

nums_width

=

int

math

sqrt

nums

))

nums_height

=

int

((

nums

+

nums_width

-

1

/

nums_width

img_width

=

nums_width

*

size

img_height

=

nums_height

*

size

image

=

Image

new

“RGB”

img_width

img_height

),

“white”

x

=

0

y

=

0

files

=

os

listdir

dir

if

rand

random

shuffle

files

for

i

in

tqdm

files

):

try

img

=

Image

open

os

path

join

dir

i

))

except

IOError

print

i

print

“影象開啟失敗”

else

img

=

img

resize

((

size

size

),

Image

ANTIALIAS

image

paste

img

x

*

size

y

*

size

))

x

+=

1

if

x

==

nums_width

x

=

0

y

+=

1

img

close

()

image

save

f

“avatar_{size}。jpg”

image

close

()

def

mean_pixel

colors

):

colors

=

0。3

*

r

+

0。59

*

g

+

0。11

*

b

for

r

g

b

in

colors

return

int

np

mean

colors

))

def

generate2

dir

source

size

rand

):

print

f

“正在拼接尺寸:{size}。。。”

files

=

os

listdir

dir

if

rand

random

shuffle

files

image

=

Image

open

source

image

=

image

convert

“RGB”

img_width

img_height

=

image

size

img_width

=

((

img_width

+

size

-

1

//

size

*

size

*

((

size

+

9

//

10

img_height

=

((

img_height

+

size

-

1

//

size

*

size

*

((

size

+

9

//

10

image

=

image

resize

((

img_width

img_height

),

Image

ANTIALIAS

colors

=

defaultdict

list

for

i

in

tqdm

files

):

try

img

=

Image

open

os

path

join

dir

i

))

except

IOError

print

i

print

“影象開啟失敗”

else

img

=

img

convert

“RGB”

img

=

img

resize

((

size

size

),

Image

ANTIALIAS

colors

mean_pixel

img

getdata

())]

append

i

img

close

()

for

i

in

range

256

):

if

len

colors

i

])

==

0

for

n

in

range

1

256

):

if

len

colors

i

-

n

])

!=

0

colors

i

=

colors

i

-

n

break

if

len

colors

i

+

n

])

!=

0

colors

i

=

colors

i

+

n

break

index

=

defaultdict

int

for

i

in

trange

0

img_width

size

):

for

j

in

range

0

img_height

size

):

now_colors

=

[]

for

ii

in

range

i

i

+

size

):

for

jj

in

range

j

j

+

size

):

now_colors

append

image

getpixel

((

ii

jj

)))

mean_color

=

mean_pixel

now_colors

img

=

Image

open

os

path

join

dir

colors

mean_color

][

index

mean_color

%

len

colors

mean_color

])]

img

=

img

convert

“RGB”

img

=

img

resize

((

size

size

),

Image

ANTIALIAS

image

paste

img

i

j

))

img

close

()

index

mean_color

+=

1

source_name

=

“。”

join

source

split

“。”

)[:

-

1

])

image

save

f

“{source_name}_{size}。jpg”

image

close

()

if

__name__

==

“__main__”

parser

=

argparse

ArgumentParser

()

parser

add_argument

“——dir”

“-d”

type

=

str

default

=

“avatar”

help

=

“directory of the avatars”

parser

add_argument

“——img”

“-i”

type

=

str

default

=

“”

help

=

“source image to be coverd”

parser

add_argument

“——size”

“-s”

type

=

str

default

=

“30”

help

=

“size of each avatar (size1,size2,。。。)”

parser

add_argument

“——rand”

“-r”

action

=

“store_true”

help

=

“whether to shuffle the avatars”

args

=

parser

parse_args

()

sizes

=

int

s

for

s

in

args

size

split

“,”

)]

for

size

in

sizes

if

len

args

img

==

0

generate1

args

dir

size

args

rand

else

generate2

args

dir

args

img

size

args

rand

標簽: img  size  COLORS  Image  width