pairofdocs / dc6_png_enhance

Tool to convert from .dc6 (D2 images) to upsampled .png images
GNU General Public License v2.0
9 stars 2 forks source link

Add transparency: change rgb0,0,0 +/- to alpha channel for dc6-pngs #2

Open pairofdocs opened 3 years ago

pairofdocs commented 3 years ago

Using imagemagick

for png_name in pngs:
  # transparency
  convert .\png\png_name -fuzz 7.0% -transparent black .\png\tmp.png
  #alpha layer
  convert .\png\tmp.png -alpha extract .\png\tmp2.png
  # clean up alpha layer 
  convert .\png\tmp2.png -define connected-components:mean-color=true -define connected-components:area-threshold=200 -connected-components 4 .\png\tmp3.png
  # add blur to alpha layer boundary
  convert .\png\tmp3.png -blur 0x0.5 .\png\tmp3-b.png
  # compose orig image with alpha layer
  convert .\png\png_name .\png\tmp3-b.png -alpha off -compose copy_opacity -composite .\png\png_name + '-trsp-hd.png'
  # resize to proper D2R size
  convert .\png\png_name + '-trsp-hd.png' -resize 87.5% .\png_hd_resize\png_name + 'hd-resize.png'
  # sharpen the image
  convert .\png_hd_resize\png_name + 'hd-resize.png' -unsharp 0x1 .\png_hd_resize\png_name + 'hd-sharp.png'

  # Then convert pngs to .sprites using SpriteEdit

references: https://legacy.imagemagick.org/discourse-server/viewtopic.php?t=33589

pairofdocs commented 3 years ago

a working python script that saves images to .\processed_items_D2R\

import glob
import subprocess
import time
import os

# post processing for transparency.  then do resizing
# fuzz 11% removed too much if the dark pixels in the weps/armors.   originally had 7% but chose 5.5% to remove the least pixels from the objects
# fuzz of 5.5% didnt work well on the helm invfhl.  set fuzz to 4.5%
hd_pngs = glob.glob('png-hd/*hd.png')
for hd_png in hd_pngs:
  print(f'starting to process:   {os.path.basename(hd_png)}')
  cmd = 'convert ' + hd_png +  ' -bordercolor black -border 1x1 -fuzz 4.5% -transparent black -shave 1x1 .\\processed_items_D2R\\tmp.png'
  subprocess.run(cmd, shell=True, check=True)

  cmd = 'convert .\\processed_items_D2R\\tmp.png -alpha extract .\\processed_items_D2R\\tmp2.png'
  subprocess.run(cmd, shell=True, check=True)

  cmd = 'convert .\\processed_items_D2R\\tmp2.png -define connected-components:mean-color=true -define connected-components:area-threshold=200 -connected-components 4 .\\processed_items_D2R\\tmp3.png'
  subprocess.run(cmd, shell=True, check=True)

  cmd = 'convert .\\processed_items_D2R\\tmp3.png -blur 0x0.5 .\\processed_items_D2R\\tmp3-b.png'
  subprocess.run(cmd, shell=True, check=True)

  cmd = 'convert ' + hd_png +  ' .\\processed_items_D2R\\tmp3-b.png -alpha off -compose copy_opacity -composite .\\processed_items_D2R\\' +  os.path.basename(hd_png)[:-4] + '-trspr.png'
  subprocess.run(cmd, shell=True, check=True)

  time.sleep(0.25)

# size of D2R invgisu    98 x 392.     size of  D2 hd.png    112 x 448    scale 87.5%  both x and y    -v'
# tried with and without sharpening. No-sharpening looked better in-game
trsp_pngs = glob.glob('processed_items_D2R/*trspr.png')
for trsp_png in trsp_pngs:
  print(f'starting to process:   {os.path.basename(trsp_png)}')
  cmd = 'convert ' + trsp_png + ' -resize 87.5%  ' + trsp_png[:-4] + '-resize.png'
  subprocess.run(cmd, shell=True, check=True)
  time.sleep(0.25)