10

पर क्रॉप करने के लिए मुझे एक छवि को आकार देने और एक विशिष्ट चौड़ाई और ऊंचाई पर फसल करने की आवश्यकता है। मैं एक विधि बनाने में सक्षम था जो स्क्वायर थंबनेल बनाएगा, लेकिन मुझे यह लागू करने के बारे में अनिश्चितता है कि वांछित थंबनेल स्क्वायर नहीं है।ऐप इंजन एक विशिष्ट चौड़ाई और ऊंचाई

def rescale(data, width, height): 
"""Rescale the given image, optionally cropping it to make sure the result image has the specified width and height.""" 
from google.appengine.api import images 

new_width = width 
new_height = height 

img = images.Image(data) 

org_width, org_height = img.width, img.height 

# We must determine if the image is portrait or landscape 
# Landscape 
if org_width > org_height: 
    # With the Landscape image we want the crop to be centered. We must find the 
    # height to width ratio of the image and Convert the denominater to a float 
    # so that ratio will be a decemal point. The ratio is the percentage of the image 
    # that will remain. 
    ratio = org_height/float(org_width) 
    # To find the percentage of the image that will be removed we subtract the ratio 
    # from 1 By dividing this number by 2 we find the percentage that should be 
    # removed from each side this is also our left_x coordinate 
    left_x = (1- ratio)/2 
    # By subtract the left_x from 1 we find the right_x coordinate 
    right_x = 1 - left_x 
    # crop(image_data, left_x, top_y, right_x, bottom_y), output_encoding=images.PNG) 
    img.crop(left_x, 0.0, right_x, 1.0) 
    # resize(image_data, width=0, height=0, output_encoding=images.PNG) 
    img.resize(height=height) 
# Portrait 
elif org_width < org_height: 
    ratio = org_width/float(org_height) 
    # crop(image_data, left_x, top_y, right_x, bottom_y), output_encoding=images.PNG) 
    img.crop(0.0, 0.0, 1.0, ratio) 
    # resize(image_data, width=0, height=0, output_encoding=images.PNG) 
    img.resize(width=witdh) 

thumbnail = img.execute_transforms() 
return thumbnail 

यदि ऐसा करने का एक बेहतर तरीका है तो कृपया मुझे बताएं। किसी भी तरह की सहायता का स्वागत किया जाएगा।

वांछित प्रक्रिया को समझाते हुए एक आरेख है। crop_diagram

धन्यवाद,

केली

उत्तर

17

मुझे एक ही समस्या थी (आपका स्क्रीनशॉट बहुत उपयोगी था)। यह मेरा समाधान है:

def rescale(img_data, width, height, halign='middle', valign='middle'): 
    """Resize then optionally crop a given image. 

    Attributes: 
    img_data: The image data 
    width: The desired width 
    height: The desired height 
    halign: Acts like photoshop's 'Canvas Size' function, horizontally 
      aligning the crop to left, middle or right 
    valign: Verticallly aligns the crop to top, middle or bottom 

    """ 
    image = images.Image(img_data) 

    desired_wh_ratio = float(width)/float(height) 
    wh_ratio = float(image.width)/float(image.height) 

    if desired_wh_ratio > wh_ratio: 
    # resize to width, then crop to height 
    image.resize(width=width) 
    image.execute_transforms() 
    trim_y = (float(image.height - height)/2)/image.height 
    if valign == 'top': 
     image.crop(0.0, 0.0, 1.0, 1 - (2 * trim_y)) 
    elif valign == 'bottom': 
     image.crop(0.0, (2 * trim_y), 1.0, 1.0) 
    else: 
     image.crop(0.0, trim_y, 1.0, 1 - trim_y) 
    else: 
    # resize to height, then crop to width 
    image.resize(height=height) 
    image.execute_transforms() 
    trim_x = (float(image.width - width)/2)/image.width 
    if halign == 'left': 
     image.crop(0.0, 0.0, 1 - (2 * trim_x), 1.0) 
    elif halign == 'right': 
     image.crop((2 * trim_x), 0.0, 1.0, 1.0) 
    else: 
     image.crop(trim_x, 0.0, 1 - trim_x, 1.0) 

    return image.execute_transforms() 
+0

धन्यवाद। यही वही है जो मैं ढूंढ रहा था। –

+0

कोड के लिए बहुत बहुत धन्यवाद, कमाल काम करता है! – goggin13

+0

thx! ठीक है जो मैं – fceruti

2

आप resize करने के लिए दोनों heightऔरwidth पैरामीटर निर्दिष्ट कर सकते हैं - यह पहलू अनुपात में परिवर्तन नहीं होगा (यदि आप ऐसा नहीं कर सकते कि साथ GAE के images मॉड्यूल), लेकिन यह सुनिश्चित करेगा कि दो आयामों में से प्रत्येक <= आपके द्वारा निर्दिष्ट संबंधित मान (वास्तव में, एक आपके द्वारा निर्दिष्ट मूल्य के बराबर होगा, दूसरा <= होगा)।

मुझे यकीन नहीं है कि आप पहले क्यों फसल कर रहे हैं और बाद में आकार बदल रहे हैं - ऐसा लगता है कि आपको चीजों को अन्य तरीकों से करना चाहिए ... आकार बदलें ताकि मूल छवि जितनी संभव हो सके "फिट हो" फिर सटीक परिणामी आयाम सुनिश्चित करने के लिए फसल। (इसलिए आप आकार बदलने के लिए ऊंचाई और चौड़ाई के मूल प्रदान किए गए मानों का उपयोग नहीं करेंगे - आप उन्हें स्केल करेंगे ताकि परिणामस्वरूप छवि में से कोई भी "बर्बाद" उर्फ ​​"रिक्त" न हो, अगर मैं आपकी आवश्यकताओं को सही ढंग से समझता हूं)। तो हो सकता है कि मैं वास्तव में समझ में नहीं आ रहा हूं कि आपको क्या चाहिए - क्या आप एक उदाहरण प्रदान कर सकते हैं (एक छवि के लिए यूआरएल जैसा कि यह प्रसंस्करण से पहले दिखता है, यह प्रसंस्करण के बाद कैसा दिखता है, और आपके द्वारा पारित किए जा रहे पैरामीटर के विवरण) ?

+0

धन्यवाद एलेक्स, उदाहरण के लिए कहें मेरे पास एक ऐसी छवि है जिसमें 500px की चौड़ाई और 300px की ऊंचाई है। मैं फसल की छवि (थंबनेल) 150px की चौड़ाई और 100px की ऊंचाई होना चाहता हूं। मुझे विश्वास है कि पहले आकार बदलने का आपका अधिकार है। मुझे यकीन है कि पूरा कार्य काफी सरल है, मैं अभी कोड के साथ संघर्ष कर रहा हूं। प्रक्रिया का वर्णन करने वाले आरेख का एक लिंक यहां दिया गया है। http://farm3.static.flickr.com/2543/4205690696_b3821a12e9_o.gif –

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