tinify / tinify-python Goto Github PK
View Code? Open in Web Editor NEWPython client for the Tinify API.
Home Page: https://tinypng.com/developers
License: MIT License
Python client for the Tinify API.
Home Page: https://tinypng.com/developers
License: MIT License
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.
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
.
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
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.
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.
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.
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?
There is no method name of "from_file" in tinify. I'm running tinify 1.5.1 on Python 3.7.4
i have seen the previous solutions for this but it still doesn't solves this issues please how do i go about this ?
This is clearly a test issue.
tinify is installed
$ pip show tinify
Name: tinify
Version: 1.1.0
Location: /Library/Python/2.6/site-packages
Requires: requests
but when i use it by:
source = tinify.from_file("test.png")
only found that :'module' object has no attribute 'from_file'
did i do something wrong?
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.