Log in

View Full Version : Process and overwrite each individual png in a folder?


shoopdabloop
1st September 2009, 04:25
I was wondering what the most efficient way would be to process/filter and overwrite each .png (there are hundreds and not named uniformly) in a folder with Avisynth, hopefully with little manual effort besides setup. I am willing to use any third-party software. Thanks in advance!

Also, please do not ask why unless you know how to do this, I don't want any posts before I get an answer :D
If you must know, it is simply out of curiosity, and has to do with filtering dumped textures for reloading.

hanfrunz
3rd September 2009, 16:50
i think you should write a windows-batch file that does this:

-use the "for" command to get all *.png files (see windows help)
-then use "echo" commands to create a avs file example: echo directshowsource("yourfile.avi") >> myavs.avs (use >> to append to an file and ">" to create a new file.
-in that avs use the imagereader + imagewriter filter to read and create the new image
-then delete the original (assuming you have a backup!!)
-then rename the new one to the old's filename

that's it. If you have problems post your work and we'll help.

hanfrunz

shoopdabloop
3rd September 2009, 23:40
i'm confused, would i have to rename each new png individually? :( there are hundreds, named like so:

TEXTURE#0C89D16B#0#2_all.png

all with different bolded parts.

maxhondur
4th September 2009, 03:10
For me, I would use AVSP, because the creator of the program already made a macro with the kind of request that you're making specifically in mind.

Get avsp, start avsp.exe, go to the macros tab, select 'examples -- image processing' and give it a go, and make the edits as you see fit.






Or, if making the edits in the macro is too confusing, here's a more explicit method: In the avsp 'macros' folder, make a new text file, call it anynameyouwant.py, insert the below text

# This example shows how to use AvsP's macros to turn AviSynth into an
# all-purpose image editor. It's similar to the batch example, but instead
# of getting all sources in a directory and generating AviSynth scripts, this
# macro gets all the bitmaps or jpgs in a directory and generates a bunch of
# new pngs filtered according to the specified AviSynth functions. The
# macro also shows how to get image properties such as width and height,
# necessary in this example to ensure the width and height are both acceptable
# for a ConvertToYV12() (assumming you are using YV12 specific filters).
# Also demonstrated is the progress box, which shows elapsed and remaining
# time and allows you to cancel the processing whenever you want.

import os

# Get the directory containing files
dirname = avsp.GetDirectory()

if dirname and avsp.GetText() == '':
# Create the list of file names in the directory which are pngs
namelist = []
for name in os.listdir(dirname):
if os.path.splitext(name)[1] in ('.png'):
namelist.append(name)
# Create a progress box
pbox = avsp.ProgressBox(len(namelist), 'Processing images...')
# Generate each of the image files
for i, filename in enumerate(namelist):
fullname = os.path.join(dirname, filename)
# Clear all the text in the tab
avsp.SetText('')
# Get the extension-based template string
srctxt = avsp.GetSourceString(fullname)
avsp.InsertText('%s\n' % srctxt)
# Get the width and height of the video
w = avsp.GetVideoWidth()
h = avsp.GetVideoHeight()
# Add borders to make the width and height mod 32
wpad = 32 - w % 32
hpad = 32 - h % 32
txt = 'AddBorders(0,0,%i,%i)\n' % (wpad, hpad)
avsp.InsertText(txt)
# Add the rest of the script
# (the crop at the end gets rid of any borders added earlier)
avsp.InsertText(
'ConvertToYV12()\n'
'SwapUV()\n'
'Sharpen(1.0)\n'
'ConvertToRGB32()\n'
'Crop(0,0,-%i,-%i)\n' % (wpad, hpad)
)
# Save the image as a png
newname = os.path.join(dirname, filename)
avsp.SaveImage(newname)
# Update the progress box, exit if user canceled
if not pbox.Update(i):
break
# Destroy the progress box
pbox.Destroy()
# Clear the remaining text
avsp.SetText('')
else:
avsp.MsgBox('Macro aborted')







and run that (ie go to the avsp tab 'macros -- anynameyouwant.py'). The above code will take all png files in a directory that you choose (the code asks you to select the directory in a popup box), and overwrite them all, using the avisynth filters

'ConvertToYV12()\n'
'SwapUV()\n'
'Sharpen(1.0)\n'
'ConvertToRGB32()\n'
'Crop(0,0,-%i,-%i)\n' % (wpad, hpad)

, which was specified in the code. Therefore, if you wanted to just use awarpsharp(3), then in the above code, delete the lines of filters you don't want (like 'SwapUV()\n'), and insert lines like 'awarpsharp(3)\n' in its stead.




If this is too confusing, let me know. Hopefully one of these posted suggestions will be helpful to you :)

hanfrunz
4th September 2009, 08:51
i'm confused, would i have to rename each new png individually? :( there are hundreds, named like so:

TEXTURE#0C89D16B#0#2_all.png

all with different bolded parts.

no, this can be done with your batchfile. Read the windows help or search for some guides for windows batch-files.