我有这个Python代码:
import Quartz.CoreGraphics as cg
region = cg.CGRectMake(0, 0, 100, 100) # You can also use cg.CGRectInfinite for the full screen.
image = cg.CGWindowListCreateImage(region, cg.kCGWindowListOptionOnScreenOnly, cg.kCGNullWindowID, cg.kCGWindowImageDefault)
prov = cg.CGImageGetDataProvider(image)
img_data = cg.CGDataProviderCopyData(prov)
img_width, img_height = cg.CGImageGetWidth(image), cg.CGImageGetHeight(image)
from pngcanvas import PNGCanvas
import struct
canvas = PNGCanvas(img_width, img_height)
for x in range(img_width):
for y in range(img_height):
# Calculate offset, based on http://www.markj.net/iphone-uiimage-pixel-color/
offset = 4 * ((img_width * int(round(y))) + int(round(x)))
# Pixel data is unsigned char (8bit unsigned integer), and there are for (blue,green,red,alpha).
# Unpack data from string into Python'y integers.
b, g, r, a = struct.unpack_from("BBBB", img_data, offset=offset)
# Assign BGRA color as RGBA.
canvas.point(x, y, color = (r, g, b, a))
with open("test.png", "wb") as f:
f.write(canvas.dump())
它似乎有效,除了保存的PNG图像看起来很乱的事实:
代码逻辑基于此post的代码(请参阅:pixel
函数)。我没有看到上面的代码有任何问题。我缺少什么?