빠른 참조

ChunkyPNG 치트시트 (Ruby PNG 이미지 라이브러리)

순수 Ruby 기반 PNG 이미지 처리 라이브러리 ChunkyPNG 핵심 치트시트. 이미지 로드, 저장, 픽셀 조작, 자르기 및 캔버스 변환을 정리합니다.

· 게시일

ChunkyPNG은 외부 C 확장이나 ImageMagick 없이 PNG 이미지를 읽고 쓰고 조작할 수 있는 순수 Ruby 라이브러리입니다.

이미지 로드 (Loading Images)

require 'chunky_png'

# Load directly from filesystem
image = ChunkyPNG::Image.from_file('input.png')

# Load from binary in-memory blob or IO stream
image = ChunkyPNG::Image.from_blob(File.binread('input.png'))
image = ChunkyPNG::Image.from_io(io_stream)

이미지 저장 및 내보내기 (Saving Images)

# Save to file
image.save('output.png')

# Export to binary string blob
binary_blob = image.to_blob

# Write to stream
File.open('stream.png', 'wb') { |io| image.write(io) }

픽셀 조작 및 도형 그리기 (Pixel Drawing)

# Set individual pixel RGBA values
image[0, 0] = ChunkyPNG::Color.rgba(255, 0, 0, 128)

# Color constructors
red  = ChunkyPNG::Color.from_hex('#ff0000')
cyan = ChunkyPNG::Color.rgba(0, 255, 255, 255)

# Draw geometric primitives
image.line(10, 10, 100, 10, red)
image.rect(20, 20, 80, 80, red, cyan) # border, fill
image.circle(50, 50, 25, red)

자르기 및 캔버스 변환 (Canvas Transforms)

# Crop sub-rectangle: crop(x, y, width, height)
cropped = image.crop(10, 10, 200, 200)

# Flipping and rotation
transformed = image.flip_horizontally.rotate_right
flipped_v   = image.flip_vertically

관련 주제