Skip to content

Instantly share code, notes, and snippets.

@CnrLwlss
Created February 9, 2018 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CnrLwlss/5ad5f460f98e6dee555b64d69d07c27f to your computer and use it in GitHub Desktop.
Save CnrLwlss/5ad5f460f98e6dee555b64d69d07c27f to your computer and use it in GitHub Desktop.
Converting non-image .tiff data to pseudo-images (8-bit greyscale, high contrast).
from PIL import Image
import numpy as np
import os
for fname in os.listdir("."):
if fname.endswith(".tiff"):
im=Image.open(fname)
arr=np.array(im,dtype=np.uint16)
maxval = np.max(arr[arr>0])
minval = np.min(arr[arr>0])
arr=arr-minval
arrf = np.array(arr,dtype=np.float)
arrf = np.array(np.round(255.0*arrf/(maxval-minval)),dtype=np.uint8)
im8 = Image.fromarray(arrf)
im8 = im8.resize([500,500])
im8.save(fname.strip(".tiff")+".jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment