Python 我需要做一个;“马赛克”但是很简单
我发现的mosaic的最佳代码可以在本页看到: 然而,这段代码在我的Windows电脑上运行得不好,而且我认为这段代码太高级了,不适合它应该做的事情。以下是我在reddit上发布的要求: 1) 主照片的颜色数量已经减少(8) 2) 我已经有了与需要替换的颜色相关的所有图像(例如,数字1应该替换黑色像素,数字2替换绿色像素…) 3) 我需要按小照片的大小放大照片(9 x 9小照片将产生81倍大的图像),这将使像素“2n”点彼此远离,但不是在每个像素周围产生一个n x n相同颜色的区域(我认为放大通常是这样的,如果我错了,请纠正我),它只会用无法识别的颜色给空白区域上色,这与任何小照片都不相关(我们称之为颜色C) 4) 现在它所需要的就是遍历所有非C色像素,并将图像放在该像素的中心,这将创建马赛克Python 我需要做一个;“马赛克”但是很简单,python,image,request,photo,mosaic,Python,Image,Request,Photo,Mosaic,我发现的mosaic的最佳代码可以在本页看到: 然而,这段代码在我的Windows电脑上运行得不好,而且我认为这段代码太高级了,不适合它应该做的事情。以下是我在reddit上发布的要求: 1) 主照片的颜色数量已经减少(8) 2) 我已经有了与需要替换的颜色相关的所有图像(例如,数字1应该替换黑色像素,数字2替换绿色像素…) 3) 我需要按小照片的大小放大照片(9 x 9小照片将产生81倍大的图像),这将使像素“2n”点彼此远离,但不是在每个像素周围产生一个n x n相同颜色的区域(我认为放大
因为我对Python(特别是图形)非常陌生,只需要一次就可以使用它,有人能帮我创建代码吗?我觉得启发我的代码太复杂了。有两件事我不需要: 1) “近似”-如果放大度小于100%质量所需的放大度(例如,照片为9x9,但原始照片的每一面只能放大3倍,则程序需要将一些不同颜色的像素合并在一起,从而导致质量损失) 2) 协会颜色图片:我的图片调色板很小,颜色也很好,我可以手动操作 对于那些没有理解我的意思的人,我的想法是:我很快就学会了使用: 这台2015年的笔记本电脑大约需要5秒的时间,并呈现出以下图像: 我不得不缩小它以便上传,但这里有一个细节(第一个H的左下角): 这里有一个指向马赛克的谷歌硬盘链接,也许它可以工作:
下面是github上的代码:请发布一套完整的示例图像(您的8色照片,加上您的8色主图像),否则我将不得不在编写程序的同时制作测试数据。-但是,颜色选择不是很有希望。您好,您需要将mainpic保存为PNG,而不是JPG,否则您将有超过8种颜色。另外,您如何知道每个主图片颜色使用哪个smallpic?。我使用在线转换器来减少颜色的数量。他们确实希望我用JPG发布图片,但一旦颜色的数量减少,它就不会增加(例如,当保存为PNG时)。。。2.它可以自动(复杂)或手动完成,如我所愿。这取决于你。颜色的调色板有点单调,很难匹配,请自己选择(颜色不一定要匹配,只需一个小图片=一种颜色就是我所需要的)谢谢你,但是你能帮我安装PyVIP吗?它在Windows上似乎非常复杂,我不知道为什么不能作为文件夹提供。他们到处都说“pip安装”,这本应该让事情变得简单,但对我来说却更难。这里有注释:。。。下载zip,
pip install pyvips
,然后将这两行代码放在程序的开头。
#!/usr/bin/python3
import sys
import os
import pyvips
if len(sys.argv) != 4:
print("usage: tile-directory input-image output-image")
sys.exit(1)
# the size of each tile ... 16x16 for us
tile_size = 16
# load all the tile images, forcing them to the tile size
print(f"loading tiles from {sys.argv[1]} ...")
for root, dirs, files in os.walk(sys.argv[1]):
tiles = [pyvips.Image.thumbnail(os.path.join(root, name), tile_size,
height=tile_size, size="force")
for name in files]
# drop any alpha
tiles = [image.flatten() if image.hasalpha() else image
for image in tiles]
# copy the tiles to memory, since we'll be using them many times
tiles = [image.copy_memory() for image in tiles]
# calculate the average rgb for an image, eg. image -> [12, 13, 128]
def avg_rgb(image):
m = image.stats()
return [m(4,i)[0] for i in range(1,4)]
# find the avg rgb for each tile
tile_colours = [avg_rgb(image) for image in tiles]
# load the main image ... we can do this in streaming mode, since we only
# make a single pass over the image
main = pyvips.Image.new_from_file(sys.argv[2], access="sequential")
# find the abs of an image, treating each pixel as a vector
def pyth(image):
return sum([band ** 2 for band in image.bandsplit()]) ** 0.5
# calculate a distance map from the main image to each tile colour
distance = [pyth(main - colour) for colour in tile_colours]
# make a distance index -- hide the tile index in the bottom 16 bits of the
# distance measure
index = [(distance[i] << 16) + i for i in range(len(distance))]
# find the minimum distance for each pixel and mask out the bottom 16 bits to
# get the tile index for each pixel
index = index[0].bandrank(index[1:], index=0) & 0xffff
# replicate each tile image to make a set of layers, and zoom the index to
# make an index matching the output size
layers = [tile.replicate(main.width, main.height) for tile in tiles]
index = index.zoom(tile_size, tile_size)
# now for each layer, select pixels matching the index
final = pyvips.Image.black(main.width * tile_size, main.height * tile_size)
for i in range(len(layers)):
final = (index == i).ifthenelse(layers[i], final)
print(f"writing {sys.argv[3]} ...")
final.write_to_file(sys.argv[3])
$ ./mosaic3.py smallpic/ mainpic/Use\ this.jpg x.png
loading tiles from smallpic/ ...
writing x.png ...
$