2011-08-22 15 views
8

प्रश्न के आधार पर Create PDF with (resized) PNG images using Pycairo - rescaling Surface issue मैंने कोड को बनाने का प्रयास किया है जो नीचे दिए गए कोड में दिखाए गए अनुसार एक विशिष्ट स्थिति पर एक छवि को सहेजता है और रखता है (उदाहरण के लिए, छवियों को अंतर्निहित आयताकारों को "ओवर" दिखाई देना चाहिए)। हालांकि, मुझे लगता है कि छवि सही स्थान पर दिखाई नहीं दे रही है।pyCairo: छवि का आकार बदलने और स्थिति कैसे बदलें?

मैं और दोनों पैमाने पर सही ढंग से स्थिति के क्रम में, मुझे क्या बदलना चाहिए, यह जानने की सराहना करता हूं।

import cairo 
if not cairo.HAS_PDF_SURFACE: 
    raise SystemExit('cairo was not compiled with PDF support') 


def draw_image(ctx, image, top, left, height, width): 
    """Draw a scaled image on a given context.""" 
    image_surface = cairo.ImageSurface.create_from_png(image) 
    # calculate proportional scaling 
    img_height = image_surface.get_height() 
    img_width = image_surface.get_width() 
    width_ratio = float(width)/float(img_width) 
    height_ratio = float(height)/float(img_height) 
    scale_xy = min(height_ratio, width_ratio) 
    # scale image and add it 
    ctx.save() 
    ctx.scale(scale_xy, scale_xy) 
    ctx.translate(left, top) 
    ctx.set_source_surface(image_surface) 

    ctx.paint() 
    ctx.restore() 


def draw_box(ctx, left, top, width, height): 
    """Draw a box on a given context.""" 
    ctx.rectangle(left, top, width, height) 
    ctx.set_source_rgb(1, 1, 1) 
    ctx.fill() 
    ctx.rectangle(left, top, width, height) 
    ctx.set_source_rgb(0, 0, 0) 
    ctx.stroke() 


# A4 Page (in points) 
surface = cairo.PDFSurface("box.pdf", 595, 842) 
context = cairo.Context(surface) 
# sizes (in points) 
height = 250 
width = 180 
margin = 20 
# draw boxes 
draw_box(context, margin, margin, width, height) 
draw_box(context, margin + width, margin + height, width, height) 
# draw images - SHOULD be superimposed over rectangles, but are NOT 
image = "hello.png" 
draw_image(context, image, margin, margin, height, width) 
draw_image(context, image, margin + height, margin + width, height, width) 
+0

आपको अच्छे उत्तर पाने की संभावना नहीं है क्योंकि लोग इसे जांचने के लिए बस इस कोड को नहीं चला सकते हैं। यदि संभव हो तो कृपया इसे स्वयं निहित उदाहरण के साथ अपडेट करें (और इसे चलाने के लिए जो भी मॉड्यूल/छवियों/इत्यादि की आवश्यकता है) – agf

+6

प्रतीक्षा करें। आप * स्थापित * PyCairo ?? असंभव। – batman

+0

@agf - वास्तव में यह एक स्वयं निहित स्क्रिप्ट है ... इसे चलाने की कोशिश करते समय आपको क्या समस्याएं आईं? एकमात्र मॉड्यूल आवश्यक कैरो है। – Derek

उत्तर

3

स्केल के क्रम को स्विच करें और अनुवाद करें। पहले अनुवाद करें, फिर स्केल करें।

संबंधित मुद्दे