You can use the python PIL (or Pillow for python3) library to do this, or to do this directly in Blender (which is slower but works, and the generated image is directly usable as an image texture or whatever you'd like).
PIL method:
from PIL import Image
imPath = "C:/tmp/0017.png"
im = Image.open( imPath )
half = Image.new('RGB', tuple([ int(d/2) for d in im.size ]) )
for i in range( im.size[0] ):
for j in range( im.size[1] ):
if i % 2 == 0 and j % 2 == 0:
half.putpixel(( int(i/2), int(j/2) ), im.getpixel((i,j)) )
half.show()
Direct blender script:
This method is slower than the PIL solution but not too slow (took me around 2 seconds on my i7 laptop for a 1280x720 pixel image).
It will create a Blender Image Data (bpy.data.images) object and another one for the generated half size image (you can see it in the UV/image editor).
import bpy
from os.path import dirname, basename
imPath = "C:/tmp/x.png" # <== Replace this with the path to your image
bpy.ops.image.open(
filepath = imPath,
directory = dirname( imPath ),
files = [ { 'name' : basename( imPath ) } ],
relative_path = False
)
origImg = bpy.data.images[ basename( imPath ) ]
half = bpy.data.images.new(
"half",
int( origImg.size[0] / 2 ),
int( origImg.size[1] / 2 )
)
pxList = list( origImg.pixels )
quartets = [ pxList[i:i+4] for i in range(0, len(origImg.pixels), 4) ]
halved = [ p for i,p in enumerate( quartets ) if int( i / origImg.size[0] ) % 2 == 0 and i % 2 == 0 ]
half.pixels = [ chan for p in halved for chan in p ]