Giter Site home page Giter Site logo

tinify / tinify-python Goto Github PK

View Code? Open in Web Editor NEW
127.0 13.0 20.0 280 KB

Python client for the Tinify API.

Home Page: https://tinypng.com/developers

License: MIT License

Python 99.09% Shell 0.91%
image-compression image-optimization tinypng tinyjpg python python-client

tinify-python's Introduction

MIT License CI PyPI PyPI - Python Version PyPI - Wheel

Tinify API client for Python

Python client for the Tinify API, used for TinyPNG and TinyJPG. Tinify compresses your images intelligently. Read more at http://tinify.com.

Documentation

Go to the documentation for the Python client.

Installation

Install the API client:

pip install tinify

Usage

import tinify
tinify.key = 'YOUR_API_KEY'

tinify.from_file('unoptimized.png').to_file('optimized.png')

Running tests

pip install -r requirements.txt -r test-requirements.txt
py.test

To test more runtimes, tox can be used

tox

Integration tests

pip install -r requirements.txt -r test-requirements.txt
TINIFY_KEY=$YOUR_API_KEY py.test test/integration.py

License

This software is licensed under the MIT License. View the license.

tinify-python's People

Contributors

eddyh avatar geonu avatar mattijsvandruenen avatar middagj avatar rkoopmans avatar rolftimmermans avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tinify-python's Issues

Tinify "fit" example from API reference doesn't work

I created a test program on my machine to verify behavior but it doesn't seem to work. It does seem to be going to the API and getting compressed, but the original dimensions seem to be preserved, meaning "fit" isn't being respected.

Test program:

import tinify

tinify.key = "SECRETKEY"
source = tinify.from_file("./resources/images/blah.jpg")
source.resize(
    method="scale",
    width=150
)
source.to_file("./output/output.jpg")

I then tested with "scale" and "cover" and both of those have the same behavior - compresssing but not actually mutating the image, making me think that the action word just isn't being passed down.

Keep getting Error while connecting

I have a valid API key. If I run tinify.validate() I get a True returned.

When I want to use the from_file method, I keep getting the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jeroen/.virtualenvs/dingen/lib/python2.7/site-packages/tinify/__init__.py", line 65, in from_file
    return Source.from_file(path)
  File "/home/jeroen/.virtualenvs/dingen/lib/python2.7/site-packages/tinify/source.py", line 14, in from_file
    return cls._shrink(f.read())
  File "/home/jeroen/.virtualenvs/dingen/lib/python2.7/site-packages/tinify/source.py", line 26, in _shrink
    response = tinify.get_client().request('POST', '/shrink', obj)
  File "/home/jeroen/.virtualenvs/dingen/lib/python2.7/site-packages/tinify/client.py", line 52, in request
    raise ConnectionError('Error while connecting: {0}'.format(err), cause=err)
tinify.errors.ConnectionError: Error while connecting: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

I am not sure what's wrong. The file is accessible and Python is able to read it.

AttributeError: module 'flask.sessions' has no attribute 'Session'

I'm using tinify with flask to compress images after uploading them, but I get the following error: AttributeError: module 'flask.sessions' has no attribute 'Session'

Here's the full trace:

Traceback (most recent call last):
  File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "D:\projects\docspert\venv\Lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "D:\projects\docspert\venv\Lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "D:\projects\docspert\venv\Lib\site-packages\flask\app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\projects\docspert\admin\core\auth_sys.py", line 93, in f
    return function(*args, **kwargs)
  File "D:\projects\docspert\admin\url_rules\affiliates\webinars\__root__.py", line 150, in post
    'error': str(error)
  File "D:\projects\docspert\venv\Lib\site-packages\tinify\__init__.py", line 79, in from_file
    return Source.from_file(path)
  File "D:\projects\docspert\venv\Lib\site-packages\tinify\source.py", line 14, in from_file
    return cls._shrink(f.read())
  File "D:\projects\docspert\venv\Lib\site-packages\tinify\source.py", line 26, in _shrink
    response = tinify.get_client().request('POST', '/shrink', obj)
  File "D:\projects\docspert\venv\Lib\site-packages\tinify\__init__.py", line 60, in get_client
    self._client = Client(self._key, self._app_identifier, self._proxy)
  File "D:\projects\docspert\venv\Lib\site-packages\tinify\client.py", line 25, in __init__
    self.session = requests.sessions.Session()
AttributeError: module 'flask.sessions' has no attribute 'Session'

Here's the code:

file.save(os.path.join(app.config['FILES_WEBINARS_POSTERS'], postersrc))
source = tinify.from_file(os.path.join(app.config['FILES_WEBINARS_POSTERS'], postersrc)) # the error is from this line
source.to_file(os.path.join(app.config['FILES_WEBINARS_POSTERS'], "optimized.jpg"))

The file uploading is working properly and files get uploaded but the optimized images don't get uploaded.

How can I fix this?

Tinify converts images to JPEG instead of given input type

When I try to convert a JPG or PNG file to WebP, I get a JPEG file as output without any errors. At first I thought that something in my code was causing this strange behaviour, but then I hardcoded the values and I am still geting the same output.

import tinify


try:
    tinify.key = ...
    tinify.validate()
except tinify.Error as e:
    print(e)


source = tinify.from_file("space-1.jpg")
converted = source.convert(type=["image/webp"])
extension = converted.result().extension
converted.to_file("space-1." + extension)

Output: space-1.jpeg

Am I missing something? Not long ago, everything was working fine. Compressing and resizing still works normally.

Test

This is clearly a test issue.

Image grows in size after compressing

After calling tinify.from_file(image) on a few different images the image actually gets larger.

Here is one of the images that grew in size: https://docs.moonbeam.network/images/tokens/connect/mathwallet/mathwallet-5.png

The original file size is: 1050.744140625KB and after the compression the size grows to: 1059.2841796875KB

Is there a reason why the image size increased? I think at the very least, if the image grows in size after compressing, the original image should be returned instead.

Optionally use aiohttp instead of requests if desired.

Currently requests uses thread.sleep or w/e that can deadlock code that uses asyncio in python 3. Because of this aiohttp was created for use with async code that uses asyncio. As such it would be nice to support aiohttp first if they have aiohttp installed. Or provide an special class or something that us asyncio users can call with our async code to make the requests to this API. This is so our async code does not deadlock. If a class is made for this you guys can make it so that it is only shown when aiohttp is installed without explicitly requireing it to be installed.

Why aiohttp over requests?

Well aiohttp supports everything that requests does, but also for async code only. It is perfect for just about all sorts of web based requests in async code. For an example project that uses aiohttp and asyncio you can pip install discord.py or view it on github under Rapptz/discord.py.

Service is adding invalid Expires header

I am using the Tinify python package to compress images, after which it uploads them to our S3 bucket. The store method is used.

I am finding the Tinify is adding an Expires header with a value string of Invalid Date to the metadata for the S3 object. This breaks some downstream services that parse headers on the object, as they do not expect this bad value. I think this started to be noticed towards the beginning of November 2018.

I was able to bypass it in the ruby version of the package by manually specifying the header value in the store method, which sets a proper value. However, this shouldn't be required...and from what I can tell it wasn't even a present header on uploaded objects in the past before this error started happening.

python 2.6 support?

Hello,

The tinify page on PyPi suggests that python 2.6 is still supported.

However, I'm getting

  File "./tinify-test.py", line 28, in <module>
    source = tinify.from_file(uncompressed_path)
  File "/home/.../tinify_venv/lib/python2.6/site-packages/tinify/__init__.py", line 79, in from_file
    return Source.from_file(path)
  File "/home/.../tinify_venv/lib/python2.6/site-packages/tinify/source.py", line 14, in from_file
    return cls._shrink(f.read())
  File "/home/.../tinify_venv/lib/python2.6/site-packages/tinify/source.py", line 26, in _shrink
    response = tinify.get_client().request('POST', '/shrink', obj)
  File "/home/.../tinify_venv/lib/python2.6/site-packages/tinify/client.py", line 64, in request
    raise ConnectionError('Error while connecting: {0}'.format(err), cause=err)
tinify.errors.ConnectionError: Error while connecting: request() keywords must be strings

with

$ python --version
Python 2.6.4

and

$ pip freeze
requests==2.14.2
tinify==1.5.0
wsgiref==0.1.2

looks like it might be similar to closed issue #2

I don't get the error in a python 2.7 virtualenvironment

No module named 'tinify'

i have seen the previous solutions for this but it still doesn't solves this issues please how do i go about this ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.