Skip to content

Instantly share code, notes, and snippets.

@CnrLwlss
Last active May 1, 2021 21:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CnrLwlss/5330143 to your computer and use it in GitHub Desktop.
Save CnrLwlss/5330143 to your computer and use it in GitHub Desktop.
Counting colony forming units (CFU) generated from cells plated onto solid agar. Left click to tag cells with red marker, right click to tag with yellow marker. Generates an image record of marked CFUs with red and yellow counts embedded into image record filename.
import pygame, os, sys
from pygame.locals import *
# Runs through all jpgs in current directory
# Opens each in turn, allowing user to click on image features
# Left clicking draws a red dot, right-clicking draws a yellow dot
# Press q or esc to move to next image
# Once an image is finished, a version with click locations overlaid is saved to file
# Numbers of left and right clicks are embedded into filename
allfiles=os.listdir(os.getcwd())
filelist=[f for f in allfiles if f[-4:] in ['.jpg','.JPG','.jpeg','.JPEG']]
dotfrac=0.0035
for imname in filelist:
pygame.init()
inf=pygame.display.Info()
sw,sh=inf.current_w,inf.current_h
PhotoSurface = pygame.image.load(imname)
ow,oh=PhotoSurface.get_size()
if ((float(ow)/float(oh))>(float(sw)/float(sh))):
size=(sw,int(oh*float(sw)/float(ow)))
else:
size=(int(ow*float(sh)/float(oh)),sh)
dotradius=int(round(dotfrac*min(size)))
screen = pygame.display.set_mode(size,pygame.FULLSCREEN)
PhotoSurface = pygame.transform.scale(PhotoSurface,size)
background = pygame.Surface(size)
screen.blit(PhotoSurface, (0, 0))
pygame.display.flip()
leftcoords,rightcoords=[],[]
running=True
while running == True:
for event in pygame.event.get():
if event.type ==KEYDOWN and event.key in (K_q,K_ESCAPE):
running=False
else:
if event.type == MOUSEBUTTONDOWN:
mx,my=event.pos
if event.button==1:
leftcoords.append([mx,my])
if event.button==3:
rightcoords.append([mx,my])
screen.blit(PhotoSurface, (0, 0))
for coord in leftcoords:
pygame.draw.circle(screen, (255,0,0), coord, dotradius)
for coord in rightcoords:
pygame.draw.circle(screen, (255,255,0), coord, dotradius)
pygame.display.flip()
fname=imname[0:-4]+"_L%04d_R%04d.png"%(len(leftcoords),len(rightcoords))
pygame.image.save(screen,fname)
pygame.quit()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment