Python Forum
Convert .bmp images to .gif inside script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert .bmp images to .gif inside script
#1
I am writing a python program that applies filters to images. I am running an image through two different functions. One of them only takes .bmp files and the other doesn't take .bmp files but can take .gif or .ppm files. How do I convert the image from .bmp to .gif before running it through the second function.
Reply
#2
You can use io module.
Something like this:

from io import BytesIO

with BytesIO()as image:
    img.save(image, format='GIF') # the img is the picture you have already opened
    image.seek(0) # move the pointer back to the beginning of the "file"
    my_gif = Image.open(image)
Now you can work with my_gif object.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(Apr-25-2018, 05:26 AM)wavic Wrote: You can use io module.
Something like this:

from io import BytesIO

with BytesIO()as image:
    img.save(image, format='GIF') # the img is the picture you have already opened
    image.seek(0) # move the pointer back to the beginning of the "file"
    my_gif = Image.open(image)
Now you can work with my_gif object.

Thank you! The solution you provided me works but I get an error when I try to implement it in my filter. Here is my code. Further below is the error. smoothImage is a function from mean_filter.py that improves the quality of pixelated images by applying a filter to them.
[inline]
import cv2
import io
from io import BytesIO
from mean_filter import *

myImage = cv2.imread("people3_pix.bmp")
cv2.imwrite("output.bmp", myImage)

with open("output.bmp", "r+") as img:
    imageToFilter = img.read()
    
with BytesIO() as image:
    imageToFilter.save(image, format = "GIF")
    image.seek(0)
    my_gif = Image.open(image)

myImage1 = smoothImage(my_gif) 
[/inline]


[inline]Traceback (most recent call last):
File "test.py", line 10, in <module>
imageToFilter = img.read()
File "/usr/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 2: invalid start byte
[/inline]
Reply
#4
You are opening the image in 'r' mode. You should use 'b'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Apr-25-2018, 07:48 PM)wavic Wrote: You are opening the image in 'r' mode. You should use 'b'


If I use "b" mode, I get:

[inline]Traceback (most recent call last):
File "test.py", line 11, in <module>
with open("output.bmp", "b+") as img:
ValueError: Must have exactly one of create/read/write/append mode and at most one plus
[/inline]

If I use "rb" mode, I get:

[inline]Traceback (most recent call last):
File "test.py", line 15, in <module>
imageToFilter.save(image, format = "GIF")
AttributeError: 'bytes' object has no attribute 'save'
[/inline]
Reply
#6
Why 'b+'?
Why two ways to open an image? You already use the cv2 module for that. Why Python file IO?

imageToFilter = cv2.imread('output.bmp')
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 350 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  Python Script to convert Json to CSV file chvsnarayana 8 2,524 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
Question convert autohotkey script to python / macro - press key when pixel get colour willson94d 1 3,655 Jan-01-2022, 08:13 PM
Last Post: Yoriz
  Convert .py script to dll file and .exe srikanthpython 0 3,136 Aug-20-2020, 06:01 PM
Last Post: srikanthpython
  How to do the same as python -m requests.certs inside script? geekgeek 2 2,278 Feb-27-2020, 07:11 AM
Last Post: buran
  Python script that recursively zips folders WITHOUT nesting the folder inside the zip umkc1 1 2,853 Feb-11-2020, 09:12 PM
Last Post: michael1789
  Convert Python script to Php JuroClash 0 8,155 Oct-21-2019, 03:01 PM
Last Post: JuroClash
  convert sh script to python script Reims 0 2,158 Oct-01-2019, 12:10 PM
Last Post: Reims
  Is there any way to convert a python script (with Tkinter module), to a .exe (Windows moste 3 4,011 May-12-2019, 12:02 PM
Last Post: snippsat
  Issue with a script to convert xls to json Will86 2 3,827 Dec-19-2018, 08:23 AM
Last Post: Will86

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020