Skip to content

Instantly share code, notes, and snippets.

@douglasgoodwin
Last active May 2, 2023 00:02
Show Gist options
  • Save douglasgoodwin/aea0cb6ec13e69e0d580f55fd4c8e69c to your computer and use it in GitHub Desktop.
Save douglasgoodwin/aea0cb6ec13e69e0d580f55fd4c8e69c to your computer and use it in GitHub Desktop.
# WWF
# WWF
# Read about these projects:
# https://twistedsifter.com/2019/10/populations-of-endangered-species-depicted-by-the-number-of-pixels/
# https://mymodernmet.com/endangered-species-pixelated-photos-jjsmooth44/
# Bonobo: approx 10000 remain
# the squareroot of 10000 is 100
# width is 600, so sample the image every 6 pixels
import math
# remote images
# imgurl = "https://gist.githubusercontent.com/douglasgoodwin/aea0cb6ec13e69e0d580f55fd4c8e69c/raw/924160ee1a26d1c4b11e3ed6fa8d04a9891f9077/africanWildDog.jpg"
imgurl = "https://gist.githubusercontent.com/douglasgoodwin/aea0cb6ec13e69e0d580f55fd4c8e69c/raw/924160ee1a26d1c4b11e3ed6fa8d04a9891f9077/amurLeopard.jpg"
# https://gist.githubusercontent.com/douglasgoodwin/aea0cb6ec13e69e0d580f55fd4c8e69c/raw/924160ee1a26d1c4b11e3ed6fa8d04a9891f9077/amurTiger.jpg
# https://gist.githubusercontent.com/douglasgoodwin/aea0cb6ec13e69e0d580f55fd4c8e69c/raw/924160ee1a26d1c4b11e3ed6fa8d04a9891f9077/asianElephant.jpg
# https://gist.githubusercontent.com/douglasgoodwin/aea0cb6ec13e69e0d580f55fd4c8e69c/raw/924160ee1a26d1c4b11e3ed6fa8d04a9891f9077/bengalTiger.jpg
# https://gist.githubusercontent.com/douglasgoodwin/aea0cb6ec13e69e0d580f55fd4c8e69c/raw/924160ee1a26d1c4b11e3ed6fa8d04a9891f9077/blackFootedFerret.jpg
# https://gist.githubusercontent.com/douglasgoodwin/aea0cb6ec13e69e0d580f55fd4c8e69c/raw/924160ee1a26d1c4b11e3ed6fa8d04a9891f9077/blackRhino.jpg
# https://gist.githubusercontent.com/douglasgoodwin/aea0cb6ec13e69e0d580f55fd4c8e69c/raw/924160ee1a26d1c4b11e3ed6fa8d04a9891f9077/blueWhale.jpg
# https://gist.githubusercontent.com/douglasgoodwin/aea0cb6ec13e69e0d580f55fd4c8e69c/raw/924160ee1a26d1c4b11e3ed6fa8d04a9891f9077/bonobo.jpg
# https://gist.githubusercontent.com/douglasgoodwin/aea0cb6ec13e69e0d580f55fd4c8e69c/raw/924160ee1a26d1c4b11e3ed6fa8d04a9891f9077/boreanOrangutan.jpg
# https://gist.githubusercontent.com/douglasgoodwin/aea0cb6ec13e69e0d580f55fd4c8e69c/raw/924160ee1a26d1c4b11e3ed6fa8d04a9891f9077/borneoPygmyElephant.jpg
# https://gist.githubusercontent.com/douglasgoodwin/aea0cb6ec13e69e0d580f55fd4c8e69c/raw/924160ee1a26d1c4b11e3ed6fa8d04a9891f9077/easternLowlandGorilla.jpg
# fetch the image
from io import BytesIO
from urllib.request import urlopen
imgfile = BytesIO(urlopen(imgurl).read())
# Write the stream to a file
with open("animal.jpg", "wb") as f:
f.write(imgfile.getbuffer())
name = "Amur Leopard"
ct = 36
imgpath = "animal.jpg" # you don't need to change this unless you're using your own file
wh = math.isqrt(ct)
res = int(600/wh)
print(f"{name} estimated that about {ct} remain. wh is {wh}, resolution is {res}")
def setup():
size(600,600)
global img
no_stroke()
# images must be in the "data" directory to load correctly
img = load_image(imgpath)
img.load_pixels() # see https://py5coding.org/reference/sketch_load_pixels.html
image(img, 0, 0, width,height)
for y in range(0,height,res):
for x in range(0,width,res):
# get the color of the pixel at x,y
#
# first: calculate the 1D pixel location
# read more here: https://processing.org/tutorials/pixels
#
# Assume an image with a given WIDTH and HEIGHT.
# We then know the pixel array has a total number of elements equaling WIDTH * HEIGHT.
# For any given X, Y point in the window, the location in our 1 dimensional pixel array is:
loc = int( x+y * img.width)
# get the r,g,b at that location
r = red(img.pixels[loc])
g = green(img.pixels[loc])
b = blue(img.pixels[loc])
fill(r,g,b)
square(x,y,res)
fill(255)
text_size(80)
text("HI",100,height-100)
save("poster.jpg")
def draw():
no_loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment