Skip to content

Instantly share code, notes, and snippets.

@mikerenfro
Last active December 30, 2023 19:42
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 mikerenfro/7659cf3845fa9e53ab7821b40e1266c6 to your computer and use it in GitHub Desktop.
Save mikerenfro/7659cf3845fa9e53ab7821b40e1266c6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 9 15:47:44 2018
@author: renfro
"""
from PIL import Image, ImageDraw, ImageFont
# displayDict structure
# - name: (string) name of display (only used to label outlined picture)
# - res: (2-tuple of ints) horizontal and vertical resolution
# - dpi: (int) dots or pixels per inch of display
# - origin: (2-tuple of floats) absolute (x, y) origin of upper-left corner of
# display with positive x pointing right, and positive y pointing
# down (same as PIL notation)
displayDict = {
# 1: {'name': 'Lenovo T27p-10',
# 'res': (3840, 2160), 'dpi': 163.5,
# 'origin': (0, 0)},
# 2: {'name': 'Lenovo T27p-10',
# 'res': (3840, 2160), 'dpi': 163.5,
# 'origin': (24, 0)},
# 3: {'name': 'MacBook Pro (Retina, 16-inch, 2019)',
# 'res': (3072, 1920), 'dpi': 226,
# 'origin': (48, 5.2)}
1: {'name': 'HP 24yh',
'res': (1080, 1920), 'dpi': 92.53,
'origin': (0, 0)},
2: {'name': 'CB272U',
'res': (2560, 1440), 'dpi': 108.94,
'origin': (12.375, 3.5)},
3: {'name': 'HP 24yh',
'res': (1080, 1920), 'dpi': 92.53,
'origin': (36.625, 0)},
# 1: {'name': 'HP/Compaq LA2405wg',
# 'res': (1920, 1200), 'dpi': 94,
# 'origin': (0, 0)},
# 2: {'name': 'HP/Compaq LA2405wg',
# 'res': (1920, 1200), 'dpi': 94,
# 'origin': (21.8, 0)},
# 3: {'name': 'MacBook Pro (Retina, 13-inch, Early 2015)',
# 'res': (2560, 1600), 'dpi': 227,
# 'origin': (30.95, 13.5)}
}
(x0, y0) = (2450, 650) # location in image for most upper-left display corner
# inputImage = 'WallpaperFusion-earth-at-night-Original-16384x8192.jpg'
inputImage = 'WallpaperFusion-blue-marble-2012-eastern-hemisphere-Original-11500x11500.jpg'
outlineImage = 'earth-outlined.jpg'
displayImageFormat = 'earth-mon{0}.jpg'
# inputImage = 'WallpaperFusion-apollo-11-Original-15409x4340.jpg'
# outlineImage = 'apollo-outlined.jpg'
# displayImageFormat = 'apollo-mon{0}.jpg'
# inputImage = 'WallpaperFusion-grand-canyon-overlook-Original-12280x6480.jpg'
# outlineImage = 'grandcanyon-outlined.jpg'
# displayImageFormat = 'grandcanyon-mon{0}.jpg'
# inputImage = 'grid-17000-9000-50.png'
# outlineImage = 'grid-outlined.jpg'
# displayImageFormat = 'grid-mon{0}.jpg'
## Shouldn't need to modify anything else below
(minDpi, maxDpi) = (10000, 1)
(minHeight, minWidth) = (0, 0)
for (k, v) in displayDict.items():
if v['dpi'] > maxDpi:
maxDpi = v['dpi']
if v['dpi'] < minDpi:
minDpi = v['dpi']
for (k, v) in displayDict.items():
(xi, yi) = (x0+v['origin'][0]*maxDpi, y0+v['origin'][1]*maxDpi)
dpiRatio = maxDpi/v['dpi']
xMax = xi+v['res'][0]*dpiRatio
yMax = yi+v['res'][1]*dpiRatio
if xMax > minWidth:
minWidth = xMax
if yMax > minHeight:
minHeight = yMax
im = Image.open(inputImage)
(_, _, imageWidth, imageHeight) = im.getbbox()
if imageWidth < minWidth or imageHeight < minHeight:
raise IndexError('Source image ({0}x{1}) too small. ({2}x{3}) required.'.format(imageWidth, imageHeight, int(minWidth+1), int(minHeight+1)))
draw = ImageDraw.Draw(im)
# fnt = ImageFont.truetype('Arial.ttf', size=100)
for (k, v) in displayDict.items():
dpiRatio = maxDpi/v['dpi']
(xi, yi) = (x0+v['origin'][0]*maxDpi, y0+v['origin'][1]*maxDpi)
mon = im.resize((v['res'][0], v['res'][1]),
resample=Image.BICUBIC,
box=(xi, yi,
xi+v['res'][0]*dpiRatio,
yi+v['res'][1]*dpiRatio))
mon.save(displayImageFormat.format(k))
draw.rectangle((xi, yi,
xi+v['res'][0]*dpiRatio,
yi+v['res'][1]*dpiRatio), outline='red')
msg = '{0}: {1}\n'.format(k, v['name'])
# (w, h) = draw.textsize(msg, font=fnt)
# (xt, yt) = (x0+v['origin'][0]*maxDpi+(v['res'][0]*dpiRatio-w)/2,
# y0+v['origin'][1]*maxDpi+(v['res'][1]*dpiRatio-h)/2)
# draw.text((xt, yt), msg, align='center', font=fnt, fill='red')
im.save(outlineImage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment