API Reference

AG亚游集团,整车制造确实不是一件容易的事,尤其是浙江,建国后由于特殊原因,在产业布局上更多地偏向于轻工产业。不过,勤奋的浙江人,愣是在汽摩配产品上走出一条自己的道路,在浙江很多县市都有汽摩配产业,至今我们仍能在慈溪、宁海、瑞安、诸暨等经济发达的县市看到已经形成鲜明块状特色的汽摩配产业。

  国内各个省市充分重视中国软件会对推动国家软件产业发展的重要作用,纷纷利用这一平台为本地产业发展寻求机遇,参加展会的参展省市团组20个,参观团组超过50个。上海市副市长杨雄、天津市政协副主席曹秀荣、甘肃省人大副主任苏志希、深圳市常务副市长刘应力、沈阳市副市长王英、银川市副市长王久彬等省市领导亲自率团参会,体现了全国各省市对中国软件会的高度关注。什么项目前景好央广网广安12月13日消息(记者韩民权广安台记者苏坤明通讯员冯文杰)今天上午,记者从四川广安市邻水县公安局交警大队网站上获悉,广安市交警支队近日公布了1012名吸毒人员被强制注销驾驶证名单,这也是近年来交警部门首次依规向社会发布。

The Tinify API allows you to compress and optimize JPEG and PNG images. It is designed as a REST service. The client libraries in various languages make it very easy to interact with the Tinify API.

Installation

You can use the Python client by installing the Python package:

pip install --upgrade tinify

The source code is available on Github.

Authentication

To use the API you must provide your API key. You can get an API key by registering with your name and email address. Always keep your API key secret!

import tinify
tinify.key = "YOUR_API_KEY"

All requests will be made over an encrypted HTTPS connection.

You can instruct the API client to make all requests over an HTTP proxy. Set the URL of your proxy server, which can optionally include credentials.

tinify.proxy = "/user:pass@192.168.0.1:8080"

Compressing images

You can upload any JPEG or PNG image to the Tinify API to compress it. We will automatically detect the type of image and optimise with the TinyPNG or TinyJPG engine accordingly. Compression will start as soon as you upload a file or provide the URL to the image.

You can choose a local file as the source and write it to another file.

source = tinify.from_file("unoptimized.jpg")
source.to_file("optimized.jpg")

You can also upload an image from a buffer (a string with binary) and get the compressed image data.

with open("unoptimized.jpg", 'rb') as source:
    source_data = source.read()
    result_data = tinify.from_buffer(source_data).to_buffer()

You can provide a URL to your image instead of having to upload it.

source = tinify.from_url("/tinypngaff4com/images/panda-happy.png")
source.to_file("optimized.jpg")

Resizing images

Use the API to create resized versions of your uploaded images. By letting the API handle resizing you avoid having to write such code yourself and you will only have to upload your image once. The resized images will be optimally compressed with a nice and crisp appearance.

You can also take advantage of intelligent cropping to create thumbnails that focus on the most visually important areas of your image.

Resizing counts as one additional compression. For example, if you upload a single image and retrieve the optimized version plus 2 resized versions this will count as 3 compressions in total.

To resize an image, call the resize method on an image source:

source = tinify.from_file("large.jpg")
resized = source.resize(
    method="fit",
    width=150,
    height=100
)
resized.to_file("thumbnail.jpg")

The method describes the way your image will be resized. The following methods are available:

Scale
scale
Scales the image down proportionally. You must provide either a target width or a target height, but not both. The scaled image will have exactly the provided width or height.
Fit
fit
Scales the image down proportionally so that it fits within the given dimensions. You must provide both a width and a height. The scaled image will not exceed either of these dimensions.
Cover
cover
Scales the image proportionally and crops it if necessary so that the result has exactly the given dimensions. You must provide both a width and a height. Which parts of the image are cropped away is determined automatically. An intelligent algorithm determines the most important areas of your image.
Thumb
thumb
A more advanced implementation of cover that also detects cut out images with plain backgrounds. The image is scaled down to the width and height you provide. If an image is detected with a free standing object it will add more background space where necessary or crop the unimportant parts. This feature is new and AG亚游集团we’d love to hear your feedback!

If the target dimensions are larger than the original dimensions, the image will not be scaled up. Scaling up is prevented in order to protect the quality of your images.

Preserving metadata

You can request that specific metadata is copied from the uploaded image to the compressed version. Preserving copyright information, the GPS location and the creation date are currently supported. Preserving metadata adds to the compressed file size, so you should only preserve metadata that is important to keep.

Preserving metadata will not count as an extra compression. However, in the background the image will be created again with the additional metadata.

To preserve specific metadata, call the preserve method on an image source:

source = tinify.from_file("large.jpg")
copyrighted = source.preserve("copyright", "creation")
copyrighted.to_file("optimized-copyright.jpg")

You can provide the following options to preserve specific metadata. No metadata will be added if the requested metadata is not present in the uploaded image.

copyright
Preserves any copyright information. This includes the EXIF copyright tag (JPEG), the XMP rights tag (PNG) as well as a Photoshop copyright flag or URL. Uses up to 90 additional bytes, plus the length of the copyright data.
creation
Preserves any creation date or time. This is the moment the image or photo was originally created. This includes the EXIF original date time tag (JPEG) or the XMP creation time (PNG). Uses around 70 additional bytes.
location (JPEG only)
Preserves any GPS location data that describes where the image or photo was taken. This includes the EXIF GPS latitude and GPS longitude tags (JPEG). Uses around 130 additional bytes.

Saving to Amazon S3

You can tell the Tinify API to save compressed images directly to Amazon S3. If you use S3 to host your images this saves you the hassle of downloading images to your server and uploading them to S3 yourself.

To save an image to S3, call the store method on an image source:

source = tinify.from_file("large.jpg")
source.store(
    service="s3",
    aws_access_key_id="AKIAIOSFODNN7EXAMPLE",
    aws_secret_access_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
    region="us-west-1",
    path="example-bucket/my-images/optimized.jpg"
)

You need to provide the following options in order to save an image on Amazon S3:

service
Specify s3 to store to Amazon S3.
aws_access_key_id
aws_secret_access_key
Your AWS access key ID and secret access key. These are the credentials to an Amazon AWS user account. Find out how to obtain them in Amazon’s documentation. The user must have the correct permissions, see below for details.
region
The AWS region in which your S3 bucket is located.
path
The path at which you want to store the image including the bucket name. The path must be supplied in the following format: <bucket>/<path>/<filename>.

The following settings are optional:

headers (experimental)
You can add a Cache-Control header to control browser caching of the stored image, with for example: public, max-age=31536000. The full list of directives can be found in the MDN web docs.

The user that corresponds to your AWS access key ID must have the PutObject and PutObjectAcl permissions on the paths of the objects you intend to create.

Example S3 access policy

If you want to create a user with limited access specifically for the Tinify API, you can use the following example policy as a starting point:

{
  "Statement": {
    "Effect": "Allow",
    "Action": [
      "s3:PutObject",
      "s3:PutObjectAcl"
    ],
    "Resource": [
      "arn:aws:s3:::example-bucket/*"
    ]
  }
}

Saving to Google Cloud Storage

You can tell the Tinify API to save compressed images directly to Google Cloud Storage. If you use GCS to host your images this saves you the hassle of downloading images to your server and uploading them to GCS yourself.

Before you can store an image in GCS you will need to generate an access token with a service account.

We still need to create a piece of example code for this language that generates an access code. In case you have a working example ready, please share your code!

Once you have generated the access token you can then save the optimised image directly to GCS by calling the store method on an image source:

source = tinify.from_file("large.jpg")
source.store(
    service="gcs",
    gcp_access_token="EXAMPLE_TOKEN_Ag_0HvsglgriGh5_ZmgUBmjaoGU_EXAMPLE_TOKEN",
    path="example-bucket/my-images/optimized.jpg"
)

You need to provide the following options in order to save an image on Google Cloud Storage:

service
Specify gcs to store to Google Cloud Storage.
gcp_access_token
The access token for authenticating to Google's Cloud Platform. Find out how to generate these tokens with the example above.
path
The path at which you want to store the image including the bucket name. The path must be supplied in the following format: <bucket>/<path>/<filename>.

The following settings are optional:

headers (experimental)
You can add a Cache-Control header to control browser caching of the stored image, with for example: public, max-age=31536000. The full list of directives can be found in the AG亚游集团MDN web docs.

Error handling

The Tinify API uses HTTP status codes to indicate success or failure. Any HTTP errors are converted into exceptions, which are thrown by the client library.

There are four distinct types of errors. The exception message will contain a more detailed description of the error condition.

AccountError
There was a problem with your API key or with your API account. Your request could not be authorized. If your compression limit is reached, you can wait until the next calendar month or upgrade your subscription. After verifying your API key and your account status, you can retry the request.
ClientError
The request could not be completed because of a problem with the submitted data. The exception message will contain more information. You should not retry the request.
ServerError
The request could not be completed because of a temporary problem with the Tinify API. It is safe to retry the request after a few minutes. If you see this error repeatedly for a longer period of time, please contact us.
ConnectionError
The request could not be sent because there was an issue connecting to the Tinify API. You should verify your network connection. It is safe to retry the request.

You can handle each type of error separately:

try:
  # Use the Tinify API client.
  pass
except tinify.AccountError, e:
  print "The error message is: %s" % e.message
  # Verify your API key and account limit.
except tinify.ClientError, e:
  # Check your source image and request options.
  pass
except tinify.ServerError, e:
  # Temporary issue with the Tinify API.
  pass
except tinify.ConnectionError, e:
  # A network connection error occurred.
  pass
except Exception, e:
  # Something else went wrong, unrelated to the Tinify API.
  pass

If you are writing code that uses an API key configured by your users, you may want to validate the API key before attempting to compress images. The validation makes a dummy request to check the network connection and verify the API key. An error is thrown if the dummy request fails.

try:
  tinify.key = "YOUR_API_KEY"
  tinify.validate()
except tinify.Error, e:
  # Validation of API key failed.
  pass

Compression count

The API client automatically keeps track of the number of compressions you have made this month. You can get the compression count after you have validated your API key or after you have made at least one compression request.

compressions_this_month = tinify.compression_count

Need help? Got feedback?

We’re always here to help, so if you’re stuck just drop us a note on AG亚游集团support@tinify.com. It’s also the perfect place to send us all your suggestions and feedback.

Try TinyPNG with a new browser

TinyPNG is created for modern browsers with HTML5 & CSS3 support. We have not tried it out in other browsers. The site may work, or it may not. If you see this message you may want to try a different browser!

高处“不觉寒” 镍价强势依旧 乒乓球波兰赛韩国队包揽四金 梁夏银成就双冠王 西格:现在展望世界杯还早 满意亚洲杯女足表现 我国产融结合发展浅析 Mozilla高管抱怨谷歌偏心 Firefox访问Yo… B站确认被约谈:将扩编一倍以上审核人力 传播正能量 巴坎布&比埃拉已完成注册 顺利获新赛季出场资格 中国放射学家金征宇获欧洲放射学会荣誉会员称号 工人中6千万20年后 豪宅内景曝光太震撼-组图 女生发掀裙自拍照震惊网络:年纪轻轻却离不开尿布 住建部10天两度重申房地产调控不放松 遏制炒房 4名城管高校门口殴打瓜贩 官方:临时工 已停职
兰州牛肉拉面多赚钱?北京一家小店年入近200万元 315晚会上这些企业被曝光 你上当了吗? 马云首次回应阿里收购中天微:让芯片更具普惠性 零售与科技股推动美股收高 罗素2000指数创历史新高 吃鸡“外挂”藏木马 一天20万电脑中毒 格德斯:听说要对中超裁判客气点 首要目标拿冠军 云南边境扫雷获阶段成果:380万平米雷场重归和平 禁止化武组织报告:前俄间谍所中毒剂为诺维乔克 韩媒:朝鲜提议16日在板门店举行高级别会谈 证监会核发两公司IPO批文 宁德时代在列 大数据助小炮独擒3.25高赔!中超日职早场再战 美国大搞“石油孤立”后 这个大国或出手救伊朗?
穆帅:瓜迪奥拉要是还砸钱 那曼联肯定追不上 英媒称美俄关系甚于冷战 伊朗誓言将替叙报复以色列 陆奇将卸任百度总裁兼COO 百度周五早盘股价大跌超8% 佩雷拉:比赛结果难以接受 我们今天表现值得晋级 85后私募传奇:钉住一个板块 三年赚8个亿 卓尔创队史最佳开局却心惊肉跳 李铁还需一个稳字 霍金为何与诺奖无缘?或因“黑洞”理论难求证 你不知道的一代香港马神:演戏是副业赛马才是主业 竞彩大势:罗森博格客场不败 巴西国际难高看 最有前景的十大行业 女生最值得学的手艺 白手起家的成功案例 养20头牛一年赚多少钱 AG亚游集团