[英]Is there a fast way to save images files on python?
I made a program to filter an images with only 20 colors, and i want to save the process that the program is making until it finished, i've successfully saved the images however it takes a lot of time. 我制作了一个程序来仅过滤20种颜色的图像,并且我想保存程序在完成之前所进行的过程,我已经成功保存了图像,但是这需要很多时间。
Lets say the user gives me an image of 800x600, it will take about 15 seconds or less to my program to finish editing the image, but if i save after every step, it will take around 10 min.. and that is because after every save, the program is waiting for the file to be ready altho it doesn't need the image it can move on and let winodws handle it. 可以说用户给我一张800x600的图像,我的程序需要大约15秒或更短的时间才能完成图像的编辑,但是如果我在每一步之后保存,大约需要10分钟..那是因为每次保存,程序正在等待文件准备就绪,尽管它不需要可以继续移动的图像并让winodws处理它。
I've tried doing it with threading and i get the same result, about 1s per image.. 我试着用线程来做,我得到相同的结果,每个图像大约1s。
from PIL import Image
from PIL import ImageGrab
im = Image.open('braw.png') # Can be many different formats.
rgb_im = im.convert('RGB')
pix = rgb_im.load()
height, width = im.size
image = ImageGrab.grab()
path = os.getcwd()
def read_color(height,width, COLORS):
for x in range(height):
for y in range(width):
r,g,b = rgb_im.getpixel((x,y))
color = closest_color(r, g, b, COLORS)
back_work(color,x,y)
save_frame(x, '/frames')
def save_frame(x, location):
try:
rgb_im.save(path + location+'/ark_ai' + str(x) + '.jpeg')
except OSError:
sleep(0.5)
def back_work(color, x, y):
pix[x,y] = color
I've expected the program to keep saving images after every loop even tho its not done, is there a faster way of doing so that i am not aware of? 我期望程序在每次循环后都会保存图像,即使没有完成,是否有一种更快的方式来使我不知道?
It looks like you're saving every time you update a pixel, which means you're saving 480,000 images, with a total of 691 GB written to the disk. 看起来您每次更新像素都在保存,这意味着您要保存480,000张图像,总共691 GB写入磁盘。 Granted, not all of that will be saved to the hard drive at once, since you're overwriting 99% of the files.
当然,由于您要覆盖99%的文件,因此并非所有内容都会立即保存到硬盘中。 Nonetheless, that's a lot of file I/O, and there simply isn't much you can do to speed that up.
尽管如此,那还是很多文件I / O,而您根本无能为力。
Why don't you try saving after each column is updated, rather than each pixel? 为什么您不尝试在更新每列而不是每个像素之后进行保存? That would reduce the amount of saving you do by a factor of 600. And the output should be the same, since you're only skipping saving the frames that would have been overwritten anyway.
这将使您节省的工作量减少了600倍。输出应该是相同的,因为您只是跳过保存无论如何都会被覆盖的帧。
def read_color(height,width, COLORS):
for x in range(height):
for y in range(width):
r,g,b = rgb_im.getpixel((x,y))
color = closest_color(r, g, b, COLORS)
back_work(color,x,y)
save_frame(x, '/frames')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.